Skip to content

Commit

Permalink
Add number of clusters alive to statistics, and add leak check to suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Jun 17, 2013
1 parent 406b882 commit 19f9c70
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func newCluster(userSeeds []string, direct bool, dial dialer) *mongoCluster {
}
cluster.serverSynced.L = cluster.RWMutex.RLocker()
cluster.sync = make(chan bool, 1)
stats.cluster(+1)
go cluster.syncServersLoop()
return cluster
}
Expand Down Expand Up @@ -91,6 +92,7 @@ func (cluster *mongoCluster) Release() {
}
// Wake up the sync loop so it can die.
cluster.syncServers()
stats.cluster(-1)
}
cluster.Unlock()
}
Expand Down
10 changes: 10 additions & 0 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func ResetStats() {
old := stats
stats = &Stats{}
// These are absolute values:
stats.Clusters = old.Clusters
stats.SocketsInUse = old.SocketsInUse
stats.SocketsAlive = old.SocketsAlive
stats.SocketRefs = old.SocketRefs
Expand All @@ -66,6 +67,7 @@ func ResetStats() {
}

type Stats struct {
Clusters int
MasterConns int
SlaveConns int
SentOps int
Expand All @@ -76,6 +78,14 @@ type Stats struct {
SocketRefs int
}

func (stats *Stats) cluster(delta int) {
if stats != nil {
statsMutex.Lock()
stats.Clusters += delta
statsMutex.Unlock()
}
}

func (stats *Stats) conn(delta int, master bool) {
if stats != nil {
statsMutex.Lock()
Expand Down
22 changes: 18 additions & 4 deletions suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,29 @@ func (s *S) TearDownTest(c *C) {
s.Thaw(host)
}
}
for i := 0; i < 20; i++ {
stats := mgo.GetStats()
var stats mgo.Stats
for i := 0; ; i++ {
stats = mgo.GetStats()
if stats.SocketsInUse == 0 && stats.SocketsAlive == 0 {
return
break
}
if i == 20 {
c.Fatal("Test left sockets in a dirty state")
}
c.Logf("Waiting for sockets to die: %d in use, %d alive", stats.SocketsInUse, stats.SocketsAlive)
time.Sleep(500 * time.Millisecond)
}
c.Fatal("Test left sockets in a dirty state")
for i := 0; ; i++ {
stats = mgo.GetStats()
if stats.Clusters == 0 {
break
}
if i == 60 {
c.Fatal("Test left clusters alive")
}
c.Logf("Waiting for clusters to die: %d alive", stats.Clusters)
time.Sleep(1 * time.Second)
}
}

func (s *S) Stop(host string) {
Expand Down

0 comments on commit 19f9c70

Please sign in to comment.