Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "time"
- "fmt"
- "gopkg.in/mgo.v2"
- )
- func main() {
- // No connections
- // db.serverStatus().connections.current = 6
- mongoSession := connectMGO("localhost", "27017", "admin")
- // 1 new connection created
- //db.serverStatus().connections.current = 7
- produceDataMGO(mongoSession)
- produceDataMGO(mongoSession)
- produceDataMGO(mongoSession)
- produceDataMGO(mongoSession)
- // 4 new connections created and closed
- // db.serverStatus().connections.current = 7
- go produceDataMGO(mongoSession)
- go produceDataMGO(mongoSession)
- go produceDataMGO(mongoSession)
- go produceDataMGO(mongoSession)
- // 4 new connections created and closed concurrently
- // db.serverStatus().connections.current = 10
- time.Sleep(time.Hour * 24) // wait any amount of time
- // db.serverStatus().connections.current = 10
- }
- func connectMGO(host, port, dbName string) *mgo.Session {
- session, _ := mgo.DialWithInfo(&mgo.DialInfo{
- Addrs: []string{fmt.Sprintf("%s:%s", host, port)},
- Timeout: 10 * time.Second,
- Database: dbName,
- Username: "",
- Password: "",
- })
- return session
- }
- func produceDataMGO(conn *mgo.Session) {
- dbConn := conn.Copy()
- dbConn.DB("").C("test").Insert("")
- dbConn.Close()
- }
Add Comment
Please, Sign In to add comment