Skip to content

Commit cc3f827

Browse files
authored
Added client.StopTime(), to expose disconnected (#400)
* Added client.StopTime() to expose `disconnected` * Added a test of Client.StopTime() I added a check to TestClientStop(), both before and after stopping. I also noticed a race condition in the test (comparing a time against time.Now) and fixed it to allow a one-second discrepancy.
1 parent 5966c7f commit cc3f827

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

clients.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ func (cl *Client) StopCause() error {
417417
return cl.State.stopCause.Load().(error)
418418
}
419419

420+
// StopTime returns the the time the client disconnected in unix time, else zero.
421+
func (cl *Client) StopTime() int64 {
422+
return atomic.LoadInt64(&cl.State.disconnected)
423+
}
424+
420425
// Closed returns true if client connection is closed.
421426
func (cl *Client) Closed() bool {
422427
return cl.State.open == nil || cl.State.open.Err() != nil

clients_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,11 @@ func TestClientReadDone(t *testing.T) {
583583

584584
func TestClientStop(t *testing.T) {
585585
cl, _, _ := newTestClient()
586+
require.Equal(t, int64(0), cl.StopTime())
586587
cl.Stop(nil)
587588
require.Equal(t, nil, cl.State.stopCause.Load())
588-
require.Equal(t, time.Now().Unix(), cl.State.disconnected)
589+
require.InDelta(t, time.Now().Unix(), cl.State.disconnected, 1.0)
590+
require.Equal(t, cl.State.disconnected, cl.StopTime())
589591
require.True(t, cl.Closed())
590592
require.Equal(t, nil, cl.StopCause())
591593
}

server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ func (s *Server) loadRetained(v []storage.Message) {
16891689
// than their given expiry intervals.
16901690
func (s *Server) clearExpiredClients(dt int64) {
16911691
for id, client := range s.Clients.GetAll() {
1692-
disconnected := atomic.LoadInt64(&client.State.disconnected)
1692+
disconnected := client.StopTime()
16931693
if disconnected == 0 {
16941694
continue
16951695
}

0 commit comments

Comments
 (0)