Skip to content

Commit a7acb64

Browse files
authored
Merge pull request #713 from Miraclechukwuemeka/feat/per-endpoint-concurrency
feat: add per-endpoint concurrency ceiling with shed policy
2 parents 8642e49 + a4683a1 commit a7acb64

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

internal/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ type Config struct {
132132
// in-flight requests to complete before forcing shutdown. Env:
133133
// GRACEFUL_SHUTDOWN_TIMEOUT (default: DefaultGracefulShutdownTimeout).
134134
GracefulShutdownTimeout int // seconds
135+
// ConcurrencyCapsPath is the path to the per-endpoint concurrency caps YAML
136+
// configuration file. When empty, concurrency shedding is disabled.
137+
// Env: CONCURRENCY_CAPS_PATH (default: "" — disabled; set to deploy/concurrency-caps.yaml to enable)
138+
ConcurrencyCapsPath string
139+
140+
// OTelLogsEnabled toggles OpenTelemetry log export (env: OTEL_LOGS_ENABLED).
141+
OTelLogsEnabled bool
135142
}
136143

137144
// ValidationResult holds the result of configuration validation
@@ -282,6 +289,7 @@ func Load(opts ...Option) (Config, error) {
282289
DBStatementCacheMode: DefaultDBStatementCacheMode,
283290
PgBouncerIdleInTxTimeout: DefaultPgBouncerIdleInTxTimeout,
284291
GracefulShutdownTimeout: DefaultGracefulShutdownTimeout,
292+
ConcurrencyCapsPath: getEnv("CONCURRENCY_CAPS_PATH", ""),
285293
}
286294

287295
// Resolve secrets through the provider

internal/routes/routes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func Register(r *gin.Engine) {
5252
r.Use(middleware.TailSamplingSignals())
5353
r.Use(middleware.TraceIDMiddleware())
5454

55+
// Per-endpoint concurrency shedding — shed excess load before rate limiting
56+
if cfg.ConcurrencyCapsPath != "" {
57+
concCfg, err := middleware.LoadConcurrencyConfig(cfg.ConcurrencyCapsPath)
58+
if err != nil {
59+
fmt.Printf("WARNING: failed to load concurrency caps config from %s: %v\n", cfg.ConcurrencyCapsPath, err)
60+
} else {
61+
r.Use(middleware.InflightMiddleware(concCfg))
62+
}
63+
}
64+
5565
// Rate limiting
5666
rateLimitConfig := middleware.RateLimiterConfig{
5767
Enabled: cfg.RateLimitEnabled,

0 commit comments

Comments
 (0)