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

Keep in sync server.System.Inflight #92

Merged
merged 2 commits into from
Aug 17, 2022
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
8 changes: 6 additions & 2 deletions server/internal/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,17 @@ func (i *Inflight) Delete(key uint16) bool {
}

// ClearExpired deletes any inflight messages that have remained longer than
// the servers InflightTTL duration.
func (i *Inflight) ClearExpired(expiry int64) {
// the servers InflightTTL duration. Returns number of deleted inflights.
func (i *Inflight) ClearExpired(expiry int64) int64 {
i.Lock()
defer i.Unlock()
var deleted int64
for k, m := range i.internal {
if m.Created < expiry || m.Created == 0 {
delete(i.internal, k)
deleted++
}
}

return deleted
}
3 changes: 2 additions & 1 deletion server/internal/clients/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,13 +916,14 @@ func TestInflightClearExpired(t *testing.T) {

require.Len(t, cl.Inflight.internal, 4)

cl.Inflight.ClearExpired(n - 2)
deleted := cl.Inflight.ClearExpired(n - 2)
cl.Inflight.RLock()
defer cl.Inflight.RUnlock()
require.Len(t, cl.Inflight.internal, 2)
require.Equal(t, (n - 1), cl.Inflight.internal[1].Created)
require.Equal(t, (n - 2), cl.Inflight.internal[2].Created)
require.Equal(t, int64(0), cl.Inflight.internal[3].Created)
require.Equal(t, int64(2), deleted)
}

var (
Expand Down
4 changes: 3 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,8 @@ func (s *Server) clearExpiredInflights(dt int64) {
expiry := dt - s.Options.InflightTTL

for _, client := range s.Clients.GetAll() {
client.Inflight.ClearExpired(expiry)
deleted := client.Inflight.ClearExpired(expiry)
atomic.AddInt64(&s.System.Inflight, deleted*-1)
}

if s.Store != nil {
Expand All @@ -1111,6 +1112,7 @@ func (s *Server) clearExpiredInflights(dt int64) {
func (s *Server) clearAbandonedInflights(cl *clients.Client) {
for i := range cl.Inflight.GetAll() {
cl.Inflight.Delete(i)
atomic.AddInt64(&s.System.Inflight, -1)
}
}

Expand Down
2 changes: 2 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2739,6 +2739,7 @@ func TestServerClearExpiredInflights(t *testing.T) {
require.Len(t, cl.Inflight.GetAll(), 4)
s.clearExpiredInflights(n)
require.Len(t, cl.Inflight.GetAll(), 2)
require.Equal(t, int64(-2), s.System.Inflight)
}

func TestServerClearAbandonedInflights(t *testing.T) {
Expand Down Expand Up @@ -2774,4 +2775,5 @@ func TestServerClearAbandonedInflights(t *testing.T) {
s.clearAbandonedInflights(cl)
require.Len(t, cl.Inflight.GetAll(), 0)
require.Len(t, cl2.Inflight.GetAll(), 2)
require.Equal(t, int64(-2), s.System.Inflight)
}