Skip to content

Commit

Permalink
[feat] Make memcached client timeout configurable
Browse files Browse the repository at this point in the history
The default value of `100ms` for `memcached` client timeout might
not always be sufficient.

Therefore, we would like to be able to fine-tune this parameter via
environment var.
  • Loading branch information
0x416e746f6e committed Apr 7, 2023
1 parent 84a9439 commit 650be2e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"syscall"

"github.com/bradfitz/gomemcache/memcache"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/flashbots/go-boost-utils/bls"
"github.com/flashbots/mev-boost-relay/beaconclient"
Expand Down Expand Up @@ -47,6 +48,7 @@ func init() {
apiCmd.Flags().StringSliceVar(&beaconNodeURIs, "beacon-uris", defaultBeaconURIs, "beacon endpoints")
apiCmd.Flags().StringVar(&redisURI, "redis-uri", defaultRedisURI, "redis uri")
apiCmd.Flags().StringVar(&postgresDSN, "db", defaultPostgresDSN, "PostgreSQL DSN")
apiCmd.Flags().DurationVar(&memcachedTimeout, "memcached-timeout", memcache.DefaultTimeout, "Specify memcached client timeout")
apiCmd.Flags().StringSliceVar(&memcachedURIs, "memcached-uris", defaultMemcachedURIs,
"Enable memcached, typically used as secondary backup to Redis for redundancy")
apiCmd.Flags().StringVar(&apiSecretKey, "secret-key", apiDefaultSecretKey, "secret key for signing bids")
Expand Down Expand Up @@ -104,7 +106,7 @@ var apiCmd = &cobra.Command{
var mem *datastore.Memcached
if len(memcachedURIs) > 0 {
log.Infof("Connecting to Memcached at %s ...", strings.Join(memcachedURIs, ", "))
mem, err = datastore.NewMemcached(networkInfo.Name, memcachedURIs...)
mem, err = datastore.NewMemcached(networkInfo.Name, memcachedTimeout, memcachedURIs...)
if err != nil {
log.WithError(err).Fatalf("Failed to connect to Memcached")
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"os"
"time"

"github.com/flashbots/mev-boost-relay/common"
)
Expand All @@ -15,10 +16,11 @@ var (
defaultLogJSON = os.Getenv("LOG_JSON") != ""
defaultLogLevel = common.GetEnv("LOG_LEVEL", "info")

beaconNodeURIs []string
redisURI string
postgresDSN string
memcachedURIs []string
beaconNodeURIs []string
redisURI string
postgresDSN string
memcachedTimeout time.Duration
memcachedURIs []string

logJSON bool
logLevel string
Expand Down
4 changes: 3 additions & 1 deletion datastore/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"time"

"github.com/bradfitz/gomemcache/memcache"
"github.com/flashbots/go-utils/cli"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (m *Memcached) GetExecutionPayload(slot uint64, proposerPubKey, blockHash s
return result, nil
}

func NewMemcached(prefix string, servers ...string) (*Memcached, error) {
func NewMemcached(prefix string, timeout time.Duration, servers ...string) (*Memcached, error) {
if len(servers) == 0 {
return nil, nil
}
Expand All @@ -89,6 +90,7 @@ func NewMemcached(prefix string, servers ...string) (*Memcached, error) {
if err := client.Ping(); err != nil {
return nil, err
}
client.Timeout = timeout

return &Memcached{client: client, keyPrefix: prefix}, nil
}
3 changes: 2 additions & 1 deletion datastore/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

consensusspec "github.com/attestantio/go-eth2-client/spec"
"github.com/bradfitz/gomemcache/memcache"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/flashbots/go-boost-utils/bls"
Expand Down Expand Up @@ -81,7 +82,7 @@ func initMemcached(t *testing.T) (mem *Memcached, err error) {
return
}

mem, err = NewMemcached("test", memcachedEndpoints...)
mem, err = NewMemcached("test", memcache.DefaultTimeout, memcachedEndpoints...)
if err != nil {
return
}
Expand Down

0 comments on commit 650be2e

Please sign in to comment.