Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rerun to api relay config #233

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading