Skip to content

Commit

Permalink
Add limit on number of horizon requests in flight
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Mar 13, 2024
1 parent 4b0b078 commit afac1df
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions services/horizon/internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ func (a *App) init() error {
SSEUpdateFrequency: a.config.SSEUpdateFrequency,
StaleThreshold: a.config.StaleThreshold,
ConnectionTimeout: a.config.ConnectionTimeout,
MaxConcurrentRequests: a.config.MaxConcurrentRequests,
MaxHTTPRequestSize: a.config.MaxHTTPRequestSize,
NetworkPassphrase: a.config.NetworkPassphrase,
MaxPathLength: a.config.MaxPathLength,
Expand Down
11 changes: 6 additions & 5 deletions services/horizon/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ type Config struct {
SSEUpdateFrequency time.Duration
ConnectionTimeout time.Duration
// MaxHTTPRequestSize is the maximum allowed request payload size
MaxHTTPRequestSize uint
RateQuota *throttled.RateQuota
FriendbotURL *url.URL
LogLevel logrus.Level
LogFile string
MaxHTTPRequestSize uint
RateQuota *throttled.RateQuota
MaxConcurrentRequests uint
FriendbotURL *url.URL
LogLevel logrus.Level
LogFile string

// MaxPathLength is the maximum length of the path returned by `/paths` endpoint.
MaxPathLength uint
Expand Down
12 changes: 11 additions & 1 deletion services/horizon/internal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const (
// StellarTestnet is a constant representing the Stellar test network
StellarTestnet = "testnet"

defaultMaxHTTPRequestSize = uint(200 * 1024)
defaultMaxConcurrentRequests = 1000
defaultMaxHTTPRequestSize = uint(200 * 1024)
)

var (
Expand Down Expand Up @@ -445,6 +446,15 @@ func Flags() (*Config, support.ConfigOptions) {
Usage: "sets the limit on the maximum allowed http request payload size, default is 200kb, to disable the limit check, set to 0, only do so if you acknowledge the implications of accepting unbounded http request payload sizes.",
UsedInCommands: ApiServerCommands,
},
&support.ConfigOption{
Name: "max-concurrent-requests",
ConfigKey: &config.MaxConcurrentRequests,
OptType: types.Uint,
FlagDefault: defaultMaxConcurrentRequests,
Usage: "sets the limit on the maximum number of concurrent http requests, default is 1000, to disable the limit set to 0. " +
"If Horizon receives a request which would exceed the limit of concurrent http requests, Horizon will respond with a 429 status code.",
UsedInCommands: ApiServerCommands,
},
&support.ConfigOption{
Name: "per-hour-rate-limit",
ConfigKey: &config.RateQuota,
Expand Down
12 changes: 8 additions & 4 deletions services/horizon/internal/httpx/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (
)

type RouterConfig struct {
DBSession db.SessionInterface
PrimaryDBSession db.SessionInterface
TxSubmitter *txsub.System
RateQuota *throttled.RateQuota
DBSession db.SessionInterface
PrimaryDBSession db.SessionInterface
TxSubmitter *txsub.System
RateQuota *throttled.RateQuota
MaxConcurrentRequests uint

BehindCloudflare bool
BehindAWSLoadBalancer bool
Expand Down Expand Up @@ -90,6 +91,9 @@ func (r *Router) addMiddleware(config *RouterConfig,
BehindAWSLoadBalancer: config.BehindAWSLoadBalancer,
}))
r.Use(loggerMiddleware(serverMetrics))
if config.MaxConcurrentRequests > 0 {
r.Use(chimiddleware.Throttle(int(config.MaxConcurrentRequests)))

Check failure on line 95 in services/horizon/internal/httpx/router.go

View workflow job for this annotation

GitHub Actions / golangci

r.Use undefined (type *Router has no field or method Use) (typecheck)
}
r.Use(timeoutMiddleware(config.ConnectionTimeout))
if config.MaxHTTPRequestSize > 0 {
r.Use(func(handler http.Handler) http.Handler {
Expand Down

0 comments on commit afac1df

Please sign in to comment.