From 26dc0d5db947e85c3102347bcdbb679734fbb48b Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 11:33:41 +0300 Subject: [PATCH 01/16] feat(entities): add RateLimit() method to SubscriptionName - Returns subscription.Limit() * 2 for daily API request budgets - Add 4 test cases covering Free, Pro, Ultra, and 200K subscriptions - RateLimit(): Free=400, Pro=10000, Ultra=20000, 200K=400000 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/entities/user.go | 5 +++++ api/pkg/entities/user_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/api/pkg/entities/user.go b/api/pkg/entities/user.go index 6cf2eba93..94fda428c 100644 --- a/api/pkg/entities/user.go +++ b/api/pkg/entities/user.go @@ -37,6 +37,11 @@ func (subscription SubscriptionName) Limit() uint { } } +// RateLimit returns the daily API request rate limit for a subscription +func (subscription SubscriptionName) RateLimit() uint { + return subscription.Limit() * 2 +} + // SubscriptionNameFree represents a free subscription const SubscriptionNameFree = SubscriptionName("free") diff --git a/api/pkg/entities/user_test.go b/api/pkg/entities/user_test.go index 0417e63fe..63dcf3a61 100644 --- a/api/pkg/entities/user_test.go +++ b/api/pkg/entities/user_test.go @@ -51,3 +51,19 @@ func TestUser_GetBillingAnchorDay_PaidUserDay31(t *testing.T) { } assert.Equal(t, 31, user.GetBillingAnchorDay()) } + +func TestSubscriptionName_RateLimit_Free(t *testing.T) { + assert.Equal(t, uint(400), SubscriptionNameFree.RateLimit()) +} + +func TestSubscriptionName_RateLimit_Pro(t *testing.T) { + assert.Equal(t, uint(10000), SubscriptionNameProMonthly.RateLimit()) +} + +func TestSubscriptionName_RateLimit_Ultra(t *testing.T) { + assert.Equal(t, uint(20000), SubscriptionNameUltraMonthly.RateLimit()) +} + +func TestSubscriptionName_RateLimit_200K(t *testing.T) { + assert.Equal(t, uint(400000), SubscriptionName200KMonthly.RateLimit()) +} From c7c019126c99264848f62a8275b994f854fa6441 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 11:35:25 +0300 Subject: [PATCH 02/16] feat(events): add rate.limit.exceeded CloudEvent type Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/events/rate_limit_exceeded_event.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 api/pkg/events/rate_limit_exceeded_event.go diff --git a/api/pkg/events/rate_limit_exceeded_event.go b/api/pkg/events/rate_limit_exceeded_event.go new file mode 100644 index 000000000..d27f1731a --- /dev/null +++ b/api/pkg/events/rate_limit_exceeded_event.go @@ -0,0 +1,19 @@ +package events + +import ( + "time" + + "github.com/NdoleStudio/httpsms/pkg/entities" +) + +// RateLimitExceeded is raised when a user exceeds their daily API rate limit. +const RateLimitExceeded = "rate.limit.exceeded" + +// RateLimitExceededPayload stores the data for the RateLimitExceeded event +type RateLimitExceededPayload struct { + UserID entities.UserID `json:"user_id"` + Count int64 `json:"count"` + Limit uint `json:"limit"` + Plan string `json:"plan"` + Timestamp time.Time `json:"timestamp"` +} From 0bd0a31f05c38a1d269006786a1161fec7e57a8c Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 11:40:44 +0300 Subject: [PATCH 03/16] feat(services): implement RateLimitService with in-memory counters and Redis sync - Add RateLimitService with per-user API request tracking - Implement in-memory counters with 24h sliding windows - Add background flush to Redis every 30s with pipelining - Emit RateLimitExceeded events when limits are exceeded - Support graceful nil handling for tracer, logger, client, dispatcher - Add comprehensive tests for basic counting, weighted costs, limit enforcement, window expiry Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/services/rate_limit_service.go | 252 ++++++++++++++++++++ api/pkg/services/rate_limit_service_test.go | 111 +++++++++ 2 files changed, 363 insertions(+) create mode 100644 api/pkg/services/rate_limit_service.go create mode 100644 api/pkg/services/rate_limit_service_test.go diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go new file mode 100644 index 000000000..3498c3cbd --- /dev/null +++ b/api/pkg/services/rate_limit_service.go @@ -0,0 +1,252 @@ +package services + +import ( + "context" + "fmt" + "strconv" + "sync" + "time" + + "github.com/NdoleStudio/httpsms/pkg/entities" + "github.com/NdoleStudio/httpsms/pkg/events" + "github.com/NdoleStudio/httpsms/pkg/telemetry" + "github.com/palantir/stacktrace" + "github.com/redis/go-redis/v9" +) + +const ( + rateLimitKeyPrefix = "rate_limit:" + rateLimitNotifiedPrefix = "rate_limit_notified:" + rateLimitWindow = 24 * time.Hour + rateLimitFlushInterval = 30 * time.Second +) + +// RateLimitService tracks per-user API request counts with in-memory counters +// and periodic Redis sync. +type RateLimitService struct { + service + tracer telemetry.Tracer + logger telemetry.Logger + client *redis.Client + dispatcher *EventDispatcher + + mu sync.Mutex + counters map[string]*userCounter + notified map[string]bool + done chan struct{} +} + +type userCounter struct { + count int64 + windowExpiry time.Time + dirty int64 +} + +// NewRateLimitService creates a new RateLimitService and starts the background flush goroutine. +func NewRateLimitService( + tracer telemetry.Tracer, + logger telemetry.Logger, + client *redis.Client, + dispatcher *EventDispatcher, +) *RateLimitService { + svc := &RateLimitService{ + tracer: tracer, + logger: logger, + client: client, + dispatcher: dispatcher, + counters: make(map[string]*userCounter), + notified: make(map[string]bool), + done: make(chan struct{}), + } + + go svc.flushLoop() + return svc +} + +// Increment adds cost to the user's counter and returns the current count. +// If the count exceeds the plan's rate limit, exceeded is true. +func (svc *RateLimitService) Increment(ctx context.Context, userID entities.UserID, plan entities.SubscriptionName, cost int) (count int64, exceeded bool, err error) { + svc.mu.Lock() + defer svc.mu.Unlock() + + key := string(userID) + counter, exists := svc.counters[key] + + if !exists { + counter, err = svc.hydrate(ctx, key) + if err != nil { + counter = &userCounter{ + count: 0, + windowExpiry: time.Now().Add(rateLimitWindow), + dirty: 0, + } + } + svc.counters[key] = counter + } + + // Reset if window has expired + if time.Now().After(counter.windowExpiry) { + counter.count = 0 + counter.dirty = 0 + counter.windowExpiry = time.Now().Add(rateLimitWindow) + svc.notified[key] = false + } + + counter.count += int64(cost) + counter.dirty += int64(cost) + + limit := plan.RateLimit() + exceeded = counter.count > int64(limit) + + if exceeded && !svc.notified[key] { + svc.notified[key] = true + go svc.emitExceededEvent(ctx, userID, counter.count, limit, plan) + } + + return counter.count, exceeded, nil +} + +// Close flushes remaining dirty counters and stops the background goroutine. +func (svc *RateLimitService) Close() { + close(svc.done) + if svc.client != nil { + svc.flush(context.Background()) + } +} + +func (svc *RateLimitService) flushLoop() { + ticker := time.NewTicker(rateLimitFlushInterval) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + if svc.client != nil { + svc.flush(context.Background()) + } + case <-svc.done: + return + } + } +} + +func (svc *RateLimitService) flush(ctx context.Context) { + svc.mu.Lock() + // Collect dirty entries + type flushEntry struct { + key string + delta int64 + ttl time.Duration + } + var entries []flushEntry + now := time.Now() + + for key, counter := range svc.counters { + // Clean up expired windows + if now.After(counter.windowExpiry) { + delete(svc.counters, key) + delete(svc.notified, key) + continue + } + if counter.dirty > 0 { + entries = append(entries, flushEntry{ + key: rateLimitKeyPrefix + key, + delta: counter.dirty, + ttl: time.Until(counter.windowExpiry), + }) + counter.dirty = 0 + } + } + svc.mu.Unlock() + + // Flush to Redis outside the lock + for _, entry := range entries { + pipe := svc.client.Pipeline() + pipe.IncrBy(ctx, entry.key, entry.delta) + pipe.ExpireNX(ctx, entry.key, entry.ttl) + if _, err := pipe.Exec(ctx); err != nil { + if svc.logger != nil { + svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit for key [%s]", entry.key))) + } + } + } +} + +func (svc *RateLimitService) hydrate(ctx context.Context, userID string) (*userCounter, error) { + if svc.client == nil { + return &userCounter{ + count: 0, + windowExpiry: time.Now().Add(rateLimitWindow), + dirty: 0, + }, nil + } + + key := rateLimitKeyPrefix + userID + countStr, err := svc.client.Get(ctx, key).Result() + if err == redis.Nil { + return &userCounter{ + count: 0, + windowExpiry: time.Now().Add(rateLimitWindow), + dirty: 0, + }, nil + } + if err != nil { + return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot hydrate rate limit for user [%s]", userID)) + } + + count, err := strconv.ParseInt(countStr, 10, 64) + if err != nil { + return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse rate limit count [%s] for user [%s]", countStr, userID)) + } + + ttl, err := svc.client.TTL(ctx, key).Result() + if err != nil || ttl <= 0 { + ttl = rateLimitWindow + } + + // Check if already notified (in Redis) + notifiedKey := rateLimitNotifiedPrefix + userID + if exists, _ := svc.client.Exists(ctx, notifiedKey).Result(); exists > 0 { + svc.notified[userID] = true + } + + return &userCounter{ + count: count, + windowExpiry: time.Now().Add(ttl), + dirty: 0, + }, nil +} + +func (svc *RateLimitService) emitExceededEvent(ctx context.Context, userID entities.UserID, count int64, limit uint, plan entities.SubscriptionName) { + if svc.dispatcher == nil { + return + } + + // Set notified flag in Redis (24h TTL) to survive restarts + if svc.client != nil { + notifiedKey := rateLimitNotifiedPrefix + string(userID) + svc.client.Set(ctx, notifiedKey, "1", rateLimitWindow) + } + + payload := events.RateLimitExceededPayload{ + UserID: userID, + Count: count, + Limit: limit, + Plan: string(plan), + Timestamp: time.Now().UTC(), + } + + event, err := svc.createEvent(events.RateLimitExceeded, string(userID), payload) + if err != nil { + if svc.logger != nil { + svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot create rate limit exceeded event for user [%s]", userID))) + } + return + } + + if err = svc.dispatcher.Dispatch(ctx, event); err != nil { + if svc.logger != nil { + svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot dispatch rate limit exceeded event for user [%s]", userID))) + } + } +} diff --git a/api/pkg/services/rate_limit_service_test.go b/api/pkg/services/rate_limit_service_test.go new file mode 100644 index 000000000..96030a0df --- /dev/null +++ b/api/pkg/services/rate_limit_service_test.go @@ -0,0 +1,111 @@ +package services + +import ( + "context" + "testing" + "time" + + "github.com/NdoleStudio/httpsms/pkg/entities" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRateLimitService_Increment_BasicCount(t *testing.T) { + // Arrange + svc := newTestRateLimitService(t) + defer svc.Close() + + ctx := context.Background() + + // Act + count, exceeded, err := svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 1) + + // Assert + require.NoError(t, err) + assert.Equal(t, int64(1), count) + assert.False(t, exceeded) +} + +func TestRateLimitService_Increment_WeightedCost(t *testing.T) { + // Arrange + svc := newTestRateLimitService(t) + defer svc.Close() + + ctx := context.Background() + + // Act + count, _, err := svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 10) + + // Assert + require.NoError(t, err) + assert.Equal(t, int64(10), count) +} + +func TestRateLimitService_Increment_ExceedsLimit(t *testing.T) { + // Arrange + svc := newTestRateLimitService(t) + defer svc.Close() + + ctx := context.Background() + + // Free plan limit is 400. Exceed it. + for i := 0; i < 400; i++ { + _, _, _ = svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 1) + } + + // Act — this pushes count to 401 + count, exceeded, err := svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 1) + + // Assert + require.NoError(t, err) + assert.Equal(t, int64(401), count) + assert.True(t, exceeded) +} + +func TestRateLimitService_Increment_MultipleUsers(t *testing.T) { + // Arrange + svc := newTestRateLimitService(t) + defer svc.Close() + + ctx := context.Background() + + // Act + _, _, _ = svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 5) + count, _, err := svc.Increment(ctx, "user-2", entities.SubscriptionNameProMonthly, 3) + + // Assert + require.NoError(t, err) + assert.Equal(t, int64(3), count) +} + +func TestRateLimitService_Increment_WindowExpiry(t *testing.T) { + // Arrange + svc := newTestRateLimitService(t) + defer svc.Close() + + ctx := context.Background() + + // Simulate an existing counter with an expired window + svc.mu.Lock() + svc.counters["user-1"] = &userCounter{ + count: 500, + windowExpiry: time.Now().Add(-1 * time.Hour), // expired + dirty: 0, + } + svc.mu.Unlock() + + // Act — should reset because the window expired + count, exceeded, err := svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 1) + + // Assert + require.NoError(t, err) + assert.Equal(t, int64(1), count) + assert.False(t, exceeded) +} + +// newTestRateLimitService creates a RateLimitService with nil redis client (no hydration) +// suitable for unit tests that only test in-memory logic. +func newTestRateLimitService(t *testing.T) *RateLimitService { + t.Helper() + return NewRateLimitService(nil, nil, nil, nil) +} From 546058387d74b6068b94e50d3ec51795987a35a6 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 11:44:15 +0300 Subject: [PATCH 04/16] feat(middlewares): add rate limit middleware for tracking API usage Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/middlewares/rate_limit_middleware.go | 71 ++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 api/pkg/middlewares/rate_limit_middleware.go diff --git a/api/pkg/middlewares/rate_limit_middleware.go b/api/pkg/middlewares/rate_limit_middleware.go new file mode 100644 index 000000000..4c58dbb56 --- /dev/null +++ b/api/pkg/middlewares/rate_limit_middleware.go @@ -0,0 +1,71 @@ +package middlewares + +import ( + "os" + "strconv" + "strings" + + "github.com/NdoleStudio/httpsms/pkg/entities" + "github.com/NdoleStudio/httpsms/pkg/repositories" + "github.com/NdoleStudio/httpsms/pkg/services" + "github.com/NdoleStudio/httpsms/pkg/telemetry" + "github.com/gofiber/fiber/v3" +) + +const rateLimitCostCap = 200 + +// RateLimit tracks per-user API request counts without blocking requests. +func RateLimit( + tracer telemetry.Tracer, + logger telemetry.Logger, + service *services.RateLimitService, + userRepository repositories.UserRepository, + excludePaths []string, +) fiber.Handler { + enabled := os.Getenv("RATE_LIMIT_ENABLED") == "true" + logger = logger.WithService("middlewares.RateLimit") + + return func(c fiber.Ctx) error { + if !enabled { + return c.Next() + } + + path := c.Path() + for _, excluded := range excludePaths { + if strings.HasPrefix(path, excluded) { + return c.Next() + } + } + + ctx, span := tracer.StartFromFiberCtx(c, "middlewares.RateLimit") + defer span.End() + + authUser, ok := c.Locals(ContextKeyAuthUserID).(entities.AuthContext) + if !ok || authUser.IsNoop() { + return c.Next() + } + + cost := 1 + if c.Method() == fiber.MethodGet { + if limitParam := c.Query("limit"); limitParam != "" { + if parsed, err := strconv.Atoi(limitParam); err == nil && parsed > 0 { + cost = parsed + if cost > rateLimitCostCap { + cost = rateLimitCostCap + } + } + } + } + + user, err := userRepository.Load(ctx, authUser.ID) + if err != nil { + ctxLogger := tracer.CtxLogger(logger, span) + ctxLogger.Error(err) + return c.Next() + } + + _, _, _ = service.Increment(ctx, authUser.ID, user.SubscriptionName, cost) + + return c.Next() + } +} From 59ef5e03b961045670b72daceb05df6025b65842 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 11:46:24 +0300 Subject: [PATCH 05/16] feat(di): wire rate limit service and middleware into DI container - Add rateLimitService and redisClient fields to Container struct - Extract RedisClient() method for shared Redis client creation - Refactor Cache() to reuse RedisClient() singleton - Add RateLimitService() lazy singleton initialization - Register RateLimit middleware in App() after API key auth - Add RATE_LIMIT_ENABLED=false to .env.docker Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/.env.docker | 3 +++ api/pkg/di/container.go | 43 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/api/.env.docker b/api/.env.docker index 235972f4d..d7820d82e 100644 --- a/api/.env.docker +++ b/api/.env.docker @@ -52,6 +52,9 @@ DATABASE_URL_DEDICATED=postgresql://dbusername:dbpassword@postgres:5432/httpsms # Redis connection string REDIS_URL=redis://@redis:6379 +# Rate limiting (set to "true" to enable per-user API rate tracking) +RATE_LIMIT_ENABLED=false + # Google Cloud Storage bucket for MMS attachments. Leave empty to use in-memory storage. GCS_BUCKET_NAME= diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go index e7ca20d28..c53dd072f 100644 --- a/api/pkg/di/container.go +++ b/api/pkg/di/container.go @@ -98,6 +98,8 @@ type Container struct { phoneRistrettoCache *ristretto.Cache[string, *entities.Phone] contactRistrettoCache *ristretto.Cache[string, services.ContactCacheEntry] inMemoryCache cache.Cache + rateLimitService *services.RateLimitService + redisClient *redis.Client } // NewLiteContainer creates a Container without any routes or listeners @@ -208,6 +210,13 @@ func (container *Container) App() (app *fiber.App) { app.Use(middlewares.HTTPRequestLogger(container.Tracer(), container.Logger())) app.Use(middlewares.BearerAuth(container.Logger(), container.Tracer(), container.FirebaseAuthClient())) app.Use(middlewares.APIKeyAuth(container.Logger(), container.Tracer(), container.UserRepository())) + app.Use(middlewares.RateLimit( + container.Tracer(), + container.Logger(), + container.RateLimitService(), + container.UserRepository(), + []string{"/v1/events"}, + )) container.app = app return app @@ -451,6 +460,16 @@ func (container *Container) InMemoryCache() cache.Cache { // Cache creates a new instance of cache.Cache func (container *Container) Cache() cache.Cache { container.logger.Debug("creating cache.Cache") + return cache.NewRedisCache(container.Tracer(), container.RedisClient()) +} + +// RedisClient creates or returns the shared *redis.Client +func (container *Container) RedisClient() *redis.Client { + if container.redisClient != nil { + return container.redisClient + } + + container.logger.Debug("creating *redis.Client") opt, err := redis.ParseURL(os.Getenv("REDIS_URL")) if err != nil { container.logger.Fatal(stacktrace.Propagatef(err, "cannot parse redis url [%s]", os.Getenv("REDIS_URL"))) @@ -461,19 +480,35 @@ func (container *Container) Cache() cache.Cache { } } - redisClient := redis.NewClient(opt) + container.redisClient = redis.NewClient(opt) // Enable tracing instrumentation. - if err = redisotel.InstrumentTracing(redisClient); err != nil { + if err = redisotel.InstrumentTracing(container.redisClient); err != nil { container.logger.Error(stacktrace.Propagatef(err, "cannot instrument redis tracing")) } // Enable metrics instrumentation. - if err = redisotel.InstrumentMetrics(redisClient); err != nil { + if err = redisotel.InstrumentMetrics(container.redisClient); err != nil { container.logger.Fatal(stacktrace.Propagatef(err, "cannot instrument redis metrics")) } - return cache.NewRedisCache(container.Tracer(), redisClient) + return container.redisClient +} + +// RateLimitService creates or returns the shared *services.RateLimitService +func (container *Container) RateLimitService() *services.RateLimitService { + if container.rateLimitService != nil { + return container.rateLimitService + } + + container.logger.Debug("creating services.RateLimitService") + container.rateLimitService = services.NewRateLimitService( + container.Tracer(), + container.Logger(), + container.RedisClient(), + container.EventDispatcher(), + ) + return container.rateLimitService } // FirebaseAuthClient creates a new instance of auth.Client From 27455a233fea24e4395dbab51f8fe1f19dfefbf7 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 11:56:01 +0300 Subject: [PATCH 06/16] fix: add graceful shutdown for RateLimitService and handle Redis notification errors - Add Container.Close() to flush rate limit counters on shutdown - Call defer container.Close() in main.go - Check and log errors from Redis notification flag persistence Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/main.go | 1 + api/pkg/di/container.go | 7 +++++++ api/pkg/services/rate_limit_service.go | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/api/main.go b/api/main.go index 5b7539c9c..9b2886fea 100644 --- a/api/main.go +++ b/api/main.go @@ -42,5 +42,6 @@ func main() { } container := di.NewContainer(os.Getenv("GCP_PROJECT_ID"), Version) + defer container.Close() container.Logger().Info(container.App().Listen(fmt.Sprintf("%s:%s", os.Getenv("APP_HOST"), os.Getenv("APP_PORT"))).Error()) } diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go index c53dd072f..e0132c38d 100644 --- a/api/pkg/di/container.go +++ b/api/pkg/di/container.go @@ -109,6 +109,13 @@ func NewLiteContainer() (container *Container) { } } +// Close gracefully shuts down container resources +func (container *Container) Close() { + if container.rateLimitService != nil { + container.rateLimitService.Close() + } +} + // NewContainer creates a new dependency injection container func NewContainer(projectID string, version string) (container *Container) { container = &Container{ diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go index 3498c3cbd..0d6b9b933 100644 --- a/api/pkg/services/rate_limit_service.go +++ b/api/pkg/services/rate_limit_service.go @@ -225,7 +225,9 @@ func (svc *RateLimitService) emitExceededEvent(ctx context.Context, userID entit // Set notified flag in Redis (24h TTL) to survive restarts if svc.client != nil { notifiedKey := rateLimitNotifiedPrefix + string(userID) - svc.client.Set(ctx, notifiedKey, "1", rateLimitWindow) + if err := svc.client.Set(ctx, notifiedKey, "1", rateLimitWindow).Err(); err != nil && svc.logger != nil { + svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot persist rate limit notified flag for user [%s]", userID))) + } } payload := events.RateLimitExceededPayload{ From 3a47dea9697a365409010a13bb1894c0f159b589 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 12:03:29 +0300 Subject: [PATCH 07/16] feat(services): add 1s Redis timeout, fail-open behavior, and shutdown logging - All Redis calls in RateLimitService use 1s context timeout - If Redis is slow/down, rate limiting continues in-memory (fail open) - Added logging to flush loop start/stop and graceful shutdown Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/services/rate_limit_service.go | 38 ++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go index 0d6b9b933..b9d12838a 100644 --- a/api/pkg/services/rate_limit_service.go +++ b/api/pkg/services/rate_limit_service.go @@ -19,6 +19,7 @@ const ( rateLimitNotifiedPrefix = "rate_limit_notified:" rateLimitWindow = 24 * time.Hour rateLimitFlushInterval = 30 * time.Second + rateLimitRedisTimeout = 1 * time.Second ) // RateLimitService tracks per-user API request counts with in-memory counters @@ -108,16 +109,26 @@ func (svc *RateLimitService) Increment(ctx context.Context, userID entities.User // Close flushes remaining dirty counters and stops the background goroutine. func (svc *RateLimitService) Close() { + if svc.logger != nil { + svc.logger.Info("RateLimitService shutting down, flushing counters") + } close(svc.done) if svc.client != nil { svc.flush(context.Background()) } + if svc.logger != nil { + svc.logger.Info("RateLimitService shutdown complete") + } } func (svc *RateLimitService) flushLoop() { ticker := time.NewTicker(rateLimitFlushInterval) defer ticker.Stop() + if svc.logger != nil { + svc.logger.Info(fmt.Sprintf("RateLimitService flush loop started (interval: %s)", rateLimitFlushInterval)) + } + for { select { case <-ticker.C: @@ -125,6 +136,9 @@ func (svc *RateLimitService) flushLoop() { svc.flush(context.Background()) } case <-svc.done: + if svc.logger != nil { + svc.logger.Info("RateLimitService flush loop stopped") + } return } } @@ -159,12 +173,15 @@ func (svc *RateLimitService) flush(ctx context.Context) { } svc.mu.Unlock() - // Flush to Redis outside the lock + // Flush to Redis outside the lock with timeout + redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) + defer cancel() + for _, entry := range entries { pipe := svc.client.Pipeline() - pipe.IncrBy(ctx, entry.key, entry.delta) - pipe.ExpireNX(ctx, entry.key, entry.ttl) - if _, err := pipe.Exec(ctx); err != nil { + pipe.IncrBy(redisCtx, entry.key, entry.delta) + pipe.ExpireNX(redisCtx, entry.key, entry.ttl) + if _, err := pipe.Exec(redisCtx); err != nil { if svc.logger != nil { svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit for key [%s]", entry.key))) } @@ -181,8 +198,11 @@ func (svc *RateLimitService) hydrate(ctx context.Context, userID string) (*userC }, nil } + redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) + defer cancel() + key := rateLimitKeyPrefix + userID - countStr, err := svc.client.Get(ctx, key).Result() + countStr, err := svc.client.Get(redisCtx, key).Result() if err == redis.Nil { return &userCounter{ count: 0, @@ -199,14 +219,14 @@ func (svc *RateLimitService) hydrate(ctx context.Context, userID string) (*userC return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse rate limit count [%s] for user [%s]", countStr, userID)) } - ttl, err := svc.client.TTL(ctx, key).Result() + ttl, err := svc.client.TTL(redisCtx, key).Result() if err != nil || ttl <= 0 { ttl = rateLimitWindow } // Check if already notified (in Redis) notifiedKey := rateLimitNotifiedPrefix + userID - if exists, _ := svc.client.Exists(ctx, notifiedKey).Result(); exists > 0 { + if exists, _ := svc.client.Exists(redisCtx, notifiedKey).Result(); exists > 0 { svc.notified[userID] = true } @@ -224,8 +244,10 @@ func (svc *RateLimitService) emitExceededEvent(ctx context.Context, userID entit // Set notified flag in Redis (24h TTL) to survive restarts if svc.client != nil { + redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) + defer cancel() notifiedKey := rateLimitNotifiedPrefix + string(userID) - if err := svc.client.Set(ctx, notifiedKey, "1", rateLimitWindow).Err(); err != nil && svc.logger != nil { + if err := svc.client.Set(redisCtx, notifiedKey, "1", rateLimitWindow).Err(); err != nil && svc.logger != nil { svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot persist rate limit notified flag for user [%s]", userID))) } } From cc89b65cc65c9e6610f431309d255a0c95b7fd08 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 12:10:41 +0300 Subject: [PATCH 08/16] refactor: use Fiber OnPreShutdown hook for RateLimitService cleanup Replace defer container.Close() in main.go with app.Hooks().OnPreShutdown() which is the idiomatic Fiber v3 approach for graceful shutdown. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/main.go | 1 - api/pkg/di/container.go | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/api/main.go b/api/main.go index 9b2886fea..5b7539c9c 100644 --- a/api/main.go +++ b/api/main.go @@ -42,6 +42,5 @@ func main() { } container := di.NewContainer(os.Getenv("GCP_PROJECT_ID"), Version) - defer container.Close() container.Logger().Info(container.App().Listen(fmt.Sprintf("%s:%s", os.Getenv("APP_HOST"), os.Getenv("APP_PORT"))).Error()) } diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go index e0132c38d..2cf2133fe 100644 --- a/api/pkg/di/container.go +++ b/api/pkg/di/container.go @@ -225,6 +225,11 @@ func (container *Container) App() (app *fiber.App) { []string{"/v1/events"}, )) + app.Hooks().OnPreShutdown(func() error { + container.RateLimitService().Close() + return nil + }) + container.app = app return app } From d270183328df8bd10cbeb155bfef1c8fd2a32dd7 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 12:13:55 +0300 Subject: [PATCH 09/16] refactor: remove nil checks for logger in RateLimitService, use OnPreShutdown - Logger is always injected via DI, nil checks are unnecessary - Use Fiber's app.Hooks().OnPreShutdown() for graceful shutdown - Remove defer container.Close() from main.go Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/services/rate_limit_service.go | 104 +++++++++----------- api/pkg/services/rate_limit_service_test.go | 17 +++- 2 files changed, 61 insertions(+), 60 deletions(-) diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go index b9d12838a..268e946af 100644 --- a/api/pkg/services/rate_limit_service.go +++ b/api/pkg/services/rate_limit_service.go @@ -66,15 +66,15 @@ func NewRateLimitService( // Increment adds cost to the user's counter and returns the current count. // If the count exceeds the plan's rate limit, exceeded is true. -func (svc *RateLimitService) Increment(ctx context.Context, userID entities.UserID, plan entities.SubscriptionName, cost int) (count int64, exceeded bool, err error) { - svc.mu.Lock() - defer svc.mu.Unlock() +func (service *RateLimitService) Increment(ctx context.Context, userID entities.UserID, plan entities.SubscriptionName, cost int) (count int64, exceeded bool, err error) { + service.mu.Lock() + defer service.mu.Unlock() key := string(userID) - counter, exists := svc.counters[key] + counter, exists := service.counters[key] if !exists { - counter, err = svc.hydrate(ctx, key) + counter, err = service.hydrate(ctx, key) if err != nil { counter = &userCounter{ count: 0, @@ -82,7 +82,7 @@ func (svc *RateLimitService) Increment(ctx context.Context, userID entities.User dirty: 0, } } - svc.counters[key] = counter + service.counters[key] = counter } // Reset if window has expired @@ -90,7 +90,7 @@ func (svc *RateLimitService) Increment(ctx context.Context, userID entities.User counter.count = 0 counter.dirty = 0 counter.windowExpiry = time.Now().Add(rateLimitWindow) - svc.notified[key] = false + service.notified[key] = false } counter.count += int64(cost) @@ -99,53 +99,45 @@ func (svc *RateLimitService) Increment(ctx context.Context, userID entities.User limit := plan.RateLimit() exceeded = counter.count > int64(limit) - if exceeded && !svc.notified[key] { - svc.notified[key] = true - go svc.emitExceededEvent(ctx, userID, counter.count, limit, plan) + if exceeded && !service.notified[key] { + service.notified[key] = true + go service.emitExceededEvent(ctx, userID, counter.count, limit, plan) } return counter.count, exceeded, nil } // Close flushes remaining dirty counters and stops the background goroutine. -func (svc *RateLimitService) Close() { - if svc.logger != nil { - svc.logger.Info("RateLimitService shutting down, flushing counters") - } - close(svc.done) - if svc.client != nil { - svc.flush(context.Background()) - } - if svc.logger != nil { - svc.logger.Info("RateLimitService shutdown complete") +func (service *RateLimitService) Close() { + service.logger.Info("RateLimitService shutting down, flushing counters") + close(service.done) + if service.client != nil { + service.flush(context.Background()) } + service.logger.Info("RateLimitService shutdown complete") } -func (svc *RateLimitService) flushLoop() { +func (service *RateLimitService) flushLoop() { ticker := time.NewTicker(rateLimitFlushInterval) defer ticker.Stop() - if svc.logger != nil { - svc.logger.Info(fmt.Sprintf("RateLimitService flush loop started (interval: %s)", rateLimitFlushInterval)) - } + service.logger.Info(fmt.Sprintf("RateLimitService flush loop started (interval: %s)", rateLimitFlushInterval)) for { select { case <-ticker.C: - if svc.client != nil { - svc.flush(context.Background()) - } - case <-svc.done: - if svc.logger != nil { - svc.logger.Info("RateLimitService flush loop stopped") + if service.client != nil { + service.flush(context.Background()) } + case <-service.done: + service.logger.Info("RateLimitService flush loop stopped") return } } } -func (svc *RateLimitService) flush(ctx context.Context) { - svc.mu.Lock() +func (service *RateLimitService) flush(ctx context.Context) { + service.mu.Lock() // Collect dirty entries type flushEntry struct { key string @@ -155,11 +147,11 @@ func (svc *RateLimitService) flush(ctx context.Context) { var entries []flushEntry now := time.Now() - for key, counter := range svc.counters { + for key, counter := range service.counters { // Clean up expired windows if now.After(counter.windowExpiry) { - delete(svc.counters, key) - delete(svc.notified, key) + delete(service.counters, key) + delete(service.notified, key) continue } if counter.dirty > 0 { @@ -171,26 +163,24 @@ func (svc *RateLimitService) flush(ctx context.Context) { counter.dirty = 0 } } - svc.mu.Unlock() + service.mu.Unlock() // Flush to Redis outside the lock with timeout redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) defer cancel() for _, entry := range entries { - pipe := svc.client.Pipeline() + pipe := service.client.Pipeline() pipe.IncrBy(redisCtx, entry.key, entry.delta) pipe.ExpireNX(redisCtx, entry.key, entry.ttl) if _, err := pipe.Exec(redisCtx); err != nil { - if svc.logger != nil { - svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit for key [%s]", entry.key))) - } + service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit for key [%s]", entry.key))) } } } -func (svc *RateLimitService) hydrate(ctx context.Context, userID string) (*userCounter, error) { - if svc.client == nil { +func (service *RateLimitService) hydrate(ctx context.Context, userID string) (*userCounter, error) { + if service.client == nil { return &userCounter{ count: 0, windowExpiry: time.Now().Add(rateLimitWindow), @@ -202,7 +192,7 @@ func (svc *RateLimitService) hydrate(ctx context.Context, userID string) (*userC defer cancel() key := rateLimitKeyPrefix + userID - countStr, err := svc.client.Get(redisCtx, key).Result() + countStr, err := service.client.Get(redisCtx, key).Result() if err == redis.Nil { return &userCounter{ count: 0, @@ -219,15 +209,15 @@ func (svc *RateLimitService) hydrate(ctx context.Context, userID string) (*userC return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse rate limit count [%s] for user [%s]", countStr, userID)) } - ttl, err := svc.client.TTL(redisCtx, key).Result() + ttl, err := service.client.TTL(redisCtx, key).Result() if err != nil || ttl <= 0 { ttl = rateLimitWindow } // Check if already notified (in Redis) notifiedKey := rateLimitNotifiedPrefix + userID - if exists, _ := svc.client.Exists(redisCtx, notifiedKey).Result(); exists > 0 { - svc.notified[userID] = true + if exists, _ := service.client.Exists(redisCtx, notifiedKey).Result(); exists > 0 { + service.notified[userID] = true } return &userCounter{ @@ -237,18 +227,18 @@ func (svc *RateLimitService) hydrate(ctx context.Context, userID string) (*userC }, nil } -func (svc *RateLimitService) emitExceededEvent(ctx context.Context, userID entities.UserID, count int64, limit uint, plan entities.SubscriptionName) { - if svc.dispatcher == nil { +func (service *RateLimitService) emitExceededEvent(ctx context.Context, userID entities.UserID, count int64, limit uint, plan entities.SubscriptionName) { + if service.dispatcher == nil { return } // Set notified flag in Redis (24h TTL) to survive restarts - if svc.client != nil { + if service.client != nil { redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) defer cancel() notifiedKey := rateLimitNotifiedPrefix + string(userID) - if err := svc.client.Set(redisCtx, notifiedKey, "1", rateLimitWindow).Err(); err != nil && svc.logger != nil { - svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot persist rate limit notified flag for user [%s]", userID))) + if err := service.client.Set(redisCtx, notifiedKey, "1", rateLimitWindow).Err(); err != nil { + service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot persist rate limit notified flag for user [%s]", userID))) } } @@ -260,17 +250,13 @@ func (svc *RateLimitService) emitExceededEvent(ctx context.Context, userID entit Timestamp: time.Now().UTC(), } - event, err := svc.createEvent(events.RateLimitExceeded, string(userID), payload) + event, err := service.createEvent(events.RateLimitExceeded, string(userID), payload) if err != nil { - if svc.logger != nil { - svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot create rate limit exceeded event for user [%s]", userID))) - } + service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot create rate limit exceeded event for user [%s]", userID))) return } - if err = svc.dispatcher.Dispatch(ctx, event); err != nil { - if svc.logger != nil { - svc.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot dispatch rate limit exceeded event for user [%s]", userID))) - } + if err = service.dispatcher.Dispatch(ctx, event); err != nil { + service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot dispatch rate limit exceeded event for user [%s]", userID))) } } diff --git a/api/pkg/services/rate_limit_service_test.go b/api/pkg/services/rate_limit_service_test.go index 96030a0df..29224a7fd 100644 --- a/api/pkg/services/rate_limit_service_test.go +++ b/api/pkg/services/rate_limit_service_test.go @@ -6,8 +6,10 @@ import ( "time" "github.com/NdoleStudio/httpsms/pkg/entities" + "github.com/NdoleStudio/httpsms/pkg/telemetry" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/trace" ) func TestRateLimitService_Increment_BasicCount(t *testing.T) { @@ -107,5 +109,18 @@ func TestRateLimitService_Increment_WindowExpiry(t *testing.T) { // suitable for unit tests that only test in-memory logic. func newTestRateLimitService(t *testing.T) *RateLimitService { t.Helper() - return NewRateLimitService(nil, nil, nil, nil) + return NewRateLimitService(nil, &testLogger{}, nil, nil) } + +type testLogger struct{} + +func (l *testLogger) Info(string) {} +func (l *testLogger) Error(error) {} +func (l *testLogger) Warn(error) {} +func (l *testLogger) Fatal(error) {} +func (l *testLogger) Trace(string) {} +func (l *testLogger) Debug(string) {} +func (l *testLogger) WithService(string) telemetry.Logger { return l } +func (l *testLogger) WithString(string, string) telemetry.Logger { return l } +func (l *testLogger) WithSpan(trace.SpanContext) telemetry.Logger { return l } +func (l *testLogger) Printf(string, ...interface{}) {} From 07461d8e0804e34427f6c5ecc7ae6dd8e88cb881 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 12:15:48 +0300 Subject: [PATCH 10/16] refactor: gate rate limiting at container level, skip middleware entirely when disabled Move RATE_LIMIT_ENABLED check to container.go so the middleware, service, and Redis client are never initialized when rate limiting is disabled. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/di/container.go | 24 +++++++++++--------- api/pkg/middlewares/rate_limit_middleware.go | 13 ++--------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go index 2cf2133fe..c663947e9 100644 --- a/api/pkg/di/container.go +++ b/api/pkg/di/container.go @@ -217,18 +217,20 @@ func (container *Container) App() (app *fiber.App) { app.Use(middlewares.HTTPRequestLogger(container.Tracer(), container.Logger())) app.Use(middlewares.BearerAuth(container.Logger(), container.Tracer(), container.FirebaseAuthClient())) app.Use(middlewares.APIKeyAuth(container.Logger(), container.Tracer(), container.UserRepository())) - app.Use(middlewares.RateLimit( - container.Tracer(), - container.Logger(), - container.RateLimitService(), - container.UserRepository(), - []string{"/v1/events"}, - )) - app.Hooks().OnPreShutdown(func() error { - container.RateLimitService().Close() - return nil - }) + if os.Getenv("RATE_LIMIT_ENABLED") == "true" { + app.Use(middlewares.RateLimit( + container.Tracer(), + container.Logger(), + container.RateLimitService(), + container.UserRepository(), + []string{"/v1/events"}, + )) + app.Hooks().OnPreShutdown(func() error { + container.RateLimitService().Close() + return nil + }) + } container.app = app return app diff --git a/api/pkg/middlewares/rate_limit_middleware.go b/api/pkg/middlewares/rate_limit_middleware.go index 4c58dbb56..2551888b9 100644 --- a/api/pkg/middlewares/rate_limit_middleware.go +++ b/api/pkg/middlewares/rate_limit_middleware.go @@ -1,7 +1,6 @@ package middlewares import ( - "os" "strconv" "strings" @@ -12,7 +11,7 @@ import ( "github.com/gofiber/fiber/v3" ) -const rateLimitCostCap = 200 +const rateLimitCostCap = 100 // RateLimit tracks per-user API request counts without blocking requests. func RateLimit( @@ -22,14 +21,9 @@ func RateLimit( userRepository repositories.UserRepository, excludePaths []string, ) fiber.Handler { - enabled := os.Getenv("RATE_LIMIT_ENABLED") == "true" logger = logger.WithService("middlewares.RateLimit") return func(c fiber.Ctx) error { - if !enabled { - return c.Next() - } - path := c.Path() for _, excluded := range excludePaths { if strings.HasPrefix(path, excluded) { @@ -49,10 +43,7 @@ func RateLimit( if c.Method() == fiber.MethodGet { if limitParam := c.Query("limit"); limitParam != "" { if parsed, err := strconv.Atoi(limitParam); err == nil && parsed > 0 { - cost = parsed - if cost > rateLimitCostCap { - cost = rateLimitCostCap - } + cost = min(parsed, rateLimitCostCap) } } } From 4b25d063589920bf3d67b609f4e12bfc1b63f9c8 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 12:16:39 +0300 Subject: [PATCH 11/16] refactor: rename rate limiter service --- api/pkg/services/rate_limit_service.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go index 268e946af..7232f9655 100644 --- a/api/pkg/services/rate_limit_service.go +++ b/api/pkg/services/rate_limit_service.go @@ -15,8 +15,8 @@ import ( ) const ( - rateLimitKeyPrefix = "rate_limit:" - rateLimitNotifiedPrefix = "rate_limit_notified:" + rateLimitKeyPrefix = "user_rate_limit:" + rateLimitNotifiedPrefix = "user_rate_limit_notified:" rateLimitWindow = 24 * time.Hour rateLimitFlushInterval = 30 * time.Second rateLimitRedisTimeout = 1 * time.Second @@ -50,7 +50,7 @@ func NewRateLimitService( client *redis.Client, dispatcher *EventDispatcher, ) *RateLimitService { - svc := &RateLimitService{ + rateLimiter := &RateLimitService{ tracer: tracer, logger: logger, client: client, @@ -60,8 +60,8 @@ func NewRateLimitService( done: make(chan struct{}), } - go svc.flushLoop() - return svc + go rateLimiter.flushLoop() + return rateLimiter } // Increment adds cost to the user's counter and returns the current count. From a14337730ed6d85203145790b5b9c5af77cf85a5 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 12:20:59 +0300 Subject: [PATCH 12/16] perf: batch all Redis flush operations into a single pipeline call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of creating a separate pipeline per user, all INCRBY+ExpireNX commands are batched into one pipeline.Exec() — one round-trip to Redis regardless of how many users have dirty counters. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/services/rate_limit_service.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go index 7232f9655..6691906ec 100644 --- a/api/pkg/services/rate_limit_service.go +++ b/api/pkg/services/rate_limit_service.go @@ -165,16 +165,18 @@ func (service *RateLimitService) flush(ctx context.Context) { } service.mu.Unlock() - // Flush to Redis outside the lock with timeout + // Flush to Redis outside the lock with timeout using a single pipeline batch redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) defer cancel() + pipe := service.client.Pipeline() for _, entry := range entries { - pipe := service.client.Pipeline() pipe.IncrBy(redisCtx, entry.key, entry.delta) pipe.ExpireNX(redisCtx, entry.key, entry.ttl) + } + if len(entries) > 0 { if _, err := pipe.Exec(redisCtx); err != nil { - service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit for key [%s]", entry.key))) + service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit batch of %d entries", len(entries)))) } } } From ba7ced3e1fd4513d1e158ad02830075bfe89e69e Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 12:26:13 +0300 Subject: [PATCH 13/16] refactor(api): use ctxLogger with span context in RateLimitService Replace plain service.logger calls with ctxLogger (tracer-aware context logger) in flush, hydrate, and emitExceededEvent methods for trace correlation. Close/flushLoop keep plain logger since no request context. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/services/rate_limit_service.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go index 6691906ec..b6d992b74 100644 --- a/api/pkg/services/rate_limit_service.go +++ b/api/pkg/services/rate_limit_service.go @@ -166,7 +166,7 @@ func (service *RateLimitService) flush(ctx context.Context) { service.mu.Unlock() // Flush to Redis outside the lock with timeout using a single pipeline batch - redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) + redisCtx, cancel := context.WithTimeout(ctx, 10*rateLimitRedisTimeout) defer cancel() pipe := service.client.Pipeline() @@ -176,7 +176,7 @@ func (service *RateLimitService) flush(ctx context.Context) { } if len(entries) > 0 { if _, err := pipe.Exec(redisCtx); err != nil { - service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit batch of %d entries", len(entries)))) + service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit batch of [%d] entries", len(entries)))) } } } @@ -190,6 +190,9 @@ func (service *RateLimitService) hydrate(ctx context.Context, userID string) (*u }, nil } + ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) + defer span.End() + redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) defer cancel() @@ -203,12 +206,12 @@ func (service *RateLimitService) hydrate(ctx context.Context, userID string) (*u }, nil } if err != nil { - return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot hydrate rate limit for user [%s]", userID)) + return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot hydrate rate limit for user [%s]", userID))) } count, err := strconv.ParseInt(countStr, 10, 64) if err != nil { - return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse rate limit count [%s] for user [%s]", countStr, userID)) + return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot parse rate limit count [%s] for user [%s]", countStr, userID))) } ttl, err := service.client.TTL(redisCtx, key).Result() @@ -222,6 +225,8 @@ func (service *RateLimitService) hydrate(ctx context.Context, userID string) (*u service.notified[userID] = true } + ctxLogger.Info(fmt.Sprintf("hydrated rate limit for user [%s] with count [%d]", userID, count)) + return &userCounter{ count: count, windowExpiry: time.Now().Add(ttl), @@ -234,13 +239,16 @@ func (service *RateLimitService) emitExceededEvent(ctx context.Context, userID e return } + ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) + defer span.End() + // Set notified flag in Redis (24h TTL) to survive restarts if service.client != nil { redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) defer cancel() notifiedKey := rateLimitNotifiedPrefix + string(userID) if err := service.client.Set(redisCtx, notifiedKey, "1", rateLimitWindow).Err(); err != nil { - service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot persist rate limit notified flag for user [%s]", userID))) + ctxLogger.Error(service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot persist rate limit notified flag for user [%s]", userID)))) } } @@ -254,11 +262,11 @@ func (service *RateLimitService) emitExceededEvent(ctx context.Context, userID e event, err := service.createEvent(events.RateLimitExceeded, string(userID), payload) if err != nil { - service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot create rate limit exceeded event for user [%s]", userID))) + ctxLogger.Error(service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot create rate limit exceeded event for user [%s]", userID)))) return } if err = service.dispatcher.Dispatch(ctx, event); err != nil { - service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot dispatch rate limit exceeded event for user [%s]", userID))) + ctxLogger.Error(service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot dispatch rate limit exceeded event for user [%s]", userID)))) } } From b6656ee0d95d4bd758c939371108f3ceada2de0e Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 30 Jun 2026 12:30:21 +0300 Subject: [PATCH 14/16] refactor: improve logging --- api/pkg/services/rate_limit_service.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go index b6d992b74..7d9361604 100644 --- a/api/pkg/services/rate_limit_service.go +++ b/api/pkg/services/rate_limit_service.go @@ -49,10 +49,10 @@ func NewRateLimitService( logger telemetry.Logger, client *redis.Client, dispatcher *EventDispatcher, -) *RateLimitService { - rateLimiter := &RateLimitService{ +) (rateLimiter *RateLimitService) { + rateLimiter = &RateLimitService{ tracer: tracer, - logger: logger, + logger: logger.WithService(fmt.Sprintf("%T", rateLimiter)), client: client, dispatcher: dispatcher, counters: make(map[string]*userCounter), @@ -109,19 +109,19 @@ func (service *RateLimitService) Increment(ctx context.Context, userID entities. // Close flushes remaining dirty counters and stops the background goroutine. func (service *RateLimitService) Close() { - service.logger.Info("RateLimitService shutting down, flushing counters") + service.logger.Info("rate limit service shutting down, flushing counters") close(service.done) if service.client != nil { service.flush(context.Background()) } - service.logger.Info("RateLimitService shutdown complete") + service.logger.Info("rate limit service shutdown complete") } func (service *RateLimitService) flushLoop() { ticker := time.NewTicker(rateLimitFlushInterval) defer ticker.Stop() - service.logger.Info(fmt.Sprintf("RateLimitService flush loop started (interval: %s)", rateLimitFlushInterval)) + service.logger.Info(fmt.Sprintf("rate limit service flush loop started with interval [%s]", rateLimitFlushInterval)) for { select { @@ -130,7 +130,7 @@ func (service *RateLimitService) flushLoop() { service.flush(context.Background()) } case <-service.done: - service.logger.Info("RateLimitService flush loop stopped") + service.logger.Info("rate limit service flush loop stopped") return } } From 86213a3f2cf216c438bec7a4ada3f1e3c90856f0 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 17 Nov 2026 03:06:19 +0200 Subject: [PATCH 15/16] fix(api): integrate rate tracking with authentication and graceful shutdown Cache subscription plans, track route-authenticated requests, and preserve Fiber-backed user IDs. Configure dedicated bounded Redis operations and flush after HTTP draining. Add focused regression checks and remove ignored design artifacts from the PR. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .github/workflows/ci.yaml | 4 + api/main.go | 16 +- api/pkg/di/container.go | 45 ++++- api/pkg/di/container_test.go | 37 ++++ api/pkg/middlewares/rate_limit_middleware.go | 48 ++++- .../middlewares/rate_limit_middleware_test.go | 188 ++++++++++++++++++ 6 files changed, 319 insertions(+), 19 deletions(-) create mode 100644 api/pkg/middlewares/rate_limit_middleware_test.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fa7a51e6b..01af04b55 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -31,6 +31,10 @@ jobs: api/go.sum tests/go.sum + - name: Run Rate Limit Regression Tests + working-directory: ./api + run: go test -race ./pkg/services ./pkg/middlewares ./pkg/di -run TestRateLimit -count=1 + - name: Generate Firebase Credentials run: | bash tests/generate-firebase-credentials.sh tests/firebase-credentials.json diff --git a/api/main.go b/api/main.go index 5b7539c9c..1cc8fa483 100644 --- a/api/main.go +++ b/api/main.go @@ -1,12 +1,18 @@ package main import ( + "context" "fmt" "os" + "os/signal" "strings" + "syscall" + "time" "github.com/NdoleStudio/httpsms/docs" "github.com/NdoleStudio/httpsms/pkg/di" + "github.com/NdoleStudio/stacktrace" + "github.com/gofiber/fiber/v3" ) // Version is injected at runtime @@ -42,5 +48,13 @@ func main() { } container := di.NewContainer(os.Getenv("GCP_PROJECT_ID"), Version) - container.Logger().Info(container.App().Listen(fmt.Sprintf("%s:%s", os.Getenv("APP_HOST"), os.Getenv("APP_PORT"))).Error()) + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + if err := container.App().Listen( + fmt.Sprintf("%s:%s", os.Getenv("APP_HOST"), os.Getenv("APP_PORT")), + fiber.ListenConfig{GracefulContext: ctx, ShutdownTimeout: 5 * time.Second}, + ); err != nil { + container.Logger().Fatal(stacktrace.Propagatef(err, "cannot listen for HTTP requests")) + } } diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go index c663947e9..1b84a0086 100644 --- a/api/pkg/di/container.go +++ b/api/pkg/di/container.go @@ -9,6 +9,7 @@ import ( "os" "strconv" "strings" + "sync" "time" "cloud.google.com/go/compute/metadata" @@ -98,8 +99,10 @@ type Container struct { phoneRistrettoCache *ristretto.Cache[string, *entities.Phone] contactRistrettoCache *ristretto.Cache[string, services.ContactCacheEntry] inMemoryCache cache.Cache - rateLimitService *services.RateLimitService - redisClient *redis.Client + rateLimitService *services.RateLimitService + redisClient *redis.Client + rateLimitRedisClient *redis.Client + closeOnce sync.Once } // NewLiteContainer creates a Container without any routes or listeners @@ -111,9 +114,16 @@ func NewLiteContainer() (container *Container) { // Close gracefully shuts down container resources func (container *Container) Close() { - if container.rateLimitService != nil { - container.rateLimitService.Close() - } + container.closeOnce.Do(func() { + if container.rateLimitService != nil { + container.rateLimitService.Close() + } + if container.rateLimitRedisClient != nil { + if err := container.rateLimitRedisClient.Close(); err != nil { + container.logger.Error(stacktrace.Propagatef(err, "cannot close rate limit Redis client")) + } + } + }) } // NewContainer creates a new dependency injection container @@ -227,7 +237,14 @@ func (container *Container) App() (app *fiber.App) { []string{"/v1/events"}, )) app.Hooks().OnPreShutdown(func() error { - container.RateLimitService().Close() + container.logger.Info("rate limit shutdown waiting for HTTP requests to drain") + return nil + }) + app.Hooks().OnPostShutdown(func(err error) error { + if err != nil { + container.logger.Error(stacktrace.Propagatef(err, "HTTP shutdown did not finish draining requests")) + } + container.Close() return nil }) } @@ -516,10 +533,24 @@ func (container *Container) RateLimitService() *services.RateLimitService { } container.logger.Debug("creating services.RateLimitService") + options := *container.RedisClient().Options() + options.ContextTimeoutEnabled = true + options.DialTimeout = time.Second + options.ReadTimeout = time.Second + options.WriteTimeout = time.Second + options.PoolTimeout = time.Second + options.MaxRetries = -1 + container.rateLimitRedisClient = redis.NewClient(&options) + if err := redisotel.InstrumentTracing(container.rateLimitRedisClient); err != nil { + container.logger.Error(stacktrace.Propagatef(err, "cannot instrument rate limit Redis tracing")) + } + if err := redisotel.InstrumentMetrics(container.rateLimitRedisClient); err != nil { + container.logger.Error(stacktrace.Propagatef(err, "cannot instrument rate limit Redis metrics")) + } container.rateLimitService = services.NewRateLimitService( container.Tracer(), container.Logger(), - container.RedisClient(), + container.rateLimitRedisClient, container.EventDispatcher(), ) return container.rateLimitService diff --git a/api/pkg/di/container_test.go b/api/pkg/di/container_test.go index 611708d31..7ca96f436 100644 --- a/api/pkg/di/container_test.go +++ b/api/pkg/di/container_test.go @@ -1,8 +1,11 @@ package di import ( + "context" + "net" "reflect" "testing" + "time" "github.com/NdoleStudio/httpsms/pkg/entities" "github.com/NdoleStudio/httpsms/pkg/services" @@ -10,6 +13,40 @@ import ( "github.com/stretchr/testify/require" ) +func TestRateLimitRedisClientBoundsStalledConnections(t *testing.T) { + t.Setenv("ENV", "local") + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { listener.Close() }) + accepted := make(chan net.Conn, 1) + go func() { + connection, err := listener.Accept() + if err == nil { + accepted <- connection + } + }() + t.Cleanup(func() { + select { + case connection := <-accepted: + connection.Close() + case <-time.After(time.Second): + } + }) + t.Setenv("REDIS_URL", "redis://"+listener.Addr().String()) + container := NewLiteContainer() + container.eventDispatcher = &services.EventDispatcher{} + container.RateLimitService() + t.Cleanup(container.Close) + t.Cleanup(func() { container.redisClient.Close() }) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + start := time.Now() + err = container.rateLimitRedisClient.Ping(ctx).Err() + require.Error(t, err) + require.Less(t, time.Since(start), 1500*time.Millisecond) +} + func TestNotificationHTTPClientUsesOTelRoundTripperWithoutRetries(t *testing.T) { t.Setenv("ENV", "local") client := NewLiteContainer().NotificationHTTPClient() diff --git a/api/pkg/middlewares/rate_limit_middleware.go b/api/pkg/middlewares/rate_limit_middleware.go index 2551888b9..c303cc2e7 100644 --- a/api/pkg/middlewares/rate_limit_middleware.go +++ b/api/pkg/middlewares/rate_limit_middleware.go @@ -1,43 +1,56 @@ package middlewares import ( + "context" "strconv" "strings" + "time" "github.com/NdoleStudio/httpsms/pkg/entities" "github.com/NdoleStudio/httpsms/pkg/repositories" - "github.com/NdoleStudio/httpsms/pkg/services" "github.com/NdoleStudio/httpsms/pkg/telemetry" "github.com/gofiber/fiber/v3" + ttlCache "github.com/patrickmn/go-cache" + "golang.org/x/sync/singleflight" ) -const rateLimitCostCap = 100 +const rateLimitCostCap = 200 + +type rateLimitCounter interface { + Increment(context.Context, entities.UserID, entities.SubscriptionName, int) (int64, bool, error) +} // RateLimit tracks per-user API request counts without blocking requests. func RateLimit( tracer telemetry.Tracer, logger telemetry.Logger, - service *services.RateLimitService, + service rateLimitCounter, userRepository repositories.UserRepository, excludePaths []string, ) fiber.Handler { logger = logger.WithService("middlewares.RateLimit") + plans := ttlCache.New(time.Minute, time.Minute) + var loads singleflight.Group return func(c fiber.Ctx) error { path := c.Path() for _, excluded := range excludePaths { - if strings.HasPrefix(path, excluded) { + if path == excluded || strings.HasPrefix(path, excluded+"/") { return c.Next() } } - ctx, span := tracer.StartFromFiberCtx(c, "middlewares.RateLimit") + ctx, span, ctxLogger := tracer.StartFromFiberCtxWithLogger(c, logger, "middlewares.RateLimit") defer span.End() + // Route-specific phone and integration authentication runs inside Next. + nextErr := c.Next() authUser, ok := c.Locals(ContextKeyAuthUserID).(entities.AuthContext) if !ok || authUser.IsNoop() { - return c.Next() + return nextErr } + // Cache keys outlive Fiber's reusable request buffers. + userID := entities.UserID(strings.Clone(string(authUser.ID))) cost := 1 if c.Method() == fiber.MethodGet { @@ -48,15 +61,28 @@ func RateLimit( } } - user, err := userRepository.Load(ctx, authUser.ID) + plan, err, _ := loads.Do(string(userID), func() (interface{}, error) { + if cached, found := plans.Get(string(userID)); found { + return cached, nil + } + loadCtx, cancel := context.WithTimeout(ctx, time.Second) + defer cancel() + user, err := userRepository.Load(loadCtx, userID) + if err != nil { + return nil, err + } + plans.SetDefault(string(userID), user.SubscriptionName) + return user.SubscriptionName, nil + }) if err != nil { - ctxLogger := tracer.CtxLogger(logger, span) ctxLogger.Error(err) - return c.Next() + return nextErr } - _, _, _ = service.Increment(ctx, authUser.ID, user.SubscriptionName, cost) + if _, _, err := service.Increment(ctx, userID, plan.(entities.SubscriptionName), cost); err != nil { + ctxLogger.Error(err) + } - return c.Next() + return nextErr } } diff --git a/api/pkg/middlewares/rate_limit_middleware_test.go b/api/pkg/middlewares/rate_limit_middleware_test.go new file mode 100644 index 000000000..4a81d9146 --- /dev/null +++ b/api/pkg/middlewares/rate_limit_middleware_test.go @@ -0,0 +1,188 @@ +package middlewares + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "testing" + + "github.com/NdoleStudio/httpsms/pkg/entities" + "github.com/NdoleStudio/httpsms/pkg/repositories" + "github.com/NdoleStudio/httpsms/pkg/telemetry" + "github.com/gofiber/fiber/v3" + "github.com/hirosassa/zerodriver" + "github.com/rs/zerolog" + "github.com/stretchr/testify/require" +) + +func TestRateLimitMiddleware_WeightedCost(t *testing.T) { + for _, test := range []struct { + name string + method string + query string + cost int + }{ + {"requested page", http.MethodGet, "?limit=10", 10}, + {"search maximum", http.MethodGet, "?limit=200", 200}, + {"capped", http.MethodGet, "?limit=300", 200}, + {"missing limit", http.MethodGet, "", 1}, + {"invalid limit", http.MethodGet, "?limit=invalid", 1}, + {"negative limit", http.MethodGet, "?limit=-10", 1}, + {"write request", http.MethodPost, "?limit=10", 1}, + } { + t.Run(test.name, func(t *testing.T) { + repo := &rateLimitUserRepository{} + counter := &recordingRateLimitCounter{} + app := rateLimitTestApp(repo, counter, rateLimitTestAuth, nil) + response, err := app.Test(httptest.NewRequest(test.method, "/v1/messages/search"+test.query, nil)) + require.NoError(t, err) + defer response.Body.Close() + require.Equal(t, http.StatusOK, response.StatusCode) + require.Equal(t, []int{test.cost}, counter.costs) + }) + } +} + +func TestRateLimitMiddleware_RouteAuthentication(t *testing.T) { + for _, phoneKey := range []bool{false, true} { + t.Run(map[bool]string{false: "3cx bearer key", true: "phone key"}[phoneKey], func(t *testing.T) { + repo := &rateLimitUserRepository{} + counter := &recordingRateLimitCounter{} + logger, tracer := rateLimitTestTelemetry() + auth := BearerAPIKeyAuth(logger, tracer, repo) + request := httptest.NewRequest(http.MethodGet, "/v1/messages/outstanding", nil) + request.Header.Set("Authorization", "Bearer test-key") + if phoneKey { + auth = PhoneAPIKeyAuth(logger, tracer, &rateLimitPhoneRepository{}) + request.Header.Set("x-api-key", "pk_test-key") + } + app := rateLimitTestApp(repo, counter, auth, nil) + response, err := app.Test(request) + require.NoError(t, err) + defer response.Body.Close() + require.Equal(t, http.StatusOK, response.StatusCode) + require.Equal(t, []entities.UserID{"user"}, counter.users) + }) + } +} + +func TestRateLimitMiddleware_CachesPlansPerUser(t *testing.T) { + repo := &rateLimitUserRepository{} + counter := &recordingRateLimitCounter{} + app := rateLimitTestApp(repo, counter, rateLimitTestAuth, nil) + for _, user := range []string{"user", "user", "other", "other", "user"} { + request := httptest.NewRequest(http.MethodGet, "/v1/messages/search", nil) + request.Header.Set("test-user", user) + response, err := app.Test(request) + require.NoError(t, err) + response.Body.Close() + } + require.Equal(t, 2, repo.loads) + require.Len(t, counter.costs, 5) +} + +func TestRateLimitMiddleware_Exclusions(t *testing.T) { + repo := &rateLimitUserRepository{} + counter := &recordingRateLimitCounter{} + app := rateLimitTestApp(repo, counter, rateLimitTestAuth, nil) + for _, path := range []string{"/v1/events", "/v1/events/dispatch", "/v1/events-other"} { + response, err := app.Test(httptest.NewRequest(http.MethodGet, path, nil)) + require.NoError(t, err) + response.Body.Close() + } + require.Equal(t, []int{1}, counter.costs) + + anonymous := rateLimitTestApp(repo, counter, func(c fiber.Ctx) error { return c.Next() }, nil) + response, err := anonymous.Test(httptest.NewRequest(http.MethodGet, "/v1/messages/search", nil)) + require.NoError(t, err) + response.Body.Close() + require.Len(t, counter.costs, 1) +} + +func TestRateLimitMiddleware_FailsOpenAndPreservesHandlerErrors(t *testing.T) { + for _, test := range []struct { + name string + repoError error + countError error + nextError error + status int + }{ + {"plan lookup fails", errors.New("database unavailable"), nil, nil, http.StatusOK}, + {"tracking fails", nil, errors.New("redis unavailable"), nil, http.StatusOK}, + {"handler fails", nil, nil, fiber.ErrTeapot, http.StatusTeapot}, + } { + t.Run(test.name, func(t *testing.T) { + app := rateLimitTestApp( + &rateLimitUserRepository{err: test.repoError}, + &recordingRateLimitCounter{err: test.countError}, + rateLimitTestAuth, test.nextError, + ) + response, err := app.Test(httptest.NewRequest(http.MethodGet, "/v1/messages/search", nil)) + require.NoError(t, err) + defer response.Body.Close() + require.Equal(t, test.status, response.StatusCode) + }) + } +} + +type rateLimitUserRepository struct { + repositories.UserRepository + loads int + err error +} + +func (repo *rateLimitUserRepository) Load(context.Context, entities.UserID) (*entities.User, error) { + repo.loads++ + return &entities.User{SubscriptionName: entities.SubscriptionNameFree}, repo.err +} + +func (*rateLimitUserRepository) LoadAuthContext(context.Context, string) (entities.AuthContext, error) { + return entities.AuthContext{ID: "user", Email: "user@example.com"}, nil +} + +type rateLimitPhoneRepository struct { + repositories.PhoneAPIKeyRepository +} + +func (*rateLimitPhoneRepository) LoadAuthContext(context.Context, string) (entities.AuthContext, error) { + return entities.AuthContext{ID: "user", Email: "user@example.com"}, nil +} + +type recordingRateLimitCounter struct { + costs []int + users []entities.UserID + err error +} + +func (counter *recordingRateLimitCounter) Increment(_ context.Context, user entities.UserID, _ entities.SubscriptionName, cost int) (int64, bool, error) { + counter.costs = append(counter.costs, cost) + counter.users = append(counter.users, user) + return 0, false, counter.err +} + +func rateLimitTestTelemetry() (telemetry.Logger, telemetry.Tracer) { + driver := zerolog.Nop() + logger := telemetry.NewZerologLogger("", nil, &zerodriver.Logger{Logger: &driver}, nil) + return logger, telemetry.NewOtelLogger("", logger) +} + +func rateLimitTestAuth(c fiber.Ctx) error { + user := c.Get("test-user", "user") + c.Locals(ContextKeyAuthUserID, entities.AuthContext{ID: entities.UserID(user), Email: "user@example.com"}) + return c.Next() +} + +func rateLimitTestApp(repo repositories.UserRepository, counter rateLimitCounter, auth fiber.Handler, handlerError error) *fiber.App { + logger, tracer := rateLimitTestTelemetry() + app := fiber.New() + app.Use(RateLimit(tracer, logger, counter, repo, []string{"/v1/events"})) + app.Use(auth) + app.Use(func(c fiber.Ctx) error { + if handlerError != nil { + return handlerError + } + return c.SendStatus(http.StatusOK) + }) + return app +} From bbc8fa8d84e52c1ea8ed3c414f138d755a62e257 Mon Sep 17 00:00:00 2001 From: Acho Arnold Ewin Date: Tue, 17 Nov 2026 03:22:21 +0200 Subject: [PATCH 16/16] feat(api): enforce atomic per-user hourly request budgets Replace delayed tracking with Redis admission before authenticated handlers. Use subscription message limits per hour, weighted costs, HTTP429 and Retry-After, one-second fail-open deadlines, and shared cross-instance quotas. Remove obsolete daily events and background flush workers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/.env.docker | 2 +- api/go.mod | 2 + api/go.sum | 4 + api/pkg/di/container.go | 27 +- api/pkg/di/container_test.go | 25 +- api/pkg/entities/user.go | 5 - api/pkg/entities/user_test.go | 16 - api/pkg/events/rate_limit_exceeded_event.go | 19 - .../middlewares/authenticated_middlesare.go | 5 +- api/pkg/middlewares/rate_limit_middleware.go | 25 +- .../middlewares/rate_limit_middleware_test.go | 82 +++- api/pkg/services/rate_limit_allow.lua | 27 ++ api/pkg/services/rate_limit_service.go | 277 ++----------- api/pkg/services/rate_limit_service_test.go | 366 ++++++++++++++---- 14 files changed, 500 insertions(+), 382 deletions(-) delete mode 100644 api/pkg/events/rate_limit_exceeded_event.go create mode 100644 api/pkg/services/rate_limit_allow.lua diff --git a/api/.env.docker b/api/.env.docker index d7820d82e..928a3fce0 100644 --- a/api/.env.docker +++ b/api/.env.docker @@ -52,7 +52,7 @@ DATABASE_URL_DEDICATED=postgresql://dbusername:dbpassword@postgres:5432/httpsms # Redis connection string REDIS_URL=redis://@redis:6379 -# Rate limiting (set to "true" to enable per-user API rate tracking) +# Set to "true" to enforce subscription message limits as hourly API budgets. RATE_LIMIT_ENABLED=false # Google Cloud Storage bucket for MMS attachments. Leave empty to use in-memory storage. diff --git a/api/go.mod b/api/go.mod index a18ad5049..c2a22647e 100644 --- a/api/go.mod +++ b/api/go.mod @@ -13,6 +13,7 @@ require ( github.com/NdoleStudio/lemonsqueezy-go v1.3.2 github.com/NdoleStudio/plunk-go v0.0.2 github.com/NdoleStudio/stacktrace v1.1.0 + github.com/alicebob/miniredis/v2 v2.35.0 github.com/avast/retry-go/v5 v5.0.0 github.com/axiomhq/axiom-go v0.38.0 github.com/carlmjohnson/requests v0.26.2 @@ -179,6 +180,7 @@ require ( github.com/xuri/efp v0.0.3-0.20260924042702-217b81952dec // indirect github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect + github.com/yuin/gopher-lua v1.1.1 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect go.opentelemetry.io/contrib v1.47.0 // indirect go.opentelemetry.io/contrib/detectors/gcp v1.45.0 // indirect diff --git a/api/go.sum b/api/go.sum index 3cb684e53..57af120a2 100644 --- a/api/go.sum +++ b/api/go.sum @@ -64,6 +64,8 @@ github.com/NdoleStudio/stacktrace v1.1.0 h1:Ai0j//4tDo/So3nR8bZgS3pQ4NYmViHlarpI github.com/NdoleStudio/stacktrace v1.1.0/go.mod h1:WOBcbXShYu+v8JtjFEM+rRV9GJW50m+dwIvp4sLuVTg= github.com/PuerkitoBio/goquery v1.12.0 h1:pAcL4g3WRXekcB9AU/y1mbKez2dbY2AajVhtkO8RIBo= github.com/PuerkitoBio/goquery v1.12.0/go.mod h1:802ej+gV2y7bbIhOIoPY5sT183ZW0YFofScC4q/hIpQ= +github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI= +github.com/alicebob/miniredis/v2 v2.35.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM= github.com/andybalholm/brotli v1.2.2 h1:HzTuoo2ErYQqf5qvcJInB8uvqSVxRttzkFexPWtnceM= github.com/andybalholm/brotli v1.2.2/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= github.com/andybalholm/cascadia v1.3.4 h1:vM2lgh0Vru9Vwyfm4cQqWP2HHMW0u0+2PAW7Q38Qufg= @@ -381,6 +383,8 @@ github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfS github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE= github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg= +github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= +github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs= github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s= go.mongodb.org/mongo-driver/v2 v2.9.2 h1:OCAdjI37pC/SVxucz/9CM8zD4jCdegtsY5QcKzYFCgE= diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go index 1b84a0086..95b3a78ed 100644 --- a/api/pkg/di/container.go +++ b/api/pkg/di/container.go @@ -100,6 +100,7 @@ type Container struct { contactRistrettoCache *ristretto.Cache[string, services.ContactCacheEntry] inMemoryCache cache.Cache rateLimitService *services.RateLimitService + rateLimitMiddleware fiber.Handler redisClient *redis.Client rateLimitRedisClient *redis.Client closeOnce sync.Once @@ -115,13 +116,13 @@ func NewLiteContainer() (container *Container) { // Close gracefully shuts down container resources func (container *Container) Close() { container.closeOnce.Do(func() { - if container.rateLimitService != nil { - container.rateLimitService.Close() - } if container.rateLimitRedisClient != nil { + container.logger.Info("closing rate limit Redis client after HTTP shutdown") if err := container.rateLimitRedisClient.Close(); err != nil { container.logger.Error(stacktrace.Propagatef(err, "cannot close rate limit Redis client")) + return } + container.logger.Info("rate limit Redis client shutdown complete") } }) } @@ -229,13 +230,6 @@ func (container *Container) App() (app *fiber.App) { app.Use(middlewares.APIKeyAuth(container.Logger(), container.Tracer(), container.UserRepository())) if os.Getenv("RATE_LIMIT_ENABLED") == "true" { - app.Use(middlewares.RateLimit( - container.Tracer(), - container.Logger(), - container.RateLimitService(), - container.UserRepository(), - []string{"/v1/events"}, - )) app.Hooks().OnPreShutdown(func() error { container.logger.Info("rate limit shutdown waiting for HTTP requests to drain") return nil @@ -268,7 +262,17 @@ func (container *Container) PhoneAPIKeyMiddleware() fiber.Handler { // AuthenticatedMiddleware creates a new instance of middlewares.Authenticated func (container *Container) AuthenticatedMiddleware() fiber.Handler { container.logger.Debug("creating middlewares.Authenticated") - return middlewares.Authenticated(container.Tracer()) + var limiter fiber.Handler + if os.Getenv("RATE_LIMIT_ENABLED") == "true" { + if container.rateLimitMiddleware == nil { + container.rateLimitMiddleware = middlewares.RateLimit( + container.Tracer(), container.Logger(), container.RateLimitService(), + container.UserRepository(), []string{"/v1/events"}, + ) + } + limiter = container.rateLimitMiddleware + } + return middlewares.Authenticated(container.Tracer(), limiter) } // Logger creates a new instance of telemetry.Logger @@ -551,7 +555,6 @@ func (container *Container) RateLimitService() *services.RateLimitService { container.Tracer(), container.Logger(), container.rateLimitRedisClient, - container.EventDispatcher(), ) return container.rateLimitService } diff --git a/api/pkg/di/container_test.go b/api/pkg/di/container_test.go index 7ca96f436..2ff3a8eb5 100644 --- a/api/pkg/di/container_test.go +++ b/api/pkg/di/container_test.go @@ -3,16 +3,40 @@ package di import ( "context" "net" + "net/http" + "net/http/httptest" "reflect" "testing" "time" "github.com/NdoleStudio/httpsms/pkg/entities" + "github.com/NdoleStudio/httpsms/pkg/middlewares" "github.com/NdoleStudio/httpsms/pkg/services" + "github.com/gofiber/fiber/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +func TestRateLimitDisabledDoesNotInitializeResources(t *testing.T) { + t.Setenv("ENV", "local") + t.Setenv("RATE_LIMIT_ENABLED", "false") + container := NewLiteContainer() + app := fiber.New() + app.Use(func(c fiber.Ctx) error { + c.Locals(middlewares.ContextKeyAuthUserID, entities.AuthContext{ID: "user", Email: "user@example.com"}) + return c.Next() + }) + app.Use(container.AuthenticatedMiddleware()) + app.Get("/", func(c fiber.Ctx) error { return c.SendStatus(http.StatusOK) }) + response, err := app.Test(httptest.NewRequest(http.MethodGet, "/", nil)) + require.NoError(t, err) + defer response.Body.Close() + require.Equal(t, http.StatusOK, response.StatusCode) + require.Nil(t, container.rateLimitService) + require.Nil(t, container.rateLimitMiddleware) + require.Nil(t, container.rateLimitRedisClient) +} + func TestRateLimitRedisClientBoundsStalledConnections(t *testing.T) { t.Setenv("ENV", "local") listener, err := net.Listen("tcp", "127.0.0.1:0") @@ -34,7 +58,6 @@ func TestRateLimitRedisClientBoundsStalledConnections(t *testing.T) { }) t.Setenv("REDIS_URL", "redis://"+listener.Addr().String()) container := NewLiteContainer() - container.eventDispatcher = &services.EventDispatcher{} container.RateLimitService() t.Cleanup(container.Close) t.Cleanup(func() { container.redisClient.Close() }) diff --git a/api/pkg/entities/user.go b/api/pkg/entities/user.go index 94fda428c..6cf2eba93 100644 --- a/api/pkg/entities/user.go +++ b/api/pkg/entities/user.go @@ -37,11 +37,6 @@ func (subscription SubscriptionName) Limit() uint { } } -// RateLimit returns the daily API request rate limit for a subscription -func (subscription SubscriptionName) RateLimit() uint { - return subscription.Limit() * 2 -} - // SubscriptionNameFree represents a free subscription const SubscriptionNameFree = SubscriptionName("free") diff --git a/api/pkg/entities/user_test.go b/api/pkg/entities/user_test.go index 63dcf3a61..0417e63fe 100644 --- a/api/pkg/entities/user_test.go +++ b/api/pkg/entities/user_test.go @@ -51,19 +51,3 @@ func TestUser_GetBillingAnchorDay_PaidUserDay31(t *testing.T) { } assert.Equal(t, 31, user.GetBillingAnchorDay()) } - -func TestSubscriptionName_RateLimit_Free(t *testing.T) { - assert.Equal(t, uint(400), SubscriptionNameFree.RateLimit()) -} - -func TestSubscriptionName_RateLimit_Pro(t *testing.T) { - assert.Equal(t, uint(10000), SubscriptionNameProMonthly.RateLimit()) -} - -func TestSubscriptionName_RateLimit_Ultra(t *testing.T) { - assert.Equal(t, uint(20000), SubscriptionNameUltraMonthly.RateLimit()) -} - -func TestSubscriptionName_RateLimit_200K(t *testing.T) { - assert.Equal(t, uint(400000), SubscriptionName200KMonthly.RateLimit()) -} diff --git a/api/pkg/events/rate_limit_exceeded_event.go b/api/pkg/events/rate_limit_exceeded_event.go deleted file mode 100644 index d27f1731a..000000000 --- a/api/pkg/events/rate_limit_exceeded_event.go +++ /dev/null @@ -1,19 +0,0 @@ -package events - -import ( - "time" - - "github.com/NdoleStudio/httpsms/pkg/entities" -) - -// RateLimitExceeded is raised when a user exceeds their daily API rate limit. -const RateLimitExceeded = "rate.limit.exceeded" - -// RateLimitExceededPayload stores the data for the RateLimitExceeded event -type RateLimitExceededPayload struct { - UserID entities.UserID `json:"user_id"` - Count int64 `json:"count"` - Limit uint `json:"limit"` - Plan string `json:"plan"` - Timestamp time.Time `json:"timestamp"` -} diff --git a/api/pkg/middlewares/authenticated_middlesare.go b/api/pkg/middlewares/authenticated_middlesare.go index d7e6221d4..a4f5b4f5e 100644 --- a/api/pkg/middlewares/authenticated_middlesare.go +++ b/api/pkg/middlewares/authenticated_middlesare.go @@ -18,7 +18,7 @@ const ( ) // Authenticated checks if the request is authenticated -func Authenticated(tracer telemetry.Tracer) fiber.Handler { +func Authenticated(tracer telemetry.Tracer, afterAuth fiber.Handler) fiber.Handler { return func(c fiber.Ctx) error { _, span := tracer.StartFromFiberCtx(c, "middlewares.Authenticated") defer span.End() @@ -31,6 +31,9 @@ func Authenticated(tracer telemetry.Tracer) fiber.Handler { }) } + if afterAuth != nil { + return afterAuth(c) + } return c.Next() } } diff --git a/api/pkg/middlewares/rate_limit_middleware.go b/api/pkg/middlewares/rate_limit_middleware.go index c303cc2e7..433ea7fad 100644 --- a/api/pkg/middlewares/rate_limit_middleware.go +++ b/api/pkg/middlewares/rate_limit_middleware.go @@ -17,10 +17,10 @@ import ( const rateLimitCostCap = 200 type rateLimitCounter interface { - Increment(context.Context, entities.UserID, entities.SubscriptionName, int) (int64, bool, error) + Allow(context.Context, entities.UserID, entities.SubscriptionName, int) (bool, time.Duration, error) } -// RateLimit tracks per-user API request counts without blocking requests. +// RateLimit enforces hourly per-user budgets after authentication and before handlers. func RateLimit( tracer telemetry.Tracer, logger telemetry.Logger, @@ -43,11 +43,9 @@ func RateLimit( ctx, span, ctxLogger := tracer.StartFromFiberCtxWithLogger(c, logger, "middlewares.RateLimit") defer span.End() - // Route-specific phone and integration authentication runs inside Next. - nextErr := c.Next() authUser, ok := c.Locals(ContextKeyAuthUserID).(entities.AuthContext) if !ok || authUser.IsNoop() { - return nextErr + return c.Next() } // Cache keys outlive Fiber's reusable request buffers. userID := entities.UserID(strings.Clone(string(authUser.ID))) @@ -76,13 +74,24 @@ func RateLimit( }) if err != nil { ctxLogger.Error(err) - return nextErr + return c.Next() } - if _, _, err := service.Increment(ctx, userID, plan.(entities.SubscriptionName), cost); err != nil { + allowed, retryAfter, err := service.Allow(ctx, userID, plan.(entities.SubscriptionName), cost) + if err != nil { ctxLogger.Error(err) + return c.Next() + } + if !allowed { + seconds := max(1, int((retryAfter+time.Second-1)/time.Second)) + c.Set(fiber.HeaderRetryAfter, strconv.Itoa(seconds)) + ctxLogger.WithString("user_id", string(userID)).WithString("plan", string(plan.(entities.SubscriptionName))).Info("hourly API rate limit exceeded") + return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{ + "status": "error", + "message": "Hourly API rate limit exceeded. Try again after the time indicated by Retry-After.", + }) } - return nextErr + return c.Next() } } diff --git a/api/pkg/middlewares/rate_limit_middleware_test.go b/api/pkg/middlewares/rate_limit_middleware_test.go index 4a81d9146..b5a492c2e 100644 --- a/api/pkg/middlewares/rate_limit_middleware_test.go +++ b/api/pkg/middlewares/rate_limit_middleware_test.go @@ -6,12 +6,16 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/NdoleStudio/httpsms/pkg/entities" "github.com/NdoleStudio/httpsms/pkg/repositories" + "github.com/NdoleStudio/httpsms/pkg/services" "github.com/NdoleStudio/httpsms/pkg/telemetry" + "github.com/alicebob/miniredis/v2" "github.com/gofiber/fiber/v3" "github.com/hirosassa/zerodriver" + "github.com/redis/go-redis/v9" "github.com/rs/zerolog" "github.com/stretchr/testify/require" ) @@ -126,6 +130,69 @@ func TestRateLimitMiddleware_FailsOpenAndPreservesHandlerErrors(t *testing.T) { } } +func TestRateLimitMiddleware_RejectsBeforeHandler(t *testing.T) { + counter := &recordingRateLimitCounter{denied: true, retryAfter: 1500 * time.Millisecond} + app := rateLimitTestApp(&rateLimitUserRepository{}, counter, rateLimitTestAuth, nil) + response, err := app.Test(httptest.NewRequest(http.MethodPost, "/v1/messages/send", nil)) + require.NoError(t, err) + defer response.Body.Close() + require.Equal(t, http.StatusTooManyRequests, response.StatusCode) + require.Equal(t, "2", response.Header.Get("Retry-After")) + require.Empty(t, response.Header.Get("X-Handler-Ran")) +} + +func TestRateLimitMiddleware_HourlyEnforcementAcrossRequests(t *testing.T) { + server := miniredis.RunT(t) + client := redis.NewClient(&redis.Options{Addr: server.Addr(), ContextTimeoutEnabled: true, MaxRetries: -1}) + t.Cleanup(func() { client.Close() }) + logger, tracer := rateLimitTestTelemetry() + limiter := services.NewRateLimitService(tracer, logger, client) + app := fiber.New() + app.Use(rateLimitTestAuth) + app.Use(Authenticated(tracer, RateLimit(tracer, logger, limiter, &rateLimitUserRepository{}, []string{"/v1/events"}))) + app.Use(func(c fiber.Ctx) error { + c.Set("X-Handler-Ran", "true") + return c.SendStatus(http.StatusOK) + }) + for _, test := range []struct { + path string + status int + }{ + {"/v1/messages/search?limit=199", http.StatusOK}, + {"/v1/messages/search?limit=2", http.StatusTooManyRequests}, + {"/v1/messages/search?limit=1", http.StatusOK}, + {"/v1/messages/search?limit=1", http.StatusTooManyRequests}, + {"/v1/events", http.StatusOK}, + } { + response, err := app.Test(httptest.NewRequest(http.MethodGet, test.path, nil)) + require.NoError(t, err) + response.Body.Close() + require.Equal(t, test.status, response.StatusCode) + if test.status == http.StatusTooManyRequests { + require.Equal(t, "3600", response.Header.Get("Retry-After")) + require.Empty(t, response.Header.Get("X-Handler-Ran")) + } + } + server.FastForward(time.Hour) + response, err := app.Test(httptest.NewRequest(http.MethodGet, "/v1/messages/search?limit=200", nil)) + require.NoError(t, err) + defer response.Body.Close() + require.Equal(t, http.StatusOK, response.StatusCode) +} + +func TestRateLimitMiddleware_AuthenticationPrecedesEnforcement(t *testing.T) { + logger, tracer := rateLimitTestTelemetry() + counter := &recordingRateLimitCounter{denied: true, retryAfter: time.Hour} + app := fiber.New() + app.Use(Authenticated(tracer, RateLimit(tracer, logger, counter, &rateLimitUserRepository{}, nil))) + app.Get("/", func(c fiber.Ctx) error { return c.SendStatus(http.StatusOK) }) + response, err := app.Test(httptest.NewRequest(http.MethodGet, "/", nil)) + require.NoError(t, err) + defer response.Body.Close() + require.Equal(t, http.StatusUnauthorized, response.StatusCode) + require.Empty(t, counter.costs) +} + type rateLimitUserRepository struct { repositories.UserRepository loads int @@ -150,15 +217,17 @@ func (*rateLimitPhoneRepository) LoadAuthContext(context.Context, string) (entit } type recordingRateLimitCounter struct { - costs []int - users []entities.UserID - err error + costs []int + users []entities.UserID + err error + denied bool + retryAfter time.Duration } -func (counter *recordingRateLimitCounter) Increment(_ context.Context, user entities.UserID, _ entities.SubscriptionName, cost int) (int64, bool, error) { +func (counter *recordingRateLimitCounter) Allow(_ context.Context, user entities.UserID, _ entities.SubscriptionName, cost int) (bool, time.Duration, error) { counter.costs = append(counter.costs, cost) counter.users = append(counter.users, user) - return 0, false, counter.err + return !counter.denied, counter.retryAfter, counter.err } func rateLimitTestTelemetry() (telemetry.Logger, telemetry.Tracer) { @@ -176,9 +245,10 @@ func rateLimitTestAuth(c fiber.Ctx) error { func rateLimitTestApp(repo repositories.UserRepository, counter rateLimitCounter, auth fiber.Handler, handlerError error) *fiber.App { logger, tracer := rateLimitTestTelemetry() app := fiber.New() - app.Use(RateLimit(tracer, logger, counter, repo, []string{"/v1/events"})) app.Use(auth) + app.Use(RateLimit(tracer, logger, counter, repo, []string{"/v1/events"})) app.Use(func(c fiber.Ctx) error { + c.Set("X-Handler-Ran", "true") if handlerError != nil { return handlerError } diff --git a/api/pkg/services/rate_limit_allow.lua b/api/pkg/services/rate_limit_allow.lua new file mode 100644 index 000000000..188b91b1e --- /dev/null +++ b/api/pkg/services/rate_limit_allow.lua @@ -0,0 +1,27 @@ +local key = KEYS[1] +local limit, cost, window = tonumber(ARGV[1]), tonumber(ARGV[2]), tonumber(ARGV[3]) +local stored = redis.call('GET', key) +local count, ttl = 0, 0 + +if stored then + count = tonumber(stored) + if not string.match(stored, '^%d+$') or not count or count > 9007199254740991 + or (#stored > 1 and string.sub(stored, 1, 1) == '0') then + return redis.error_reply('invalid hourly rate limit counter') + end + ttl = redis.call('PTTL', key) + if ttl < 0 or ttl > window then + return redis.error_reply('invalid hourly rate limit expiry') + end +end + +if cost > limit - count then + return {0, ttl} +end + +if stored then + redis.call('INCRBY', key, cost) +else + redis.call('SET', key, cost, 'PX', window) +end +return {1, 0} diff --git a/api/pkg/services/rate_limit_service.go b/api/pkg/services/rate_limit_service.go index 7d9361604..886f9d5fd 100644 --- a/api/pkg/services/rate_limit_service.go +++ b/api/pkg/services/rate_limit_service.go @@ -2,271 +2,62 @@ package services import ( "context" + _ "embed" "fmt" - "strconv" - "sync" "time" "github.com/NdoleStudio/httpsms/pkg/entities" - "github.com/NdoleStudio/httpsms/pkg/events" "github.com/NdoleStudio/httpsms/pkg/telemetry" - "github.com/palantir/stacktrace" + "github.com/NdoleStudio/stacktrace" "github.com/redis/go-redis/v9" ) const ( - rateLimitKeyPrefix = "user_rate_limit:" - rateLimitNotifiedPrefix = "user_rate_limit_notified:" - rateLimitWindow = 24 * time.Hour - rateLimitFlushInterval = 30 * time.Second - rateLimitRedisTimeout = 1 * time.Second + rateLimitKeyPrefix = "user_rate_limit:hourly:" + rateLimitWindow = time.Hour + rateLimitRedisTimeout = time.Second ) -// RateLimitService tracks per-user API request counts with in-memory counters -// and periodic Redis sync. -type RateLimitService struct { - service - tracer telemetry.Tracer - logger telemetry.Logger - client *redis.Client - dispatcher *EventDispatcher - - mu sync.Mutex - counters map[string]*userCounter - notified map[string]bool - done chan struct{} -} - -type userCounter struct { - count int64 - windowExpiry time.Time - dirty int64 -} - -// NewRateLimitService creates a new RateLimitService and starts the background flush goroutine. -func NewRateLimitService( - tracer telemetry.Tracer, - logger telemetry.Logger, - client *redis.Client, - dispatcher *EventDispatcher, -) (rateLimiter *RateLimitService) { - rateLimiter = &RateLimitService{ - tracer: tracer, - logger: logger.WithService(fmt.Sprintf("%T", rateLimiter)), - client: client, - dispatcher: dispatcher, - counters: make(map[string]*userCounter), - notified: make(map[string]bool), - done: make(chan struct{}), - } - - go rateLimiter.flushLoop() - return rateLimiter -} - -// Increment adds cost to the user's counter and returns the current count. -// If the count exceeds the plan's rate limit, exceeded is true. -func (service *RateLimitService) Increment(ctx context.Context, userID entities.UserID, plan entities.SubscriptionName, cost int) (count int64, exceeded bool, err error) { - service.mu.Lock() - defer service.mu.Unlock() - - key := string(userID) - counter, exists := service.counters[key] - - if !exists { - counter, err = service.hydrate(ctx, key) - if err != nil { - counter = &userCounter{ - count: 0, - windowExpiry: time.Now().Add(rateLimitWindow), - dirty: 0, - } - } - service.counters[key] = counter - } - - // Reset if window has expired - if time.Now().After(counter.windowExpiry) { - counter.count = 0 - counter.dirty = 0 - counter.windowExpiry = time.Now().Add(rateLimitWindow) - service.notified[key] = false - } - - counter.count += int64(cost) - counter.dirty += int64(cost) +//go:embed rate_limit_allow.lua +var rateLimitAllowScript string - limit := plan.RateLimit() - exceeded = counter.count > int64(limit) - - if exceeded && !service.notified[key] { - service.notified[key] = true - go service.emitExceededEvent(ctx, userID, counter.count, limit, plan) - } - - return counter.count, exceeded, nil +// RateLimitService admits requests against shared fixed hourly user budgets. +type RateLimitService struct { + tracer telemetry.Tracer + logger telemetry.Logger + client *redis.Client } -// Close flushes remaining dirty counters and stops the background goroutine. -func (service *RateLimitService) Close() { - service.logger.Info("rate limit service shutting down, flushing counters") - close(service.done) - if service.client != nil { - service.flush(context.Background()) +// NewRateLimitService creates a synchronous Redis-backed admission service. +func NewRateLimitService(tracer telemetry.Tracer, logger telemetry.Logger, client *redis.Client) (rateLimiter *RateLimitService) { + return &RateLimitService{ + tracer: tracer, + logger: logger.WithService(fmt.Sprintf("%T", rateLimiter)), + client: client, } - service.logger.Info("rate limit service shutdown complete") } -func (service *RateLimitService) flushLoop() { - ticker := time.NewTicker(rateLimitFlushInterval) - defer ticker.Stop() - - service.logger.Info(fmt.Sprintf("rate limit service flush loop started with interval [%s]", rateLimitFlushInterval)) - - for { - select { - case <-ticker.C: - if service.client != nil { - service.flush(context.Background()) - } - case <-service.done: - service.logger.Info("rate limit service flush loop stopped") - return - } - } -} - -func (service *RateLimitService) flush(ctx context.Context) { - service.mu.Lock() - // Collect dirty entries - type flushEntry struct { - key string - delta int64 - ttl time.Duration - } - var entries []flushEntry - now := time.Now() - - for key, counter := range service.counters { - // Clean up expired windows - if now.After(counter.windowExpiry) { - delete(service.counters, key) - delete(service.notified, key) - continue - } - if counter.dirty > 0 { - entries = append(entries, flushEntry{ - key: rateLimitKeyPrefix + key, - delta: counter.dirty, - ttl: time.Until(counter.windowExpiry), - }) - counter.dirty = 0 - } - } - service.mu.Unlock() - - // Flush to Redis outside the lock with timeout using a single pipeline batch - redisCtx, cancel := context.WithTimeout(ctx, 10*rateLimitRedisTimeout) - defer cancel() - - pipe := service.client.Pipeline() - for _, entry := range entries { - pipe.IncrBy(redisCtx, entry.key, entry.delta) - pipe.ExpireNX(redisCtx, entry.key, entry.ttl) - } - if len(entries) > 0 { - if _, err := pipe.Exec(redisCtx); err != nil { - service.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot flush rate limit batch of [%d] entries", len(entries)))) - } +// Allow atomically admits and charges cost, or returns the remaining window TTL. +// Failures are returned with allowed=true so callers can log and fail open. +// An oversized request without an active window is denied with a zero TTL. +func (service *RateLimitService) Allow(ctx context.Context, userID entities.UserID, plan entities.SubscriptionName, cost int) (allowed bool, retryAfter time.Duration, err error) { + ctx, span, _ := service.tracer.StartWithLogger(ctx, service.logger) + defer span.End() + if cost <= 0 { + return true, 0, service.tracer.WrapErrorSpan(span, stacktrace.NewErrorf("invalid rate limit cost [%d] for user [%s]", cost, userID)) } -} - -func (service *RateLimitService) hydrate(ctx context.Context, userID string) (*userCounter, error) { if service.client == nil { - return &userCounter{ - count: 0, - windowExpiry: time.Now().Add(rateLimitWindow), - dirty: 0, - }, nil + return true, 0, service.tracer.WrapErrorSpan(span, stacktrace.NewErrorf("rate limit Redis client is not configured for user [%s]", userID)) } - - ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) - defer span.End() - - redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) + ctx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) defer cancel() - - key := rateLimitKeyPrefix + userID - countStr, err := service.client.Get(redisCtx, key).Result() - if err == redis.Nil { - return &userCounter{ - count: 0, - windowExpiry: time.Now().Add(rateLimitWindow), - dirty: 0, - }, nil - } - if err != nil { - return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot hydrate rate limit for user [%s]", userID))) - } - - count, err := strconv.ParseInt(countStr, 10, 64) + result, err := service.client.Eval(ctx, rateLimitAllowScript, []string{rateLimitKeyPrefix + string(userID)}, + plan.Limit(), cost, rateLimitWindow.Milliseconds()).Int64Slice() if err != nil { - return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot parse rate limit count [%s] for user [%s]", countStr, userID))) - } - - ttl, err := service.client.TTL(redisCtx, key).Result() - if err != nil || ttl <= 0 { - ttl = rateLimitWindow - } - - // Check if already notified (in Redis) - notifiedKey := rateLimitNotifiedPrefix + userID - if exists, _ := service.client.Exists(redisCtx, notifiedKey).Result(); exists > 0 { - service.notified[userID] = true + return true, 0, service.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot check hourly rate limit for user [%s]", userID)) } - - ctxLogger.Info(fmt.Sprintf("hydrated rate limit for user [%s] with count [%d]", userID, count)) - - return &userCounter{ - count: count, - windowExpiry: time.Now().Add(ttl), - dirty: 0, - }, nil -} - -func (service *RateLimitService) emitExceededEvent(ctx context.Context, userID entities.UserID, count int64, limit uint, plan entities.SubscriptionName) { - if service.dispatcher == nil { - return - } - - ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) - defer span.End() - - // Set notified flag in Redis (24h TTL) to survive restarts - if service.client != nil { - redisCtx, cancel := context.WithTimeout(ctx, rateLimitRedisTimeout) - defer cancel() - notifiedKey := rateLimitNotifiedPrefix + string(userID) - if err := service.client.Set(redisCtx, notifiedKey, "1", rateLimitWindow).Err(); err != nil { - ctxLogger.Error(service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot persist rate limit notified flag for user [%s]", userID)))) - } - } - - payload := events.RateLimitExceededPayload{ - UserID: userID, - Count: count, - Limit: limit, - Plan: string(plan), - Timestamp: time.Now().UTC(), - } - - event, err := service.createEvent(events.RateLimitExceeded, string(userID), payload) - if err != nil { - ctxLogger.Error(service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot create rate limit exceeded event for user [%s]", userID)))) - return - } - - if err = service.dispatcher.Dispatch(ctx, event); err != nil { - ctxLogger.Error(service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot dispatch rate limit exceeded event for user [%s]", userID)))) + if len(result) != 2 || (result[0] != 0 && result[0] != 1) || result[1] < 0 || result[1] > rateLimitWindow.Milliseconds() { + return true, 0, service.tracer.WrapErrorSpan(span, stacktrace.NewErrorf("invalid hourly rate limit result for user [%s]: %v", userID, result)) } + return result[0] == 1, time.Duration(result[1]) * time.Millisecond, nil } diff --git a/api/pkg/services/rate_limit_service_test.go b/api/pkg/services/rate_limit_service_test.go index 29224a7fd..4b6060cdb 100644 --- a/api/pkg/services/rate_limit_service_test.go +++ b/api/pkg/services/rate_limit_service_test.go @@ -2,114 +2,340 @@ package services import ( "context" + "sync" + "sync/atomic" "testing" "time" "github.com/NdoleStudio/httpsms/pkg/entities" "github.com/NdoleStudio/httpsms/pkg/telemetry" + "github.com/alicebob/miniredis/v2" + "github.com/redis/go-redis/v9" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/trace" ) -func TestRateLimitService_Increment_BasicCount(t *testing.T) { - // Arrange - svc := newTestRateLimitService(t) - defer svc.Close() - - ctx := context.Background() - - // Act - count, exceeded, err := svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 1) +func newTestRateLimitService(t *testing.T, server *miniredis.Miniredis) *RateLimitService { + t.Helper() + client := redis.NewClient(&redis.Options{ + Addr: server.Addr(), ContextTimeoutEnabled: true, MaxRetries: -1, + DialTimeout: time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, PoolTimeout: time.Second, + }) + t.Cleanup(func() { require.NoError(t, client.Close()) }) + logger := &testLogger{} + return NewRateLimitService(telemetry.NewOtelLogger("test", logger), logger, client) +} - // Assert +func TestRateLimitService_FreeHourlyBudget(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + for range 200 { + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.NoError(t, err) + require.True(t, allowed) + require.Zero(t, retryAfter) + } + for range 2 { + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.NoError(t, err) + assert.False(t, allowed) + assert.InDelta(t, time.Hour.Milliseconds(), retryAfter.Milliseconds(), 1000) + } + count, err := server.Get("user_rate_limit:hourly:user") require.NoError(t, err) - assert.Equal(t, int64(1), count) - assert.False(t, exceeded) + assert.Equal(t, "200", count) } -func TestRateLimitService_Increment_WeightedCost(t *testing.T) { - // Arrange - svc := newTestRateLimitService(t) - defer svc.Close() +func TestRateLimitService_WeightedDenialDoesNotSpendRemainingBudget(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + for _, test := range []struct { + cost int + allowed bool + total string + }{ + {190, true, "190"}, + {11, false, "190"}, + {10, true, "200"}, + {1, false, "200"}, + } { + allowed, _, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, test.cost) + require.NoError(t, err) + assert.Equal(t, test.allowed, allowed) + count, err := server.Get("user_rate_limit:hourly:user") + require.NoError(t, err) + assert.Equal(t, test.total, count) + } +} - ctx := context.Background() +func TestRateLimitService_FirstDeniedRequestDoesNotStartWindow(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 201) + require.NoError(t, err) + assert.False(t, allowed) + assert.Zero(t, retryAfter) + assert.False(t, server.Exists("user_rate_limit:hourly:user")) + allowed, _, err = service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 200) + require.NoError(t, err) + assert.True(t, allowed) + assert.Equal(t, time.Hour, server.TTL("user_rate_limit:hourly:user")) +} - // Act - count, _, err := svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 10) +func TestRateLimitService_ConcurrentInstancesNeverOversubscribe(t *testing.T) { + server := miniredis.RunT(t) + var admitted atomic.Int64 + failures := make(chan error, 400) + var workers sync.WaitGroup + for range 4 { + service := newTestRateLimitService(t, server) + workers.Go(func() { + for range 100 { + allowed, _, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + if err != nil { + failures <- err + } else if allowed { + admitted.Add(1) + } + } + }) + } + workers.Wait() + close(failures) + for err := range failures { + assert.NoError(t, err) + } + assert.Equal(t, int64(200), admitted.Load()) + count, err := server.Get("user_rate_limit:hourly:user") + require.NoError(t, err) + assert.Equal(t, "200", count) +} - // Assert +func TestRateLimitService_RestartSeesSharedBudget(t *testing.T) { + server := miniredis.RunT(t) + first := newTestRateLimitService(t, server) + allowed, _, err := first.Allow(context.Background(), "user", entities.SubscriptionNameFree, 199) require.NoError(t, err) - assert.Equal(t, int64(10), count) + require.True(t, allowed) + second := newTestRateLimitService(t, server) + allowed, _, err = second.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.NoError(t, err) + assert.True(t, allowed) + allowed, _, err = second.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.NoError(t, err) + assert.False(t, allowed) } -func TestRateLimitService_Increment_ExceedsLimit(t *testing.T) { - // Arrange - svc := newTestRateLimitService(t) - defer svc.Close() +func TestRateLimitService_AdmitsAndDenialsNeverExtendWindow(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + allowed, _, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 100) + require.NoError(t, err) + require.True(t, allowed) + server.FastForward(30*time.Minute + 500*time.Millisecond) + allowed, _, err = service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.NoError(t, err) + assert.True(t, allowed) + assert.Equal(t, 30*time.Minute-500*time.Millisecond, server.TTL("user_rate_limit:hourly:user")) + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 100) + require.NoError(t, err) + assert.False(t, allowed) + assert.Equal(t, 30*time.Minute-500*time.Millisecond, retryAfter) + assert.Equal(t, retryAfter, server.TTL("user_rate_limit:hourly:user")) +} - ctx := context.Background() +func TestRateLimitService_WindowResetsAfterExactlyOneHour(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + allowed, _, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 200) + require.NoError(t, err) + require.True(t, allowed) + server.FastForward(3599 * time.Second) + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.NoError(t, err) + assert.False(t, allowed) + assert.Equal(t, time.Second, retryAfter) + server.FastForward(time.Second) + allowed, _, err = service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 200) + require.NoError(t, err) + assert.True(t, allowed) + assert.Equal(t, time.Hour, server.TTL("user_rate_limit:hourly:user")) +} - // Free plan limit is 400. Exceed it. - for i := 0; i < 400; i++ { - _, _, _ = svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 1) +func TestRateLimitService_SubscriptionHourlyBudgets(t *testing.T) { + for _, test := range []struct { + plan entities.SubscriptionName + limit int + }{ + {entities.SubscriptionNameFree, 200}, + {entities.SubscriptionNameProMonthly, 5000}, + {entities.SubscriptionNameProYearly, 5000}, + {entities.SubscriptionNameProLifetime, 5000}, + {entities.SubscriptionNameUltraMonthly, 10000}, + {entities.SubscriptionNameUltraYearly, 10000}, + {entities.SubscriptionName20KMonthly, 20000}, + {entities.SubscriptionName20KYearly, 20000}, + {entities.SubscriptionName50KMonthly, 50000}, + {entities.SubscriptionName100KMonthly, 100000}, + {entities.SubscriptionName200KMonthly, 200000}, + } { + t.Run(string(test.plan), func(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + allowed, _, err := service.Allow(context.Background(), "user", test.plan, test.limit) + require.NoError(t, err) + assert.True(t, allowed) + allowed, _, err = service.Allow(context.Background(), "user", test.plan, 1) + require.NoError(t, err) + assert.False(t, allowed) + }) } +} - // Act — this pushes count to 401 - count, exceeded, err := svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 1) - - // Assert +func TestRateLimitService_IgnoresLegacyDailyNamespace(t *testing.T) { + server := miniredis.RunT(t) + require.NoError(t, server.Set("user_rate_limit:user", "999999")) + service := newTestRateLimitService(t, server) + allowed, _, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 200) + require.NoError(t, err) + assert.True(t, allowed) + legacy, err := server.Get("user_rate_limit:user") require.NoError(t, err) - assert.Equal(t, int64(401), count) - assert.True(t, exceeded) + assert.Equal(t, "999999", legacy) } -func TestRateLimitService_Increment_MultipleUsers(t *testing.T) { - // Arrange - svc := newTestRateLimitService(t) - defer svc.Close() +func TestRateLimitService_MalformedStateFailsOpenWithError(t *testing.T) { + for _, state := range []string{"not-an-integer", "-1", "3.5", "1e2", "0200", "9007199254740992", "no-expiry", "long-expiry", "wrong-type"} { + t.Run(state, func(t *testing.T) { + server := miniredis.RunT(t) + key := "user_rate_limit:hourly:user" + switch state { + case "no-expiry": + require.NoError(t, server.Set(key, "10")) + case "long-expiry": + require.NoError(t, server.Set(key, "10")) + server.SetTTL(key, 2*time.Hour) + case "wrong-type": + server.HSet(key, "count", "10") + server.SetTTL(key, time.Hour) + default: + require.NoError(t, server.Set(key, state)) + server.SetTTL(key, time.Hour) + } + service := newTestRateLimitService(t, server) + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.Error(t, err) + assert.Contains(t, err.Error(), "user") + assert.True(t, allowed) + assert.Zero(t, retryAfter) + }) + } +} - ctx := context.Background() +func TestRateLimitService_RedisOutageFailsOpen(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + server.Close() + start := time.Now() + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.Error(t, err) + assert.True(t, allowed) + assert.Zero(t, retryAfter) + assert.Less(t, time.Since(start), 1500*time.Millisecond) +} - // Act - _, _, _ = svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 5) - count, _, err := svc.Increment(ctx, "user-2", entities.SubscriptionNameProMonthly, 3) +func TestRateLimitService_RedisDeadlineIsOneSecond(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + service.client.AddHook(&rateLimitProcessHook{run: func(ctx context.Context, _ redis.Cmder, _ redis.ProcessHook) error { + <-ctx.Done() + return ctx.Err() + }}) + start := time.Now() + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.ErrorIs(t, err, context.DeadlineExceeded) + assert.True(t, allowed) + assert.Zero(t, retryAfter) + assert.Less(t, time.Since(start), 1500*time.Millisecond) +} - // Assert - require.NoError(t, err) - assert.Equal(t, int64(3), count) +func TestRateLimitService_RequestCancellationFailsOpenWithoutSpending(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + allowed, retryAfter, err := service.Allow(ctx, "user", entities.SubscriptionNameFree, 1) + require.ErrorIs(t, err, context.Canceled) + assert.True(t, allowed) + assert.Zero(t, retryAfter) + assert.False(t, server.Exists("user_rate_limit:hourly:user")) } -func TestRateLimitService_Increment_WindowExpiry(t *testing.T) { - // Arrange - svc := newTestRateLimitService(t) - defer svc.Close() +func TestRateLimitService_OneRedisOperationPreservesTraceCorrelation(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + require.NoError(t, service.client.Ping(context.Background()).Err()) + correlation := trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID{1}, SpanID: trace.SpanID{2}}) + ctx := trace.ContextWithSpanContext(context.Background(), correlation) + var commands []string + service.client.AddHook(&rateLimitProcessHook{run: func(ctx context.Context, cmd redis.Cmder, next redis.ProcessHook) error { + commands = append(commands, cmd.Name()) + assert.Equal(t, correlation.TraceID(), trace.SpanContextFromContext(ctx).TraceID()) + return next(ctx, cmd) + }}) + allowed, _, err := service.Allow(ctx, "user", entities.SubscriptionNameFree, 1) + require.NoError(t, err) + assert.True(t, allowed) + assert.Equal(t, []string{"eval"}, commands) +} - ctx := context.Background() +func TestRateLimitService_InvalidCostCannotReduceUsage(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + allowed, _, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 200) + require.NoError(t, err) + require.True(t, allowed) + for _, cost := range []int{0, -1} { + allowed, _, err = service.Allow(context.Background(), "user", entities.SubscriptionNameFree, cost) + require.Error(t, err) + assert.True(t, allowed) + } + allowed, _, err = service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.NoError(t, err) + assert.False(t, allowed) +} - // Simulate an existing counter with an expired window - svc.mu.Lock() - svc.counters["user-1"] = &userCounter{ - count: 500, - windowExpiry: time.Now().Add(-1 * time.Hour), // expired - dirty: 0, +func TestRateLimitService_UserBudgetsAreIndependent(t *testing.T) { + server := miniredis.RunT(t) + service := newTestRateLimitService(t, server) + for _, user := range []entities.UserID{"first", "second"} { + allowed, _, err := service.Allow(context.Background(), user, entities.SubscriptionNameFree, 200) + require.NoError(t, err) + assert.True(t, allowed) } - svc.mu.Unlock() +} - // Act — should reset because the window expired - count, exceeded, err := svc.Increment(ctx, "user-1", entities.SubscriptionNameFree, 1) +func TestRateLimitService_MissingRedisClientFailsOpenWithError(t *testing.T) { + logger := &testLogger{} + service := NewRateLimitService(telemetry.NewOtelLogger("test", logger), logger, nil) + allowed, retryAfter, err := service.Allow(context.Background(), "user", entities.SubscriptionNameFree, 1) + require.Error(t, err) + assert.True(t, allowed) + assert.Zero(t, retryAfter) +} - // Assert - require.NoError(t, err) - assert.Equal(t, int64(1), count) - assert.False(t, exceeded) +type rateLimitProcessHook struct { + run func(context.Context, redis.Cmder, redis.ProcessHook) error } -// newTestRateLimitService creates a RateLimitService with nil redis client (no hydration) -// suitable for unit tests that only test in-memory logic. -func newTestRateLimitService(t *testing.T) *RateLimitService { - t.Helper() - return NewRateLimitService(nil, &testLogger{}, nil, nil) +func (h *rateLimitProcessHook) DialHook(next redis.DialHook) redis.DialHook { return next } +func (h *rateLimitProcessHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook { + return next +} + +func (h *rateLimitProcessHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook { + return func(ctx context.Context, cmd redis.Cmder) error { return h.run(ctx, cmd, next) } } type testLogger struct{} @@ -122,5 +348,5 @@ func (l *testLogger) Trace(string) {} func (l *testLogger) Debug(string) {} func (l *testLogger) WithService(string) telemetry.Logger { return l } func (l *testLogger) WithString(string, string) telemetry.Logger { return l } -func (l *testLogger) WithSpan(trace.SpanContext) telemetry.Logger { return l } +func (l *testLogger) WithSpan(trace.Span) telemetry.Logger { return l } func (l *testLogger) Printf(string, ...interface{}) {}