Count every reconnect in reconnect_count (closes #68) #77
+15
-10
@@ -60,17 +60,22 @@ func New() *Tracker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConnected updates the connection status
|
// SetConnected updates the connection status. Every connection after the
|
||||||
|
// first counts one reconnect.
|
||||||
func (t *Tracker) SetConnected(connected bool) {
|
func (t *Tracker) SetConnected(connected bool) {
|
||||||
wasConnected := t.isConnected.Swap(connected)
|
t.isConnected.Store(connected)
|
||||||
if connected {
|
if !connected {
|
||||||
t.mu.Lock()
|
return
|
||||||
t.connectedSince = time.Now()
|
}
|
||||||
t.mu.Unlock()
|
|
||||||
// Increment reconnect count (but not for the initial connection)
|
t.mu.Lock()
|
||||||
if wasConnected || t.reconnectCount.Load() > 0 {
|
// connectedSince is zero until the first connection.
|
||||||
t.reconnectCount.Add(1)
|
connectedBefore := !t.connectedSince.IsZero()
|
||||||
}
|
t.connectedSince = time.Now()
|
||||||
|
t.mu.Unlock()
|
||||||
|
|
||||||
|
if connectedBefore {
|
||||||
|
t.reconnectCount.Add(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package metrics
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// TestReconnectCount connects three times with a disconnect between each, the
|
||||||
|
// way the streamer does, and expects every connection after the first to count
|
||||||
|
// one reconnect.
|
||||||
|
func TestReconnectCount(t *testing.T) {
|
||||||
|
tracker := New()
|
||||||
|
|
||||||
|
tracker.SetConnected(true)
|
||||||
|
tracker.SetConnected(false)
|
||||||
|
tracker.SetConnected(true)
|
||||||
|
tracker.SetConnected(false)
|
||||||
|
tracker.SetConnected(true)
|
||||||
|
|
||||||
|
const wantReconnects = 2
|
||||||
|
if got := tracker.GetReconnectCount(); got != wantReconnects {
|
||||||
|
t.Fatalf("reconnect count = %d, want %d", got, wantReconnects)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user