Skip to content

Commit

Permalink
Make gateway timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Jan 31, 2024
1 parent 3ff74a4 commit 41c7408
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
dbCacheSizeF = "db-cache-size"
dbMaxHandlesF = "db-max-handles"
gwAPIKeyF = "gw-api-key" //nolint: gosec
gwTimeoutF = "gw-timeout" //nolint: gosec
cnNameF = "cn-name"
cnFeederURLF = "cn-feeder-url"
cnGatewayURLF = "cn-gateway-url"
Expand Down Expand Up @@ -107,6 +108,7 @@ const (
defaultCNL2ChainID = ""
defaultCNCoreContractAddressStr = ""
defaultCallMaxSteps = 4_000_000
defaultGwTimeout = 5 * time.Second

configFlagUsage = "The yaml configuration file."
logLevelFlagUsage = "Options: debug, info, warn, error."
Expand Down Expand Up @@ -148,6 +150,7 @@ const (
dbCacheSizeUsage = "Determines the amount of memory (in megabytes) allocated for caching data in the database."
dbMaxHandlesUsage = "A soft limit on the number of open files that can be used by the DB"
gwAPIKeyUsage = "API key for gateway endpoints to avoid throttling" //nolint: gosec
gwTimeoutUsage = "Timeout for requests made to the gateway" //nolint: gosec
callMaxStepsUsage = "Maximum number of steps to be executed in starknet_call requests"
)

Expand Down Expand Up @@ -324,6 +327,7 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
junoCmd.MarkFlagsRequiredTogether(cnNameF, cnFeederURLF, cnGatewayURLF, cnL1ChainIDF, cnL2ChainIDF, cnCoreContractAddressF, cnUnverifiableRangeF) //nolint:lll
junoCmd.MarkFlagsMutuallyExclusive(networkF, cnNameF)
junoCmd.Flags().Uint(callMaxStepsF, defaultCallMaxSteps, callMaxStepsUsage)
junoCmd.Flags().Duration(gwTimeoutF, defaultGwTimeout, gwTimeoutUsage)

return junoCmd
}
16 changes: 16 additions & 0 deletions cmd/juno/juno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestConfigPrecedence(t *testing.T) {
defaultMaxCacheSize := uint(8)
defaultMaxHandles := 1024
defaultCallMaxSteps := uint(4_000_000)
defaultGwTimeout := 5 * time.Second

tests := map[string]struct {
cfgFile bool
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestConfigPrecedence(t *testing.T) {
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"custom network config file": {
Expand Down Expand Up @@ -152,6 +154,7 @@ cn-unverifiable-range: [0,10]
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"default config with no flags": {
Expand Down Expand Up @@ -183,6 +186,7 @@ cn-unverifiable-range: [0,10]
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"config file path is empty string": {
Expand Down Expand Up @@ -214,6 +218,7 @@ cn-unverifiable-range: [0,10]
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"config file doesn't exist": {
Expand Down Expand Up @@ -250,6 +255,7 @@ cn-unverifiable-range: [0,10]
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"config file with all settings but without any other flags": {
Expand Down Expand Up @@ -288,6 +294,7 @@ pprof: true
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"config file with some settings but without any other flags": {
Expand Down Expand Up @@ -323,6 +330,7 @@ http-port: 4576
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"all flags without config file": {
Expand Down Expand Up @@ -356,6 +364,7 @@ http-port: 4576
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"some flags without config file": {
Expand Down Expand Up @@ -390,6 +399,7 @@ http-port: 4576
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"all setting set in both config file and flags": {
Expand Down Expand Up @@ -448,6 +458,7 @@ db-cache-size: 8
DBCacheSize: 9,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"some setting set in both config file and flags": {
Expand Down Expand Up @@ -485,6 +496,7 @@ network: goerli
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"some setting set in default, config file and flags": {
Expand Down Expand Up @@ -518,6 +530,7 @@ network: goerli
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"only set env variables": {
Expand Down Expand Up @@ -549,6 +562,7 @@ network: goerli
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"some setting set in both env variables and flags": {
Expand Down Expand Up @@ -581,6 +595,7 @@ network: goerli
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
"some setting set in both env variables and config file": {
Expand Down Expand Up @@ -614,6 +629,7 @@ network: goerli
GatewayAPIKey: "apikey",
DBMaxHandles: defaultMaxHandles,
RPCCallMaxSteps: defaultCallMaxSteps,
GatewayTimeout: defaultGwTimeout,
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ type Config struct {
DBCacheSize uint `mapstructure:"db-cache-size"`
DBMaxHandles int `mapstructure:"db-max-handles"`

GatewayAPIKey string `mapstructure:"gw-api-key"`
GatewayAPIKey string `mapstructure:"gw-api-key"`
GatewayTimeout time.Duration `mapstructure:"gw-timeout"`
}

type Node struct {
Expand Down Expand Up @@ -137,9 +138,8 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
}
}

feederClientTimeout := 5 * time.Second
client := feeder.NewClient(cfg.Network.FeederURL).WithUserAgent(ua).WithLogger(log).
WithTimeout(feederClientTimeout).WithAPIKey(cfg.GatewayAPIKey)
WithTimeout(cfg.GatewayTimeout).WithAPIKey(cfg.GatewayAPIKey)
synchronizer := sync.New(chain, adaptfeeder.New(client), log, cfg.PendingPollInterval, dbIsRemote)
services = append(services, synchronizer)
gatewayClient := gateway.NewClient(cfg.Network.GatewayURL, log).WithUserAgent(ua).WithAPIKey(cfg.GatewayAPIKey)
Expand Down

0 comments on commit 41c7408

Please sign in to comment.