Skip to content

kep-w/MongoDB-Connection-Pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang MongoDB connection pool

Efficiently control the number of database connections and realize automatic connection management

Details:

Provide connection pool management through struct MongoPool.

The connection pool parameters are set during initialization, including:

  • cached channel for storing connections, and the cache controls the maximum number of idle connections.
  • Database address,
  • timeout period,
  • maximum number of connections.

for example:

func init() {
	once.Do(func() {
		DBPool = &MongoPool{
			pool:        make(chan *mongo.Client, 10), 
			connections: 0,                            
			timeout:     10 * time.Second,
			uri:         "mongodb://user:[email protected]:27017/dbname",
			poolSize:    10, 
		}
	})
}

Note: You can use function once.Do to ensure that the connection pool is only created once.

About

Golang MongoDB Connection Pool

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages