From 6b218099f1dc05f66415a995a32777552b540d2f Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Thu, 22 Aug 2024 04:37:25 +1000 Subject: [PATCH] bfgd, electrumx: make initial and max conns configurable (#206) --- cmd/bfgd/bfgd.go | 12 ++++++++++++ cmd/extool/extool.go | 2 +- docker/bfgd/Dockerfile | 22 +++++++++++++--------- hemi/electrumx/conn_pool_test.go | 5 +++++ hemi/electrumx/electrumx.go | 10 ++-------- service/bfg/bfg.go | 6 +++++- 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/cmd/bfgd/bfgd.go b/cmd/bfgd/bfgd.go index 9c33073b..d743f60b 100644 --- a/cmd/bfgd/bfgd.go +++ b/cmd/bfgd/bfgd.go @@ -37,6 +37,18 @@ var ( Help: "electrumx endpoint", Print: config.PrintAll, }, + "BFG_EXBTC_INITIAL_CONNECTIONS": config.Config{ + Value: &cfg.EXBTCInitialConns, + DefaultValue: 5, + Help: "electrumx initial connections", + Print: config.PrintAll, + }, + "BFG_EXBTC_MAX_CONNECTIONS": config.Config{ + Value: &cfg.EXBTCMaxConns, + DefaultValue: 100, + Help: "electrumx max connections", + Print: config.PrintAll, + }, "BFG_PUBLIC_KEY_AUTH": config.Config{ Value: &cfg.PublicKeyAuth, DefaultValue: false, diff --git a/cmd/extool/extool.go b/cmd/extool/extool.go index 07676875..6572a09a 100644 --- a/cmd/extool/extool.go +++ b/cmd/extool/extool.go @@ -37,7 +37,7 @@ func main() { log.Fatal("No address specified") } - c, err := electrumx.NewClient(address) + c, err := electrumx.NewClient(address, 1, 1) if err != nil { log.Fatalf("Failed to create electrumx client: %v", err) } diff --git a/docker/bfgd/Dockerfile b/docker/bfgd/Dockerfile index aff38699..6097922f 100644 --- a/docker/bfgd/Dockerfile +++ b/docker/bfgd/Dockerfile @@ -55,15 +55,19 @@ COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo COPY --from=builder /build/bin/bfgd /usr/local/bin/bfgd # Environment variables -ENV BFG_EXBTC_ADDRESS= -ENV BFG_PUBLIC_KEY_AUTH= -ENV BFG_BTC_START_HEIGHT= -ENV BFG_LOG_LEVEL= -ENV BFG_POSTGRES_URI= -ENV BFG_PUBLIC_ADDRESS= -ENV BFG_PRIVATE_ADDRESS= -ENV BFG_PROMETHEUS_ADDRESS= -ENV BFG_PPROF_ADDRESS= +ENV BFG_EXBTC_ADDRESS="" +ENV BFG_EXBTC_INITIAL_CONNECTIONS="" +ENV BFG_EXBTC_MAX_CONNECTIONS="" +ENV BFG_PUBLIC_KEY_AUTH="" +ENV BFG_BTC_START_HEIGHT="" +ENV BFG_LOG_LEVEL="" +ENV BFG_POSTGRES_URI="" +ENV BFG_PUBLIC_ADDRESS="" +ENV BFG_PRIVATE_ADDRESS="" +ENV BFG_PROMETHEUS_ADDRESS="" +ENV BFG_PPROF_ADDRESS="" +ENV BFG_REQUEST_LIMIT="" +ENV BFG_REQUEST_TIMEOUT="" USER bfgd:bfgd WORKDIR /etc/bfgd/ diff --git a/hemi/electrumx/conn_pool_test.go b/hemi/electrumx/conn_pool_test.go index b093d446..3e72245e 100644 --- a/hemi/electrumx/conn_pool_test.go +++ b/hemi/electrumx/conn_pool_test.go @@ -10,6 +10,11 @@ import ( "time" ) +const ( + clientInitialConnections = 2 + clientMaximumConnections = 5 +) + func TestConnPool(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() diff --git a/hemi/electrumx/electrumx.go b/hemi/electrumx/electrumx.go index 2305371d..f2b32470 100644 --- a/hemi/electrumx/electrumx.go +++ b/hemi/electrumx/electrumx.go @@ -124,19 +124,13 @@ type Client struct { var log = loggo.GetLogger("electrumx") -const ( - clientInitialConnections = 2 - clientMaximumConnections = 5 -) - // NewClient returns an initialised electrumx client. -func NewClient(address string) (*Client, error) { +func NewClient(address string, initialConns, maxConns int) (*Client, error) { c := &Client{} // The address may be empty during tests, ignore empty addresses. if address != "" { - pool, err := newConnPool("tcp", address, - clientInitialConnections, clientMaximumConnections) + pool, err := newConnPool("tcp", address, initialConns, maxConns) if err != nil { return nil, fmt.Errorf("new connection pool: %w", err) } diff --git a/service/bfg/bfg.go b/service/bfg/bfg.go index 7b679c95..db6837eb 100644 --- a/service/bfg/bfg.go +++ b/service/bfg/bfg.go @@ -63,6 +63,8 @@ func init() { func NewDefaultConfig() *Config { return &Config{ EXBTCAddress: "localhost:18001", + EXBTCInitialConns: 5, + EXBTCMaxConns: 100, PrivateListenAddress: ":8080", PublicListenAddress: ":8383", RequestLimit: bfgapi.DefaultRequestLimit, @@ -86,6 +88,8 @@ type btcClient interface { type Config struct { BTCStartHeight uint64 EXBTCAddress string + EXBTCInitialConns int + EXBTCMaxConns int PrivateListenAddress string PublicListenAddress string LogLevel string @@ -213,7 +217,7 @@ func NewServer(cfg *Config) (*Server, error) { } var err error - s.btcClient, err = electrumx.NewClient(cfg.EXBTCAddress) + s.btcClient, err = electrumx.NewClient(cfg.EXBTCAddress, cfg.EXBTCInitialConns, cfg.EXBTCMaxConns) if err != nil { return nil, fmt.Errorf("create electrumx client: %w", err) }