Last active
August 4, 2016 21:01
-
-
Save pierrebeaucamp/eed2f6d5e3269b8889b196b7a6ea596e to your computer and use it in GitHub Desktop.
Bug hunting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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