@@ -23,6 +23,7 @@ import (
23
23
"github.com/optimizely/sidedoor/pkg/api/handlers"
24
24
"github.com/optimizely/sidedoor/pkg/api/middleware"
25
25
"github.com/optimizely/sidedoor/pkg/optimizely"
26
+ "github.com/spf13/viper"
26
27
27
28
"github.com/go-chi/chi"
28
29
chimw "github.com/go-chi/chi/middleware"
@@ -31,6 +32,7 @@ import (
31
32
32
33
// RouterOptions defines the configuration parameters for Router.
33
34
type RouterOptions struct {
35
+ maxConns int
34
36
middleware middleware.OptlyMiddleware
35
37
featureAPI handlers.FeatureAPI
36
38
userEventAPI handlers.UserEventAPI
@@ -40,6 +42,7 @@ type RouterOptions struct {
40
42
// NewDefaultRouter creates a new router with the default backing optimizely.Cache
41
43
func NewDefaultRouter (optlyCache optimizely.Cache ) http.Handler {
42
44
spec := & RouterOptions {
45
+ maxConns : viper .GetInt ("api.maxconns" ),
43
46
middleware : & middleware.CachedOptlyMiddleware {Cache : optlyCache },
44
47
featureAPI : new (handlers.FeatureHandler ),
45
48
userEventAPI : new (handlers.UserEventHandler ),
@@ -53,6 +56,11 @@ func NewDefaultRouter(optlyCache optimizely.Cache) http.Handler {
53
56
func NewRouter (opt * RouterOptions ) * chi.Mux {
54
57
r := chi .NewRouter ()
55
58
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
+
56
64
r .Use (middleware .SetTime )
57
65
r .Use (render .SetContentType (render .ContentTypeJSON ), middleware .SetRequestID )
58
66
0 commit comments