@@ -23,6 +23,7 @@ import (
2323 "github.com/optimizely/sidedoor/pkg/api/handlers"
2424 "github.com/optimizely/sidedoor/pkg/api/middleware"
2525 "github.com/optimizely/sidedoor/pkg/optimizely"
26+ "github.com/spf13/viper"
2627
2728 "github.com/go-chi/chi"
2829 chimw "github.com/go-chi/chi/middleware"
@@ -31,6 +32,7 @@ import (
3132
3233// RouterOptions defines the configuration parameters for Router.
3334type RouterOptions struct {
35+ maxConns int
3436 middleware middleware.OptlyMiddleware
3537 featureAPI handlers.FeatureAPI
3638 userEventAPI handlers.UserEventAPI
@@ -40,6 +42,7 @@ type RouterOptions struct {
4042// NewDefaultRouter creates a new router with the default backing optimizely.Cache
4143func NewDefaultRouter (optlyCache optimizely.Cache ) http.Handler {
4244 spec := & RouterOptions {
45+ maxConns : viper .GetInt ("api.maxconns" ),
4346 middleware : & middleware.CachedOptlyMiddleware {Cache : optlyCache },
4447 featureAPI : new (handlers.FeatureHandler ),
4548 userEventAPI : new (handlers.UserEventHandler ),
@@ -53,6 +56,11 @@ func NewDefaultRouter(optlyCache optimizely.Cache) http.Handler {
5356func NewRouter (opt * RouterOptions ) * chi.Mux {
5457 r := chi .NewRouter ()
5558
59+ if opt .maxConns > 0 {
60+ // Note this is NOT a rate limiter, but a concurrency threshold
61+ r .Use (chimw .Throttle (opt .maxConns ))
62+ }
63+
5664 r .Use (middleware .SetTime )
5765 r .Use (render .SetContentType (render .ContentTypeJSON ), middleware .SetRequestID )
5866
0 commit comments