Skip to content

Commit b3cf02d

Browse files
authored
Added http timeout as a config parameter (#1055)
* added http timeout as a config parameter * Fixed and Added tests * added httpTimeout in config * Added logFile config parameters to readMe * Fixed comments
1 parent 7c51e97 commit b3cf02d

15 files changed

+315
-166
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,29 @@ There are a set of parameters that are configurable. These include:
109109
- Log Level: Normally debug logs are not logged into the log file. But if you want you can set `logLevel` to `debug` and fetch the debug logs.
110110
- Gas Limit: The value with which the gas limit will be multiplied while sending every transaction.
111111
- RPC Timeout: This is the threshold number of seconds after which any contract and client calls will time out.
112+
- HTTP Timeout: This is the threshold number of seconds after which an HTTP request for a job will time out.
113+
- Maximum size of log file: This is the maximum size of log file in MB
114+
- Maximum number of backups of log file: This is the maximum number of old log files to retain.
115+
- Maximum age of log file: This is the maximum number of days to retain old log files.
112116
113117
The config is set while the build is generated, but if you need to change any of the above parameter, you can use the `setConfig` command.
114118
115119
razor cli
116120
117121
```
118-
$ ./razor setConfig --provider <rpc_provider> --gasmultiplier <multiplier_value> --buffer <buffer_percentage> --wait <wait_for_n_blocks> --gasprice <gas_price> --logLevel <debug_or_info> --gasLimit <gas_limit_multiplier> --rpcTimeout <rpc_timeout>
122+
$ ./razor setConfig --provider <rpc_provider> --gasmultiplier <multiplier_value> --buffer <buffer_percentage> --wait <wait_for_n_blocks> --gasprice <gas_price> --logLevel <debug_or_info> --gasLimit <gas_limit_multiplier> --rpcTimeout <rpc_timeout> --httpTimeout <http_timeout> --logFileMaxSize <file_max_size> --logFileMaxBackups <file_max_backups> --logFileMaxAge <file_max_age>
119123
```
120124
121125
docker
122126
123127
```
124-
docker exec -it razor-go razor setConfig --provider <rpc_provider> --gasmultiplier <multiplier_value> --buffer <buffer_percentage> --wait <wait_for_n_blocks> --gasprice <gas_price> --logLevel <debug_or_info> --gasLimit <gas_limit_multiplier> --rpcTimeout <rpc_timeout>
128+
docker exec -it razor-go razor setConfig --provider <rpc_provider> --gasmultiplier <multiplier_value> --buffer <buffer_percentage> --wait <wait_for_n_blocks> --gasprice <gas_price> --logLevel <debug_or_info> --gasLimit <gas_limit_multiplier> --rpcTimeout <rpc_timeout> --httpTimeout <http_timeout> --logFileMaxSize <file_max_size> --logFileMaxBackups <file_max_backups> --logFileMaxAge <file_max_age>
125129
```
126130
127131
Example:
128132
129133
```
130-
$ ./razor setConfig --provider https://mainnet.skalenodes.com/v1/turbulent-unique-scheat --gasmultiplier 1 --buffer 20 --wait 30 --gasprice 0 --logLevel debug --gasLimit 2 --rpcTimeout 10
134+
$ ./razor setConfig --provider https://mainnet.skalenodes.com/v1/turbulent-unique-scheat --gasmultiplier 1 --buffer 20 --wait 30 --gasprice 0 --logLevel debug --gasLimit 2 --rpcTimeout 10 --httpTimeout 10 --logFileMaxSize 200 --logFileMaxBackups 52 --logFileMaxAge 365
131135
```
132136
133137
Other than setting these parameters in the config, you can use different values of these parameters in different command. Just add the same flag to any command you want to use and the new config changes will appear for that command.

cmd/config-utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func (*UtilsStruct) GetConfigData() (types.Configurations, error) {
2020
LogLevel: "",
2121
GasLimitMultiplier: 0,
2222
RPCTimeout: 0,
23+
HTTPTimeout: 0,
2324
LogFileMaxSize: 0,
2425
LogFileMaxBackups: 0,
2526
LogFileMaxAge: 0,
@@ -57,6 +58,10 @@ func (*UtilsStruct) GetConfigData() (types.Configurations, error) {
5758
if err != nil {
5859
return config, err
5960
}
61+
httpTimeout, err := cmdUtils.GetHTTPTimeout()
62+
if err != nil {
63+
return config, err
64+
}
6065
logFileMaxSize, err := cmdUtils.GetLogFileMaxSize()
6166
if err != nil {
6267
return config, err
@@ -78,6 +83,8 @@ func (*UtilsStruct) GetConfigData() (types.Configurations, error) {
7883
config.GasLimitMultiplier = gasLimit
7984
config.RPCTimeout = rpcTimeout
8085
utils.RPCTimeout = rpcTimeout
86+
config.HTTPTimeout = httpTimeout
87+
utils.HTTPTimeout = httpTimeout
8188
config.LogFileMaxSize = logFileMaxSize
8289
config.LogFileMaxBackups = logFileMaxBackups
8390
config.LogFileMaxAge = logFileMaxAge
@@ -225,6 +232,22 @@ func (*UtilsStruct) GetRPCTimeout() (int64, error) {
225232
return rpcTimeout, nil
226233
}
227234

235+
func (*UtilsStruct) GetHTTPTimeout() (int64, error) {
236+
httpTimeout, err := flagSetUtils.GetRootInt64HTTPTimeout()
237+
if err != nil {
238+
return int64(core.DefaultHTTPTimeout), err
239+
}
240+
if httpTimeout == 0 {
241+
if viper.IsSet("httpTimeout") {
242+
httpTimeout = viper.GetInt64("httpTimeout")
243+
} else {
244+
httpTimeout = int64(core.DefaultRPCTimeout)
245+
log.Debug("HTTPTimeout is not set, taking its default value ", httpTimeout)
246+
}
247+
}
248+
return httpTimeout, nil
249+
}
250+
228251
func (*UtilsStruct) GetLogFileMaxSize() (int, error) {
229252
logFileMaxSize, err := flagSetUtils.GetRootIntLogFileMaxSize()
230253
if err != nil {

cmd/config-utils_test.go

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import (
99
)
1010

1111
func TestGetConfigData(t *testing.T) {
12-
config := types.Configurations{
12+
nilConfig := types.Configurations{
1313
Provider: "",
1414
GasMultiplier: 0,
1515
BufferPercent: 0,
1616
WaitTime: 0,
1717
LogLevel: "",
1818
GasLimitMultiplier: 0,
1919
RPCTimeout: 0,
20+
HTTPTimeout: 0,
21+
LogFileMaxSize: 0,
22+
LogFileMaxBackups: 0,
23+
LogFileMaxAge: 0,
2024
}
2125

2226
configData := types.Configurations{
@@ -27,6 +31,7 @@ func TestGetConfigData(t *testing.T) {
2731
LogLevel: "debug",
2832
GasLimitMultiplier: 3,
2933
RPCTimeout: 10,
34+
HTTPTimeout: 10,
3035
LogFileMaxSize: 5,
3136
LogFileMaxBackups: 10,
3237
LogFileMaxAge: 30,
@@ -48,6 +53,8 @@ func TestGetConfigData(t *testing.T) {
4853
gasLimit float32
4954
rpcTimeout int64
5055
rpcTimeoutErr error
56+
httpTimeout int64
57+
httpTimeoutErr error
5158
gasLimitErr error
5259
logFileMaxSize int
5360
logFileMaxSizeErr error
@@ -72,6 +79,7 @@ func TestGetConfigData(t *testing.T) {
7279
logLevel: "debug",
7380
gasLimit: 3,
7481
rpcTimeout: 10,
82+
httpTimeout: 10,
7583
logFileMaxSize: 5,
7684
logFileMaxBackups: 10,
7785
logFileMaxAge: 30,
@@ -84,65 +92,73 @@ func TestGetConfigData(t *testing.T) {
8492
args: args{
8593
providerErr: errors.New("provider error"),
8694
},
87-
want: config,
95+
want: nilConfig,
8896
wantErr: errors.New("provider error"),
8997
},
9098
{
9199
name: "Test 3: When there is an error in getting gasMultiplier",
92100
args: args{
93101
gasMultiplierErr: errors.New("gasMultiplier error"),
94102
},
95-
want: config,
103+
want: nilConfig,
96104
wantErr: errors.New("gasMultiplier error"),
97105
},
98106
{
99107
name: "Test 4: When there is an error in getting bufferPercent",
100108
args: args{
101109
bufferPercentErr: errors.New("bufferPercent error"),
102110
},
103-
want: config,
111+
want: nilConfig,
104112
wantErr: errors.New("bufferPercent error"),
105113
},
106114
{
107115
name: "Test 5: When there is an error in getting waitTime",
108116
args: args{
109117
waitTimeErr: errors.New("waitTime error"),
110118
},
111-
want: config,
119+
want: nilConfig,
112120
wantErr: errors.New("waitTime error"),
113121
},
114122
{
115123
name: "Test 6: When there is an error in getting gasPrice",
116124
args: args{
117125
gasPriceErr: errors.New("gasPrice error"),
118126
},
119-
want: config,
127+
want: nilConfig,
120128
wantErr: errors.New("gasPrice error"),
121129
},
122130
{
123131
name: "Test 7: When there is an error in getting logLevel",
124132
args: args{
125133
logLevelErr: errors.New("logLevel error"),
126134
},
127-
want: config,
135+
want: nilConfig,
128136
wantErr: errors.New("logLevel error"),
129137
},
130138
{
131139
name: "Test 8: When there is an error in getting gasLimit",
132140
args: args{
133141
gasLimitErr: errors.New("gasLimit error"),
134142
},
135-
want: config,
143+
want: nilConfig,
136144
wantErr: errors.New("gasLimit error"),
137145
},
138146
{
139147
name: "Test 9: When there is an error in getting rpcTimeout",
140148
args: args{
141149
rpcTimeoutErr: errors.New("rpcTimeout error"),
142150
},
143-
want: config,
151+
want: nilConfig,
144152
wantErr: errors.New("rpcTimeout error"),
145153
},
154+
{
155+
name: "Test 10: When there is an error in getting httpTimeout",
156+
args: args{
157+
httpTimeoutErr: errors.New("httpTimeout error"),
158+
},
159+
want: nilConfig,
160+
wantErr: errors.New("httpTimeout error"),
161+
},
146162
}
147163
for _, tt := range tests {
148164
t.Run(tt.name, func(t *testing.T) {
@@ -156,6 +172,7 @@ func TestGetConfigData(t *testing.T) {
156172
cmdUtilsMock.On("GetGasLimit").Return(tt.args.gasLimit, tt.args.gasLimitErr)
157173
cmdUtilsMock.On("GetBufferPercent").Return(tt.args.bufferPercent, tt.args.bufferPercentErr)
158174
cmdUtilsMock.On("GetRPCTimeout").Return(tt.args.rpcTimeout, tt.args.rpcTimeoutErr)
175+
cmdUtilsMock.On("GetHTTPTimeout").Return(tt.args.httpTimeout, tt.args.httpTimeoutErr)
159176
cmdUtilsMock.On("GetLogFileMaxSize").Return(tt.args.logFileMaxSize, tt.args.logFileMaxSizeErr)
160177
cmdUtilsMock.On("GetLogFileMaxBackups").Return(tt.args.logFileMaxBackups, tt.args.logFileMaxBackupsErr)
161178
cmdUtilsMock.On("GetLogFileMaxAge").Return(tt.args.logFileMaxAge, tt.args.logFileMaxAgeErr)
@@ -664,3 +681,62 @@ func TestGetRPCTimeout(t *testing.T) {
664681
})
665682
}
666683
}
684+
685+
func TestGetHTTPTimeout(t *testing.T) {
686+
type args struct {
687+
httpTimeout int64
688+
httpTimeoutErr error
689+
}
690+
tests := []struct {
691+
name string
692+
args args
693+
want int64
694+
wantErr error
695+
}{
696+
{
697+
name: "Test 1: When getHTTPTimeout function executes successfully",
698+
args: args{
699+
httpTimeout: 12,
700+
},
701+
want: 12,
702+
wantErr: nil,
703+
},
704+
{
705+
name: "Test 2: When httpTimeout is 0",
706+
args: args{
707+
httpTimeout: 0,
708+
},
709+
want: 10,
710+
wantErr: nil,
711+
},
712+
{
713+
name: "Test 3: When there is an error in getting httpTimeout",
714+
args: args{
715+
httpTimeoutErr: errors.New("httpTimeout error"),
716+
},
717+
want: 10,
718+
wantErr: errors.New("httpTimeout error"),
719+
},
720+
}
721+
for _, tt := range tests {
722+
t.Run(tt.name, func(t *testing.T) {
723+
SetUpMockInterfaces()
724+
725+
flagSetMock.On("GetRootInt64HTTPTimeout").Return(tt.args.httpTimeout, tt.args.httpTimeoutErr)
726+
utils := &UtilsStruct{}
727+
got, err := utils.GetHTTPTimeout()
728+
if got != tt.want {
729+
t.Errorf("getHTTPTimeout() got = %v, want %v", got, tt.want)
730+
}
731+
if err == nil || tt.wantErr == nil {
732+
if err != tt.wantErr {
733+
t.Errorf("Error for getHTTPTimeout function, got = %v, want = %v", err, tt.wantErr)
734+
}
735+
} else {
736+
if err.Error() != tt.wantErr.Error() {
737+
t.Errorf("Error for getHTTPTimeout function, got = %v, want = %v", err, tt.wantErr)
738+
}
739+
}
740+
})
741+
}
742+
}

cmd/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ type FlagSetInterface interface {
118118
GetFloat32GasLimit(flagSet *pflag.FlagSet) (float32, error)
119119
GetStringLogLevel(flagSet *pflag.FlagSet) (string, error)
120120
GetInt64RPCTimeout(flagSet *pflag.FlagSet) (int64, error)
121+
GetInt64HTTPTimeout(flagSet *pflag.FlagSet) (int64, error)
121122
GetUint32BountyId(flagSet *pflag.FlagSet) (uint32, error)
122123
GetRootStringProvider() (string, error)
123124
GetRootFloat32GasMultiplier() (float32, error)
@@ -127,6 +128,7 @@ type FlagSetInterface interface {
127128
GetRootStringLogLevel() (string, error)
128129
GetRootFloat32GasLimit() (float32, error)
129130
GetRootInt64RPCTimeout() (int64, error)
131+
GetRootInt64HTTPTimeout() (int64, error)
130132
GetRootIntLogFileMaxSize() (int, error)
131133
GetRootIntLogFileMaxBackups() (int, error)
132134
GetRootIntLogFileMaxAge() (int, error)
@@ -170,6 +172,7 @@ type UtilsCmdInterface interface {
170172
GetGasLimit() (float32, error)
171173
GetBufferPercent() (int32, error)
172174
GetRPCTimeout() (int64, error)
175+
GetHTTPTimeout() (int64, error)
173176
GetLogFileMaxSize() (int, error)
174177
GetLogFileMaxBackups() (int, error)
175178
GetLogFileMaxAge() (int, error)

cmd/mocks/flag_set_interface.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mocks/utils_cmd_interface.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)