Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
26dc0d5
feat(entities): add RateLimit() method to SubscriptionName
AchoArnold Jun 30, 2026
c7c0191
feat(events): add rate.limit.exceeded CloudEvent type
AchoArnold Jun 30, 2026
0bd0a31
feat(services): implement RateLimitService with in-memory counters an…
AchoArnold Jun 30, 2026
5460583
feat(middlewares): add rate limit middleware for tracking API usage
AchoArnold Jun 30, 2026
59ef5e0
feat(di): wire rate limit service and middleware into DI container
AchoArnold Jun 30, 2026
27455a2
fix: add graceful shutdown for RateLimitService and handle Redis noti…
AchoArnold Jun 30, 2026
3a47dea
feat(services): add 1s Redis timeout, fail-open behavior, and shutdow…
AchoArnold Jun 30, 2026
cc89b65
refactor: use Fiber OnPreShutdown hook for RateLimitService cleanup
AchoArnold Jun 30, 2026
d270183
refactor: remove nil checks for logger in RateLimitService, use OnPre…
AchoArnold Jun 30, 2026
07461d8
refactor: gate rate limiting at container level, skip middleware enti…
AchoArnold Jun 30, 2026
4b25d06
refactor: rename rate limiter service
AchoArnold Jun 30, 2026
a143377
perf: batch all Redis flush operations into a single pipeline call
AchoArnold Jun 30, 2026
ba7ced3
refactor(api): use ctxLogger with span context in RateLimitService
AchoArnold Jun 30, 2026
b6656ee
refactor: improve logging
AchoArnold Jun 30, 2026
86213a3
fix(api): integrate rate tracking with authentication and graceful sh…
AchoArnold Nov 17, 2026
bbc8fa8
feat(api): enforce atomic per-user hourly request budgets
AchoArnold Nov 17, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions api/.env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ DATABASE_URL_DEDICATED=postgresql://dbusername:dbpassword@postgres:5432/httpsms
# Redis connection string
REDIS_URL=redis://@redis:6379

# 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.
GCS_BUCKET_NAME=

Expand Down
2 changes: 2 additions & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
16 changes: 15 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"))
}
}
93 changes: 88 additions & 5 deletions api/pkg/di/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"

"cloud.google.com/go/compute/metadata"
Expand Down Expand Up @@ -98,6 +99,11 @@ type Container struct {
phoneRistrettoCache *ristretto.Cache[string, *entities.Phone]
contactRistrettoCache *ristretto.Cache[string, services.ContactCacheEntry]
inMemoryCache cache.Cache
rateLimitService *services.RateLimitService
rateLimitMiddleware fiber.Handler
redisClient *redis.Client
rateLimitRedisClient *redis.Client
closeOnce sync.Once
}

// NewLiteContainer creates a Container without any routes or listeners
Expand All @@ -107,6 +113,20 @@ func NewLiteContainer() (container *Container) {
}
}

// Close gracefully shuts down container resources
func (container *Container) Close() {
container.closeOnce.Do(func() {
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")
}
})
}

// NewContainer creates a new dependency injection container
func NewContainer(projectID string, version string) (container *Container) {
container = &Container{
Expand Down Expand Up @@ -209,6 +229,20 @@ func (container *Container) App() (app *fiber.App) {
app.Use(middlewares.BearerAuth(container.Logger(), container.Tracer(), container.FirebaseAuthClient()))
app.Use(middlewares.APIKeyAuth(container.Logger(), container.Tracer(), container.UserRepository()))

if os.Getenv("RATE_LIMIT_ENABLED") == "true" {
app.Hooks().OnPreShutdown(func() error {
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
})
}

container.app = app
return app
}
Expand All @@ -228,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
Expand Down Expand Up @@ -451,6 +495,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")))
Expand All @@ -461,19 +515,48 @@ 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")
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.rateLimitRedisClient,
)
return container.rateLimitService
}

// FirebaseAuthClient creates a new instance of auth.Client
Expand Down
60 changes: 60 additions & 0 deletions api/pkg/di/container_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,75 @@
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")
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.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()
Expand Down
5 changes: 4 additions & 1 deletion api/pkg/middlewares/authenticated_middlesare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -31,6 +31,9 @@ func Authenticated(tracer telemetry.Tracer) fiber.Handler {
})
}

if afterAuth != nil {
return afterAuth(c)
}
return c.Next()
}
}
Loading
Loading