From 5e2325db7fc86a7c0bc1e24868750ac35f0595a4 Mon Sep 17 00:00:00 2001 From: Marco Peereboom Date: Tue, 13 Aug 2024 10:31:04 +0100 Subject: [PATCH 1/2] Add request limit and timeout to bfg config --- api/bfgapi/bfgapi.go | 2 ++ cmd/bfgd/bfgd.go | 12 ++++++++++++ service/bfg/bfg.go | 26 +++++++++++++++++--------- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/api/bfgapi/bfgapi.go b/api/bfgapi/bfgapi.go index eeb4c9cb..722567a6 100644 --- a/api/bfgapi/bfgapi.go +++ b/api/bfgapi/bfgapi.go @@ -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 { diff --git a/cmd/bfgd/bfgd.go b/cmd/bfgd/bfgd.go index 66fb668d..3abaccde 100644 --- a/cmd/bfgd/bfgd.go +++ b/cmd/bfgd/bfgd.go @@ -87,6 +87,18 @@ var ( Help: "address and port bfgd pprof listens on (open
/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, + }, } ) diff --git a/service/bfg/bfg.go b/service/bfg/bfg.go index fa307a1e..1f6c8fe0 100644 --- a/service/bfg/bfg.go +++ b/service/bfg/bfg.go @@ -65,6 +65,8 @@ func NewDefaultConfig() *Config { EXBTCAddress: "localhost:18001", PrivateListenAddress: ":8080", PublicListenAddress: ":8383", + RequestLimit: bfgapi.DefaultRequestLimit, + RequestTimeout: bfgapi.DefaultRequestTimeout, } } @@ -91,6 +93,8 @@ type Config struct { PrometheusListenAddress string PprofListenAddress string PublicKeyAuth bool + RequestLimit int + RequestTimeout int // in seconds } type Server struct { @@ -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 @@ -187,13 +190,17 @@ 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(), @@ -201,7 +208,7 @@ func NewServer(cfg *Config) (*Server, error) { sessions: make(map[string]*bfgWs), checkForInvalidBlocks: make(chan struct{}), } - for range requestLimit { + for range cfg.RequestLimit { s.requestLimiter <- true } @@ -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 { From df4ef99bb584d7a932502dcc36a71b8293c48446 Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 13 Aug 2024 19:56:44 +1000 Subject: [PATCH 2/2] e2e: add request{limit,timeout} to bfg config --- e2e/e2e_ext_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/e2e_ext_test.go b/e2e/e2e_ext_test.go index 7766faac..ee37de96 100644 --- a/e2e/e2e_ext_test.go +++ b/e2e/e2e_ext_test.go @@ -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)