Skip to content

Commit

Permalink
bfgd, electrumx: make initial and max conns configurable (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasing authored Aug 21, 2024
1 parent 348479f commit 6b21809
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
12 changes: 12 additions & 0 deletions cmd/bfgd/bfgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/extool/extool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 13 additions & 9 deletions docker/bfgd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
5 changes: 5 additions & 0 deletions hemi/electrumx/conn_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 2 additions & 8 deletions hemi/electrumx/electrumx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -86,6 +88,8 @@ type btcClient interface {
type Config struct {
BTCStartHeight uint64
EXBTCAddress string
EXBTCInitialConns int
EXBTCMaxConns int
PrivateListenAddress string
PublicListenAddress string
LogLevel string
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 6b21809

Please sign in to comment.