Guest User

Untitled

a guest
Nov 20th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "time"
  5.  
  6. "fmt"
  7.  
  8. "gopkg.in/mgo.v2"
  9. )
  10.  
  11. func main() {
  12. // No connections
  13. // db.serverStatus().connections.current = 6
  14.  
  15. mongoSession := connectMGO("localhost", "27017", "admin")
  16. // 1 new connection created
  17. //db.serverStatus().connections.current = 7
  18.  
  19. produceDataMGO(mongoSession)
  20. produceDataMGO(mongoSession)
  21. produceDataMGO(mongoSession)
  22. produceDataMGO(mongoSession)
  23. // 4 new connections created and closed
  24. // db.serverStatus().connections.current = 7
  25.  
  26. go produceDataMGO(mongoSession)
  27. go produceDataMGO(mongoSession)
  28. go produceDataMGO(mongoSession)
  29. go produceDataMGO(mongoSession)
  30. // 4 new connections created and closed concurrently
  31. // db.serverStatus().connections.current = 10
  32.  
  33. time.Sleep(time.Hour * 24) // wait any amount of time
  34. // db.serverStatus().connections.current = 10
  35. }
  36.  
  37. func connectMGO(host, port, dbName string) *mgo.Session {
  38. session, _ := mgo.DialWithInfo(&mgo.DialInfo{
  39. Addrs: []string{fmt.Sprintf("%s:%s", host, port)},
  40. Timeout: 10 * time.Second,
  41. Database: dbName,
  42. Username: "",
  43. Password: "",
  44. })
  45. return session
  46. }
  47.  
  48. func produceDataMGO(conn *mgo.Session) {
  49. dbConn := conn.Copy()
  50. dbConn.DB("").C("test").Insert("")
  51. dbConn.Close()
  52. }
Add Comment
Please, Sign In to add comment