Skip to content

Commit

Permalink
Add relay all feature
Browse files Browse the repository at this point in the history
  • Loading branch information
giskook committed Aug 28, 2024
1 parent 9d5563d commit a2c27b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
31 changes: 25 additions & 6 deletions jsonrpc/api_relay_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (

// ApiRelayConfig is the api relay config
type ApiRelayConfig struct {
Enabled bool `mapstructure:"Enabled"`
DestURI string `mapstructure:"DestURI"`
RPCs []string `mapstructure:"RPCs"`
Rerun bool `mapstructure:"Rerun"`
Enabled bool `mapstructure:"Enabled"`
DestURI string `mapstructure:"DestURI"`
RPCs []string `mapstructure:"RPCs"`
Rerun bool `mapstructure:"Rerun"`
RelayAll bool `mapstructure:"RelayAll"`
}

func shouldRelay(localCfg ApiRelayConfig, name string) bool {
func shouldRelayByMethod(localCfg ApiRelayConfig, name string) bool {
enable := localCfg.Enabled && localCfg.DestURI != ""
contained := types.Contains(localCfg.RPCs, name)
if getApolloConfig().Enable() {
Expand Down Expand Up @@ -50,7 +51,7 @@ func getRelayDestURI(localDestURI string) string {
}

func tryRelay(localCfg ApiRelayConfig, request types.Request) (types.Response, bool) {
if shouldRelay(localCfg, request.Method) && pass(&request) {
if shouldRelay(localCfg, &request) {
destURI := getRelayDestURI(localCfg.DestURI)
res, err := client.JSONRPCRelay(destURI, request, shouldRerun(localCfg))
if err != nil {
Expand All @@ -64,3 +65,21 @@ func tryRelay(localCfg ApiRelayConfig, request types.Request) (types.Response, b

return types.Response{}, false
}

func shouldRelay(localCfg ApiRelayConfig, request *types.Request) bool {
enable := localCfg.Enabled &&
localCfg.DestURI != ""
relayAll := localCfg.RelayAll
if getApolloConfig().Enable() {
getApolloConfig().RLock()
defer getApolloConfig().RUnlock()
enable = getApolloConfig().ApiRelay.Enabled &&
getApolloConfig().ApiRelay.DestURI != ""
relayAll = getApolloConfig().ApiRelay.RelayAll
}
if enable && relayAll {
return true
}

return shouldRelayByMethod(localCfg, request.Method) && pass(request)
}
1 change: 1 addition & 0 deletions jsonrpc/apollo_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (c *ApolloConfig) setApiRelayCfg(apiRelayCfg ApiRelayConfig) {
c.ApiRelay.DestURI = apiRelayCfg.DestURI
c.ApiRelay.RPCs = make([]string, len(apiRelayCfg.RPCs))
c.ApiRelay.Rerun = apiRelayCfg.Rerun
c.ApiRelay.RelayAll = apiRelayCfg.RelayAll
copy(c.ApiRelay.RPCs, apiRelayCfg.RPCs)
}

Expand Down

0 comments on commit a2c27b7

Please sign in to comment.