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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ESP8266连接阿里云出现问题
    • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并