Skip to content

Commit

Permalink
add rerun to api relay config (#233)
Browse files Browse the repository at this point in the history
* add rerun to api relay config

* upd config doc
  • Loading branch information
ylsGit committed Jul 8, 2024
1 parent abc644f commit a752112
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,7 @@ ApiKeys=[]
| - [Enabled](#RPC_ApiRelay_Enabled ) | No | boolean | No | - | - |
| - [DestURI](#RPC_ApiRelay_DestURI ) | No | string | No | - | - |
| - [RPCs](#RPC_ApiRelay_RPCs ) | No | array of string | No | - | - |
| - [Rerun](#RPC_ApiRelay_Rerun ) | No | boolean | No | - | - |

#### <a name="RPC_ApiRelay_Enabled"></a>8.30.1. `RPC.ApiRelay.Enabled`

Expand Down Expand Up @@ -2388,6 +2389,18 @@ DestURI=""
RPCs=[]
```

#### <a name="RPC_ApiRelay_Rerun"></a>8.30.4. `RPC.ApiRelay.Rerun`

**Type:** : `boolean`

**Default:** `false`

**Example setting the default value** (false):
```
[RPC.ApiRelay]
Rerun=false
```

## <a name="Synchronizer"></a>9. `[Synchronizer]`

**Type:** : `object`
Expand Down
4 changes: 4 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,10 @@
},
"type": "array",
"default": []
},
"Rerun": {
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
Expand Down
12 changes: 11 additions & 1 deletion jsonrpc/api_relay_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ApiRelayConfig struct {
Enabled bool `mapstructure:"Enabled"`
DestURI string `mapstructure:"DestURI"`
RPCs []string `mapstructure:"RPCs"`
Rerun bool `mapstructure:"Rerun"`
}

func shouldRelay(localCfg ApiRelayConfig, name string) bool {
Expand All @@ -27,6 +28,15 @@ func shouldRelay(localCfg ApiRelayConfig, name string) bool {
return enable && contained
}

func shouldRerun(localCfg ApiRelayConfig) bool {
if getApolloConfig().Enable() {
getApolloConfig().RLock()
defer getApolloConfig().RUnlock()
return getApolloConfig().ApiRelay.Rerun
}
return localCfg.Rerun
}

func getRelayDestURI(localDestURI string) string {
ret := localDestURI
if getApolloConfig().Enable() {
Expand All @@ -42,7 +52,7 @@ func getRelayDestURI(localDestURI string) string {
func tryRelay(localCfg ApiRelayConfig, request types.Request) (types.Response, bool) {
if shouldRelay(localCfg, request.Method) && pass(&request) {
destURI := getRelayDestURI(localCfg.DestURI)
res, err := client.JSONRPCRelay(destURI, request)
res, err := client.JSONRPCRelay(destURI, request, shouldRerun(localCfg))
if err != nil {
log.Errorf("failed to relay %v to %s: %v", request.Method, destURI, err)
metrics.RequestRelayFailCount(request.Method)
Expand Down
1 change: 1 addition & 0 deletions jsonrpc/apollo_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (c *ApolloConfig) setApiRelayCfg(apiRelayCfg ApiRelayConfig) {
c.ApiRelay.Enabled = apiRelayCfg.Enabled
c.ApiRelay.DestURI = apiRelayCfg.DestURI
c.ApiRelay.RPCs = make([]string, len(apiRelayCfg.RPCs))
c.ApiRelay.Rerun = apiRelayCfg.Rerun
copy(c.ApiRelay.RPCs, apiRelayCfg.RPCs)
}

Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/client/client_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// JSONRPCRelay executes a 2.0 JSON RPC HTTP Post Request to the provided URL with
// types.Request, which is compatible with the Ethereum
// JSON RPC Server.
func JSONRPCRelay(url string, request types.Request) (types.Response, error) {
func JSONRPCRelay(url string, request types.Request, rerun bool) (types.Response, error) {
httpRes, err := sendJSONRPC_HTTPRequest(url, request)
if err != nil {
return types.Response{}, err
Expand All @@ -33,7 +33,7 @@ func JSONRPCRelay(url string, request types.Request) (types.Response, error) {
if err != nil {
return types.Response{}, err
}
if res.Error != nil {
if res.Error != nil && rerun {
return types.Response{}, fmt.Errorf("response error: %v - %v", res.Error.Code, res.Error.Message)
}
return res, nil
Expand Down

0 comments on commit a752112

Please sign in to comment.