Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bug hunting
package main
import (
"log"
"time"
mgo "gopkg.in/mgo.v2"
)
func init() {
mgo.SetStats(true)
go func() {
for _ = range time.Tick(500 * time.Millisecond) {
stats := mgo.GetStats()
log.Printf("Master connections: %v - Sockets alive: %v - Sockets in use: %v - Socket Refs: %v",
stats.MasterConns, stats.SocketsAlive, stats.SocketsInUse, stats.SocketRefs)
}
}()
}
func main() {
session, err := mgo.Dial("mongodb://localhost")
if err != nil {
log.Fatal(err.Error())
}
for i := 0; i < 2000; i++ {
go openAndClose(session, i)
time.Sleep(10 * time.Millisecond)
}
}
func openAndClose(session *mgo.Session, i int) {
s := session.Copy()
c := s.DB("test").C("data")
err := c.Insert(struct{ Foo string }{"bar"})
if err != nil {
log.Print(err.Error())
}
time.Sleep(20 * time.Second)
s.Close()
}
package main
import (
"log"
"time"
mgo "gopkg.in/mgo.v2"
db "upper.io/db.v1"
"upper.io/db.v1/mongo"
)
func getSession() (db.Database, error) {
settings, err := mongo.ParseURL("mongodb://localhost/test")
if err != nil {
return nil, err
}
return db.Open(mongo.Adapter, settings)
}
func init() {
mgo.SetStats(true)
go func() {
for _ = range time.Tick(500 * time.Millisecond) {
stats := mgo.GetStats()
log.Printf("Master connections: %v - Sockets alive: %v - Sockets in use: %v - Socket Refs: %v",
stats.MasterConns, stats.SocketsAlive, stats.SocketsInUse, stats.SocketRefs)
}
}()
}
func main() {
session, err := getSession()
if err != nil {
log.Fatal(err.Error())
}
for i := 0; i < 2000; i++ {
go openAndClose(session, i)
time.Sleep(10 * time.Millisecond)
}
}
func openAndClose(session db.Database, i int) {
s, err := session.Clone()
if err != nil {
log.Print(err.Error())
return
}
c, err := s.Collection("data")
if err != nil {
log.Print(err.Error())
return
}
_, err = c.Append(struct{ Foo string }{"bar"})
if err != nil {
log.Print(err.Error())
}
time.Sleep(20 * time.Second)
s.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment