dongxiangchan0743 2019-04-10 12:05
浏览 243

通过mongo-go-driver连接mongodb会消耗大量内存

I'm using mongo-go-driver, parallel connect several ports to check whether the port is listened by mongodb

go version 1.12.3 mongo-go-driver v1.0

type BaseServerStatus struct {
   Host           string    `bson:"host"`
   Version        string    `bson:"version"`
   Process        string    `bson:"process"`
   Pid            int64     `bson:"pid"`
   Uptime         int64     `bson:"uptime"`
   UptimeMillis   int64     `bson:"uptimeMillis"`
   UptimeEstimate int64     `bson:"uptimeEstimate"`
   LocalTime      time.Time `bson:"localTime"`
}


func GetBaseServerStatus(ip, port string) (srvStatus *BaseServerStatus, err error) {
   opts := options.Client()
   opts.SetDirect(true)
   opts.SetServerSelectionTimeout(1 * time.Second)
   opts.SetConnectTimeout(2 * time.Second)
   opts.SetSocketTimeout(2 * time.Second)
   opts.SetMaxConnIdleTime(1 * time.Second)
   opts.SetMaxPoolSize(1)
   url := fmt.Sprintf("mongodb://%s:%s/admin", ip, port)
   opts.ApplyURI(url)
   ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
   conn, err := mongo.Connect(ctx, opts)
   if err != nil {
      fmt.Printf("new %s:%s mongo connection error: %v
", ip, port, err)
      return
   }
   defer conn.Disconnect(ctx)
   err = conn.Ping(ctx, nil)
   if err != nil {
      fmt.Printf("ping %s:%s ping error: %v
", ip, port, err)
      return
   }
   sr := conn.Database("admin").RunCommand(ctx, bson.D{{"serverStatus", 1}})
   if sr.Err() != nil {
      fmt.Printf("get  %s:%s server status error: %v
", ip, port, sr.Err())
      return
   }
   srvStatus = new(BaseServerStatus)
   err = sr.Decode(srvStatus)
   return
}


func main()  {
   var wg sync.WaitGroup
   //ips := []string{"xxx.xxx.xxx.xxx:22"}
   ips := []string{"xxx.xxx.xxx.xxx:22", "xxx.xxx.xxx.xxx:80", "xxx.xxx.xxx.xxx:7005", "xxx.xxx.xxx.xxx:7017", "xxx.xxx.xxx.xxx:7006", "xxx.xxx.xxx.xxx:7016", "xxx.xxx.xxx.xxx:7018", "xxx.xxx.xxx.xxx:7014", "xxx.xxx.xxx.xxx:199", "xxx.xxx.xxx.xxx:8182", "xxx.xxx.xxx.xxx:7015", "xxx.xxx.xxx.xxx:7022", "xxx.xxx.xxx.xxx:7013", "xxx.xxx.xxx.xxx:7020", "xxx.xxx.xxx.xxx:9009", "xxx.xxx.xxx.xxx:7004", "xxx.xxx.xxx.xxx:7008", "xxx.xxx.xxx.xxx:7002", "xxx.xxx.xxx.xxx:7021", "xxx.xxx.xxx.xxx:7007", "xxx.xxx.xxx.xxx:7024", "xxx.xxx.xxx.xxx:7010", "xxx.xxx.xxx.xxx:7011", "xxx.xxx.xxx.xxx:7003", "xxx.xxx.xxx.xxx:7012", "xxx.xxx.xxx.xxx:7009", "xxx.xxx.xxx.xxx:7019", "xxx.xxx.xxx.xxx:8001", "xxx.xxx.xxx.xxx:7023", "xxx.xxx.xxx.xxx:111", "xxx.xxx.xxx.xxx:7001", "xxx.xxx.xxx.xxx:8002", "xxx.xxx.xxx.xxx:19313", "xxx.xxx.xxx.xxx:15772", "xxx.xxx.xxx.xxx:19777", "xxx.xxx.xxx.xxx:15778", "xxx.xxx.xxx.xxx:15776"}

   for _, ip := range ips {
      wg.Add(1)
      //time.Sleep(3 * time.Second)
      go func(addr string) {
         fmt.Printf("start to probe port %s
", addr)
         GetBaseServerStatus(strings.Split(addr, ":")[0], strings.Split(addr, ":")[1])
         wg.Done()
      }(ip)
   }
   wg.Wait()
   fmt.Println("scan end")
   time.Sleep(20 * time.Second)

}

however, run this code ,it consume 26GB memory I use pprof to diagnose, see below

Showing top 10 nodes out of 15 flat flat% sum% cum cum% 26.29GB 96.86% 96.86% 26.29GB 96.86% go.mongodb.org/mongo-driver/x/network/connection.(*connection).ReadWireMessage

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 有人会搭建GPT-J-6B框架吗?有偿
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名