Skip to content
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
4 changes: 2 additions & 2 deletions services/rest/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func reportConnectionMetrics(conn connection.Table) {
t := time.Tick(config.MetricStatsd.FlushPeriodMs)
for {
<-t
for k, v := range conn.TotalConnectionPerGroup() {
conn.RangeConnectionPerGroup(func(k string, v int) {
metrics.Gauge("connections_count_current", v, fmt.Sprintf("conn_group=%s", k))
}
})
}
}

Expand Down
8 changes: 6 additions & 2 deletions services/rest/websocket/connection/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (t *Table) TotalConnection() int {
return len(t.connMap)
}

func (t *Table) TotalConnectionPerGroup() map[string]int {
return t.counter
func (t *Table) RangeConnectionPerGroup(fn func(string, int)) {
t.m.RLock()
defer t.m.RUnlock()
for k, v := range t.counter {
fn(k, v)
}
}
35 changes: 23 additions & 12 deletions services/rest/websocket/connection/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConnectionPerGroup(t *testing.T) {
t.Run("Should return all the group on the table with the count", func(t *testing.T) {
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: "group1"})
table.Store(identification.Identifier{ID: "user2", Group: "group1"})
table.Store(identification.Identifier{ID: "user3", Group: "group1"})
table.Store(identification.Identifier{ID: "user1", Group: "group2"})
table.Store(identification.Identifier{ID: "user2", Group: "group2"})
assert.Equal(t, map[string]int{"group1": 3, "group2": 2}, table.TotalConnectionPerGroup())
})
}

func TestStore(t *testing.T) {
t.Run("Should store new connection", func(t *testing.T) {
table := NewTable(10)
Expand Down Expand Up @@ -146,6 +134,29 @@ func TestStoreBatch(t *testing.T) {
})
}

func TestRangeConnectionPerGroup(t *testing.T) {
t.Run("Should iterate over all the group on the table with the count", func(t *testing.T) {
//set up the table data
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: "group1"})
table.Store(identification.Identifier{ID: "user2", Group: "group1"})
table.Store(identification.Identifier{ID: "user3", Group: "group1"})
table.Store(identification.Identifier{ID: "user1", Group: "group2"})
table.Store(identification.Identifier{ID: "user2", Group: "group2"})
// Track callback calls
called := make(map[string]int)
table.RangeConnectionPerGroup(func(k string, v int) {
called[k] = v
})
// Verify all keys are present
assert.Equal(t, len(table.counter), len(called))
// Verify values match
for k, v := range table.counter {
assert.Equal(t, v, called[k])
}
})
}

func BenchmarkStoreBatch(b *testing.B) {
table := NewTable(b.N)
for i := 0; i < b.N; i++ {
Expand Down