我的項目中有兩個 go 文件主程序該文件創建 http 服務器和 mongoDB 連接以及允許使用以下重用連接的方法func ConnectMongoDB() { ctx, _ := context.WithTimeout(context.Background(), 30*time.Second) // user Connection database // Set client options clientOptions := options.Client().ApplyURI("mongodb+srv://localhost:27017/demo") // Connect to MongoDB userclient, err = mongo.Connect(ctx, clientOptions) if err != nil { log.Fatal(err) } // Check the connection err = userclient.Ping(ctx, nil) if err != nil { log.Fatal(err) } fmt.Println("Connected to user MongoDB!")}//GetMongoDBClient , return mongo client for CRUD operationsfunc GetMongoDBClient() *mongo.Client { return userclient}查詢.go然后該文件定義數據庫,然后對其進行查詢client := GetMongoDBClient()collection := client.Database("demo").Collection("user")err := collection.FindOne(context.TODO(), filter).Decode(&user)當我發出 200 個請求時,我收到 Atlas 發來的電子郵件,說我已經超出了 80 個連接限制配額。這里如何利用連接池呢?
- 2 回答
- 0 關注
- 240 瀏覽
添加回答
舉報
0/150
提交
取消