Skip to content

Commit

Permalink
RateLimitManager gracefully handles individual RateLimiters being clo…
Browse files Browse the repository at this point in the history
…sed already.
  • Loading branch information
Josh Branham committed Mar 1, 2024
1 parent d96ff41 commit 15c87d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 11 additions & 8 deletions pkg/tcpproxy/rate_limit_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tcpproxy

import (
"log/slog"
"sync"
"time"
)
Expand All @@ -9,15 +10,17 @@ import (
type RateLimitManager struct {
defaultCapacity int
defaultFillRate time.Duration
logger *slog.Logger
rateLimiters map[string]*RateLimiter
mutex sync.RWMutex
}

// NewRateLimitManager returns a configured RateLimitManager.
func NewRateLimitManager(capacity int, fillRate time.Duration) *RateLimitManager {
func NewRateLimitManager(capacity int, fillRate time.Duration, logger *slog.Logger) *RateLimitManager {
return &RateLimitManager{
defaultCapacity: capacity,
defaultFillRate: fillRate,
logger: logger,
rateLimiters: make(map[string]*RateLimiter),
mutex: sync.RWMutex{},
}
Expand All @@ -39,16 +42,16 @@ func (r *RateLimitManager) RateLimiterFor(client string) *RateLimiter {
return rateLimiter
}

// Close calls Close() on all known RateLimiters. Calling this multiple times will error if a RateLimiter
// has already been closed.
func (r *RateLimitManager) Close() error {
// Close calls Close() on all known RateLimiters. RateLimiters can only be closed once, however this
// func will handle if a RateLimiter is already closed.
func (r *RateLimitManager) Close() {
r.mutex.RLock()
for _, rateLimiter := range r.rateLimiters {
if err := rateLimiter.Close(); err != nil {
return err
if rateLimiter != nil {
if err := rateLimiter.Close(); err != nil {
r.logger.Warn("error closing rate limiter", "error", err)
}
}
}
r.mutex.RUnlock()

return nil
}
5 changes: 3 additions & 2 deletions pkg/tcpproxy/rate_limit_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package tcpproxy

import (
"log/slog"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestNewRateLimitManager(t *testing.T) {
rlm := NewRateLimitManager(10, 1*time.Millisecond)
rlm := NewRateLimitManager(10, 1*time.Millisecond, slog.Default())

user1 := rlm.RateLimiterFor("user1")
assert.Equal(t, user1, rlm.RateLimiterFor("user1"))

// Ensure we clean up goroutines for the manager and any child RateLimiters
assert.NoError(t, rlm.Close())
rlm.Close()
}

0 comments on commit 15c87d8

Please sign in to comment.