Skip to content

Commit bbc8fa8

Browse files
AchoArnoldCopilot
andcommitted
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>
1 parent 86213a3 commit bbc8fa8

14 files changed

Lines changed: 500 additions & 382 deletions

‎api/.env.docker‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ DATABASE_URL_DEDICATED=postgresql://dbusername:dbpassword@postgres:5432/httpsms
5252
# Redis connection string
5353
REDIS_URL=redis://@redis:6379
5454

55-
# Rate limiting (set to "true" to enable per-user API rate tracking)
55+
# Set to "true" to enforce subscription message limits as hourly API budgets.
5656
RATE_LIMIT_ENABLED=false
5757

5858
# Google Cloud Storage bucket for MMS attachments. Leave empty to use in-memory storage.

‎api/go.mod‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/NdoleStudio/lemonsqueezy-go v1.3.2
1414
github.com/NdoleStudio/plunk-go v0.0.2
1515
github.com/NdoleStudio/stacktrace v1.1.0
16+
github.com/alicebob/miniredis/v2 v2.35.0
1617
github.com/avast/retry-go/v5 v5.0.0
1718
github.com/axiomhq/axiom-go v0.38.0
1819
github.com/carlmjohnson/requests v0.26.2
@@ -179,6 +180,7 @@ require (
179180
github.com/xuri/efp v0.0.3-0.20260924042702-217b81952dec // indirect
180181
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect
181182
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
183+
github.com/yuin/gopher-lua v1.1.1 // indirect
182184
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
183185
go.opentelemetry.io/contrib v1.47.0 // indirect
184186
go.opentelemetry.io/contrib/detectors/gcp v1.45.0 // indirect

‎api/go.sum‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ github.com/NdoleStudio/stacktrace v1.1.0 h1:Ai0j//4tDo/So3nR8bZgS3pQ4NYmViHlarpI
6464
github.com/NdoleStudio/stacktrace v1.1.0/go.mod h1:WOBcbXShYu+v8JtjFEM+rRV9GJW50m+dwIvp4sLuVTg=
6565
github.com/PuerkitoBio/goquery v1.12.0 h1:pAcL4g3WRXekcB9AU/y1mbKez2dbY2AajVhtkO8RIBo=
6666
github.com/PuerkitoBio/goquery v1.12.0/go.mod h1:802ej+gV2y7bbIhOIoPY5sT183ZW0YFofScC4q/hIpQ=
67+
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
68+
github.com/alicebob/miniredis/v2 v2.35.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
6769
github.com/andybalholm/brotli v1.2.2 h1:HzTuoo2ErYQqf5qvcJInB8uvqSVxRttzkFexPWtnceM=
6870
github.com/andybalholm/brotli v1.2.2/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
6971
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
381383
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
382384
github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE=
383385
github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
386+
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
387+
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
384388
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
385389
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
386390
go.mongodb.org/mongo-driver/v2 v2.9.2 h1:OCAdjI37pC/SVxucz/9CM8zD4jCdegtsY5QcKzYFCgE=

‎api/pkg/di/container.go‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ type Container struct {
100100
contactRistrettoCache *ristretto.Cache[string, services.ContactCacheEntry]
101101
inMemoryCache cache.Cache
102102
rateLimitService *services.RateLimitService
103+
rateLimitMiddleware fiber.Handler
103104
redisClient *redis.Client
104105
rateLimitRedisClient *redis.Client
105106
closeOnce sync.Once
@@ -115,13 +116,13 @@ func NewLiteContainer() (container *Container) {
115116
// Close gracefully shuts down container resources
116117
func (container *Container) Close() {
117118
container.closeOnce.Do(func() {
118-
if container.rateLimitService != nil {
119-
container.rateLimitService.Close()
120-
}
121119
if container.rateLimitRedisClient != nil {
120+
container.logger.Info("closing rate limit Redis client after HTTP shutdown")
122121
if err := container.rateLimitRedisClient.Close(); err != nil {
123122
container.logger.Error(stacktrace.Propagatef(err, "cannot close rate limit Redis client"))
123+
return
124124
}
125+
container.logger.Info("rate limit Redis client shutdown complete")
125126
}
126127
})
127128
}
@@ -229,13 +230,6 @@ func (container *Container) App() (app *fiber.App) {
229230
app.Use(middlewares.APIKeyAuth(container.Logger(), container.Tracer(), container.UserRepository()))
230231

231232
if os.Getenv("RATE_LIMIT_ENABLED") == "true" {
232-
app.Use(middlewares.RateLimit(
233-
container.Tracer(),
234-
container.Logger(),
235-
container.RateLimitService(),
236-
container.UserRepository(),
237-
[]string{"/v1/events"},
238-
))
239233
app.Hooks().OnPreShutdown(func() error {
240234
container.logger.Info("rate limit shutdown waiting for HTTP requests to drain")
241235
return nil
@@ -268,7 +262,17 @@ func (container *Container) PhoneAPIKeyMiddleware() fiber.Handler {
268262
// AuthenticatedMiddleware creates a new instance of middlewares.Authenticated
269263
func (container *Container) AuthenticatedMiddleware() fiber.Handler {
270264
container.logger.Debug("creating middlewares.Authenticated")
271-
return middlewares.Authenticated(container.Tracer())
265+
var limiter fiber.Handler
266+
if os.Getenv("RATE_LIMIT_ENABLED") == "true" {
267+
if container.rateLimitMiddleware == nil {
268+
container.rateLimitMiddleware = middlewares.RateLimit(
269+
container.Tracer(), container.Logger(), container.RateLimitService(),
270+
container.UserRepository(), []string{"/v1/events"},
271+
)
272+
}
273+
limiter = container.rateLimitMiddleware
274+
}
275+
return middlewares.Authenticated(container.Tracer(), limiter)
272276
}
273277

274278
// Logger creates a new instance of telemetry.Logger
@@ -551,7 +555,6 @@ func (container *Container) RateLimitService() *services.RateLimitService {
551555
container.Tracer(),
552556
container.Logger(),
553557
container.rateLimitRedisClient,
554-
container.EventDispatcher(),
555558
)
556559
return container.rateLimitService
557560
}

‎api/pkg/di/container_test.go‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,40 @@ package di
33
import (
44
"context"
55
"net"
6+
"net/http"
7+
"net/http/httptest"
68
"reflect"
79
"testing"
810
"time"
911

1012
"github.com/NdoleStudio/httpsms/pkg/entities"
13+
"github.com/NdoleStudio/httpsms/pkg/middlewares"
1114
"github.com/NdoleStudio/httpsms/pkg/services"
15+
"github.com/gofiber/fiber/v3"
1216
"github.com/stretchr/testify/assert"
1317
"github.com/stretchr/testify/require"
1418
)
1519

20+
func TestRateLimitDisabledDoesNotInitializeResources(t *testing.T) {
21+
t.Setenv("ENV", "local")
22+
t.Setenv("RATE_LIMIT_ENABLED", "false")
23+
container := NewLiteContainer()
24+
app := fiber.New()
25+
app.Use(func(c fiber.Ctx) error {
26+
c.Locals(middlewares.ContextKeyAuthUserID, entities.AuthContext{ID: "user", Email: "user@example.com"})
27+
return c.Next()
28+
})
29+
app.Use(container.AuthenticatedMiddleware())
30+
app.Get("/", func(c fiber.Ctx) error { return c.SendStatus(http.StatusOK) })
31+
response, err := app.Test(httptest.NewRequest(http.MethodGet, "/", nil))
32+
require.NoError(t, err)
33+
defer response.Body.Close()
34+
require.Equal(t, http.StatusOK, response.StatusCode)
35+
require.Nil(t, container.rateLimitService)
36+
require.Nil(t, container.rateLimitMiddleware)
37+
require.Nil(t, container.rateLimitRedisClient)
38+
}
39+
1640
func TestRateLimitRedisClientBoundsStalledConnections(t *testing.T) {
1741
t.Setenv("ENV", "local")
1842
listener, err := net.Listen("tcp", "127.0.0.1:0")
@@ -34,7 +58,6 @@ func TestRateLimitRedisClientBoundsStalledConnections(t *testing.T) {
3458
})
3559
t.Setenv("REDIS_URL", "redis://"+listener.Addr().String())
3660
container := NewLiteContainer()
37-
container.eventDispatcher = &services.EventDispatcher{}
3861
container.RateLimitService()
3962
t.Cleanup(container.Close)
4063
t.Cleanup(func() { container.redisClient.Close() })

‎api/pkg/entities/user.go‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ func (subscription SubscriptionName) Limit() uint {
3737
}
3838
}
3939

40-
// RateLimit returns the daily API request rate limit for a subscription
41-
func (subscription SubscriptionName) RateLimit() uint {
42-
return subscription.Limit() * 2
43-
}
44-
4540
// SubscriptionNameFree represents a free subscription
4641
const SubscriptionNameFree = SubscriptionName("free")
4742

‎api/pkg/entities/user_test.go‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,3 @@ func TestUser_GetBillingAnchorDay_PaidUserDay31(t *testing.T) {
5151
}
5252
assert.Equal(t, 31, user.GetBillingAnchorDay())
5353
}
54-
55-
func TestSubscriptionName_RateLimit_Free(t *testing.T) {
56-
assert.Equal(t, uint(400), SubscriptionNameFree.RateLimit())
57-
}
58-
59-
func TestSubscriptionName_RateLimit_Pro(t *testing.T) {
60-
assert.Equal(t, uint(10000), SubscriptionNameProMonthly.RateLimit())
61-
}
62-
63-
func TestSubscriptionName_RateLimit_Ultra(t *testing.T) {
64-
assert.Equal(t, uint(20000), SubscriptionNameUltraMonthly.RateLimit())
65-
}
66-
67-
func TestSubscriptionName_RateLimit_200K(t *testing.T) {
68-
assert.Equal(t, uint(400000), SubscriptionName200KMonthly.RateLimit())
69-
}

‎api/pkg/events/rate_limit_exceeded_event.go‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎api/pkg/middlewares/authenticated_middlesare.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
)
1919

2020
// Authenticated checks if the request is authenticated
21-
func Authenticated(tracer telemetry.Tracer) fiber.Handler {
21+
func Authenticated(tracer telemetry.Tracer, afterAuth fiber.Handler) fiber.Handler {
2222
return func(c fiber.Ctx) error {
2323
_, span := tracer.StartFromFiberCtx(c, "middlewares.Authenticated")
2424
defer span.End()
@@ -31,6 +31,9 @@ func Authenticated(tracer telemetry.Tracer) fiber.Handler {
3131
})
3232
}
3333

34+
if afterAuth != nil {
35+
return afterAuth(c)
36+
}
3437
return c.Next()
3538
}
3639
}

‎api/pkg/middlewares/rate_limit_middleware.go‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import (
1717
const rateLimitCostCap = 200
1818

1919
type rateLimitCounter interface {
20-
Increment(context.Context, entities.UserID, entities.SubscriptionName, int) (int64, bool, error)
20+
Allow(context.Context, entities.UserID, entities.SubscriptionName, int) (bool, time.Duration, error)
2121
}
2222

23-
// RateLimit tracks per-user API request counts without blocking requests.
23+
// RateLimit enforces hourly per-user budgets after authentication and before handlers.
2424
func RateLimit(
2525
tracer telemetry.Tracer,
2626
logger telemetry.Logger,
@@ -43,11 +43,9 @@ func RateLimit(
4343
ctx, span, ctxLogger := tracer.StartFromFiberCtxWithLogger(c, logger, "middlewares.RateLimit")
4444
defer span.End()
4545

46-
// Route-specific phone and integration authentication runs inside Next.
47-
nextErr := c.Next()
4846
authUser, ok := c.Locals(ContextKeyAuthUserID).(entities.AuthContext)
4947
if !ok || authUser.IsNoop() {
50-
return nextErr
48+
return c.Next()
5149
}
5250
// Cache keys outlive Fiber's reusable request buffers.
5351
userID := entities.UserID(strings.Clone(string(authUser.ID)))
@@ -76,13 +74,24 @@ func RateLimit(
7674
})
7775
if err != nil {
7876
ctxLogger.Error(err)
79-
return nextErr
77+
return c.Next()
8078
}
8179

82-
if _, _, err := service.Increment(ctx, userID, plan.(entities.SubscriptionName), cost); err != nil {
80+
allowed, retryAfter, err := service.Allow(ctx, userID, plan.(entities.SubscriptionName), cost)
81+
if err != nil {
8382
ctxLogger.Error(err)
83+
return c.Next()
84+
}
85+
if !allowed {
86+
seconds := max(1, int((retryAfter+time.Second-1)/time.Second))
87+
c.Set(fiber.HeaderRetryAfter, strconv.Itoa(seconds))
88+
ctxLogger.WithString("user_id", string(userID)).WithString("plan", string(plan.(entities.SubscriptionName))).Info("hourly API rate limit exceeded")
89+
return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{
90+
"status": "error",
91+
"message": "Hourly API rate limit exceeded. Try again after the time indicated by Retry-After.",
92+
})
8493
}
8594

86-
return nextErr
95+
return c.Next()
8796
}
8897
}

0 commit comments

Comments
 (0)