-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add universal 60 sec event counter to livestream (#24380)
- Loading branch information
1 parent
826f5a8
commit 8cecfbf
Showing
6 changed files
with
220 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/golang-lru/v2/expirable" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type Counter struct { | ||
EventCount uint32 | ||
UserCount uint32 | ||
} | ||
|
||
func servedHandler(stats *Stats) func(c echo.Context) error { | ||
return func(c echo.Context) error { | ||
userCount := stats.GlobalStore.Len() | ||
count := stats.Counter.Count() | ||
resp := Counter{ | ||
EventCount: uint32(count), | ||
UserCount: uint32(userCount), | ||
} | ||
return c.JSON(http.StatusOK, resp) | ||
} | ||
} | ||
|
||
func statsHandler(stats *Stats) func(c echo.Context) error { | ||
return func(c echo.Context) error { | ||
|
||
type resp struct { | ||
UsersOnProduct int `json:"users_on_product,omitempty"` | ||
Error string `json:"error,omitempty"` | ||
} | ||
|
||
authHeader := c.Request().Header.Get("Authorization") | ||
if authHeader == "" { | ||
return errors.New("authorization header is required") | ||
} | ||
|
||
claims, err := decodeAuthToken(authHeader) | ||
if err != nil { | ||
return err | ||
} | ||
token := fmt.Sprint(claims["api_token"]) | ||
|
||
var hash *expirable.LRU[string, string] | ||
var ok bool | ||
if hash, ok = stats.Store[token]; !ok { | ||
resp := resp{ | ||
Error: "no stats", | ||
} | ||
return c.JSON(http.StatusNotFound, resp) | ||
} | ||
|
||
siteStats := resp{ | ||
UsersOnProduct: hash.Len(), | ||
} | ||
return c.JSON(http.StatusOK, siteStats) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type SlidingWindowCounter struct { | ||
mu sync.Mutex | ||
events []time.Time | ||
windowSize time.Duration | ||
} | ||
|
||
func NewSlidingWindowCounter(windowSize time.Duration) *SlidingWindowCounter { | ||
return &SlidingWindowCounter{ | ||
events: make([]time.Time, 0), | ||
windowSize: windowSize, | ||
} | ||
} | ||
|
||
func (swc *SlidingWindowCounter) Increment() { | ||
swc.mu.Lock() | ||
defer swc.mu.Unlock() | ||
|
||
now := time.Now() | ||
swc.events = append(swc.events, now) | ||
swc.removeOldEvents(now) | ||
} | ||
|
||
func (swc *SlidingWindowCounter) Count() int { | ||
swc.mu.Lock() | ||
defer swc.mu.Unlock() | ||
|
||
now := time.Now() | ||
swc.removeOldEvents(now) | ||
return len(swc.events) | ||
} | ||
|
||
func (swc *SlidingWindowCounter) removeOldEvents(now time.Time) { | ||
cutoff := now.Add(-swc.windowSize) | ||
i := 0 | ||
for ; i < len(swc.events); i++ { | ||
if swc.events[i].After(cutoff) { | ||
break | ||
} | ||
} | ||
swc.events = swc.events[i:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewSlidingWindowCounter(t *testing.T) { | ||
windowSize := time.Minute | ||
swc := NewSlidingWindowCounter(windowSize) | ||
|
||
assert.Equal(t, windowSize, swc.windowSize, "Window size should match") | ||
assert.Empty(t, swc.events, "Events slice should be empty") | ||
} | ||
|
||
func TestIncrement(t *testing.T) { | ||
swc := NewSlidingWindowCounter(time.Minute) | ||
|
||
swc.Increment() | ||
assert.Equal(t, 1, swc.Count(), "Count should be 1 after first increment") | ||
|
||
swc.Increment() | ||
assert.Equal(t, 2, swc.Count(), "Count should be 2 after second increment") | ||
} | ||
|
||
func TestCount(t *testing.T) { | ||
swc := NewSlidingWindowCounter(time.Second) | ||
|
||
swc.Increment() | ||
time.Sleep(500 * time.Millisecond) | ||
swc.Increment() | ||
|
||
assert.Equal(t, 2, swc.Count(), "Count should be 2 within the time window") | ||
|
||
time.Sleep(600 * time.Millisecond) | ||
|
||
assert.Equal(t, 1, swc.Count(), "Count should be 1 after oldest event expires") | ||
} | ||
|
||
func TestRemoveOldEvents(t *testing.T) { | ||
swc := NewSlidingWindowCounter(time.Second) | ||
|
||
now := time.Now() | ||
swc.events = []time.Time{ | ||
now.Add(-2 * time.Second), | ||
now.Add(-1500 * time.Millisecond), | ||
now.Add(-500 * time.Millisecond), | ||
now, | ||
} | ||
|
||
swc.removeOldEvents(now) | ||
|
||
require.Len(t, swc.events, 2, "Should have 2 events after removal") | ||
assert.Equal(t, now.Add(-500*time.Millisecond), swc.events[0], "First event should be 500ms ago") | ||
assert.Equal(t, now, swc.events[1], "Second event should be now") | ||
} | ||
|
||
func TestConcurrency(t *testing.T) { | ||
swc := NewSlidingWindowCounter(time.Minute) | ||
iterations := 1000 | ||
var wg sync.WaitGroup | ||
|
||
wg.Add(iterations) | ||
for i := 0; i < iterations; i++ { | ||
go func() { | ||
defer wg.Done() | ||
swc.Increment() | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
|
||
assert.Equal(t, iterations, swc.Count(), "Count should match the number of increments") | ||
} |