Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added client.StopTime(), to expose disconnected #400

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ func (cl *Client) StopCause() error {
return cl.State.stopCause.Load().(error)
}

// StopTime returns the the time the client disconnected in unix time, else zero.
func (cl *Client) StopTime() int64 {
return atomic.LoadInt64(&cl.State.disconnected)
}

// Closed returns true if client connection is closed.
func (cl *Client) Closed() bool {
return cl.State.open == nil || cl.State.open.Err() != nil
Expand Down
4 changes: 3 additions & 1 deletion clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,11 @@ func TestClientReadDone(t *testing.T) {

func TestClientStop(t *testing.T) {
cl, _, _ := newTestClient()
require.Equal(t, int64(0), cl.StopTime())
cl.Stop(nil)
require.Equal(t, nil, cl.State.stopCause.Load())
require.Equal(t, time.Now().Unix(), cl.State.disconnected)
require.InDelta(t, time.Now().Unix(), cl.State.disconnected, 1.0)
require.Equal(t, cl.State.disconnected, cl.StopTime())
require.True(t, cl.Closed())
require.Equal(t, nil, cl.StopCause())
}
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ func (s *Server) loadRetained(v []storage.Message) {
// than their given expiry intervals.
func (s *Server) clearExpiredClients(dt int64) {
for id, client := range s.Clients.GetAll() {
disconnected := atomic.LoadInt64(&client.State.disconnected)
disconnected := client.StopTime()
if disconnected == 0 {
continue
}
Expand Down
Loading