So at this point in time I have the *mongo.client & mongo.Session I know *mongo.client is go routine safe and we use that for susbsequent operations , Also we do connect only once when we try to bring the docker instance .
So my question is when we bring down all the containers ? Do I need a teardown mechanism to say disconnect the client and mongo.session ? If I don’t do that what will be the problem . And if I can get a recommended way of how to close the session and client that would be helpful .
It’s best practice to call Disconnect on any connected Client when you no longer need it or when the application is shutting down. If you pass a Context with a timeout to Disconnect, it will disconnect the Client gracefully, including waiting up to the configured timeout for any in-progress operations to finish.
Example of disconnecting a client, waiting up to 30 seconds for in-progress operations to complete:
conn, err := mongo.Connect(...)
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background, 30*time.Second)
defer cancel()
defer conn.Disconnect(ctx)
// Run operations with the Client...
If you’re starting sessions with StartSession, you should eventually end that session by calling EndSession to avoid leaking sessions, which can lead to a “TooManyLogicalSessions” error. Sessions also have a maximum lifetime of 30 minutes, at which point the database will end the session.
Example of starting and ending a session:
sess, err := conn.StartSession()
if err != nil {
panic(err)
}
// Use the Session to run causally consistent
// operations or transactions...
sess.EndSession(context.TODO())