Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request limit and timeout to bfgd config #200

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions api/bfgapi/bfgapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ var (
DefaultPrometheusListen = "localhost:2112"
DefaultPrivateURL = "ws://" + DefaultPrivateListen + "/" + RouteWebsocketPrivate
DefaultPublicURL = "ws://" + DefaultPublicListen + "/" + RouteWebsocketPublic
DefaultRequestLimit = 10000 // XXX this is a bandaid
DefaultRequestTimeout = 9 // XXX PNOOMA
)

type AccessPublicKey struct {
Expand Down
12 changes: 12 additions & 0 deletions cmd/bfgd/bfgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ var (
Help: "address and port bfgd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
"BFG_REQUEST_LIMIT": config.Config{
Value: &cfg.RequestLimit,
DefaultValue: bfgapi.DefaultRequestLimit,
Help: "maximum request queue depth",
Print: config.PrintAll,
},
"BFG_REQUEST_TIMEOUT": config.Config{
Value: &cfg.RequestTimeout,
DefaultValue: bfgapi.DefaultRequestTimeout,
Help: "request timeout in seconds",
Print: config.PrintAll,
},
}
)

Expand Down
2 changes: 2 additions & 0 deletions e2e/e2e_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ func createBfgServerWithAuth(ctx context.Context, t *testing.T, pgUri string, el
EXBTCAddress: electrumxAddr,
BTCStartHeight: btcStartHeight,
PublicKeyAuth: auth,
RequestLimit: bfgapi.DefaultRequestLimit,
RequestTimeout: bfgapi.DefaultRequestTimeout,
})
if err != nil {
t.Fatal(err)
Expand Down
26 changes: 17 additions & 9 deletions service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func NewDefaultConfig() *Config {
EXBTCAddress: "localhost:18001",
PrivateListenAddress: ":8080",
PublicListenAddress: ":8383",
RequestLimit: bfgapi.DefaultRequestLimit,
RequestTimeout: bfgapi.DefaultRequestTimeout,
}
}

Expand All @@ -91,6 +93,8 @@ type Config struct {
PrometheusListenAddress string
PprofListenAddress string
PublicKeyAuth bool
RequestLimit int
RequestTimeout int // in seconds
}

type Server struct {
Expand All @@ -100,9 +104,8 @@ type Server struct {
cfg *Config

// requests
requestLimit int // Request limiter queue depth
requestLimiter chan bool // Maximum in progress websocket commands
requestTimeout time.Duration
// requestTimeout time.Duration

btcHeight uint64

Expand Down Expand Up @@ -187,21 +190,25 @@ func NewServer(cfg *Config) (*Server, error) {
if cfg == nil {
cfg = NewDefaultConfig()
}
defaultRequestTimeout := 9 * time.Second // XXX
requestLimit := 1000 // XXX
if cfg.RequestLimit <= 0 {
return nil, fmt.Errorf("invalid request limit: %v", cfg.RequestLimit)
}
minRequestTimeout := 3
if cfg.RequestTimeout <= minRequestTimeout {
return nil, fmt.Errorf("invalid request timeout (minimum %v): %v",
minRequestTimeout, cfg.RequestTimeout)
}
s := &Server{
cfg: cfg,
requestLimiter: make(chan bool, requestLimit),
requestLimit: requestLimit,
requestTimeout: defaultRequestTimeout,
requestLimiter: make(chan bool, cfg.RequestLimit),
btcHeight: cfg.BTCStartHeight,
server: http.NewServeMux(),
publicServer: http.NewServeMux(),
metrics: newMetrics(),
sessions: make(map[string]*bfgWs),
checkForInvalidBlocks: make(chan struct{}),
}
for range requestLimit {
for range cfg.RequestLimit {
s.requestLimiter <- true
}

Expand Down Expand Up @@ -251,7 +258,8 @@ func (s *Server) handleRequest(parentCtx context.Context, bws *bfgWs, wsid strin
log.Tracef("handleRequest: %v", bws.addr)
defer log.Tracef("handleRequest exit: %v", bws.addr)

ctx, cancel := context.WithTimeout(parentCtx, s.requestTimeout)
ctx, cancel := context.WithTimeout(parentCtx,
time.Duration(s.cfg.RequestTimeout)*time.Second)
defer cancel()

select {
Expand Down