The go-mongo-driver documentation at https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial recommends the following:
It is best practice to keep a client that is connected to MongoDB around so that the application can make use of connection pooling - you don't want to open and close a connection for each query.
My question is: Is there a best practice for how to do this?
I'm running an RPC service and continually listening for requests. When I receive a request I make a call to the mongo server. What I do not want to do is continually connect and disconnect to mongo.
I've attempted a solution by creating the mongo client as a global variable and then deferring the disconnection in the main function... It does not feel like a good solution.
var mongoClient *mongo.Client = buildMongoClient()
func main() {
defer disconnectFromMongo()
*** Do all the things ***
...
}