From a2c27b7b081c185a0407318af41a014c3a0f1ad6 Mon Sep 17 00:00:00 2001 From: zhangkai Date: Wed, 28 Aug 2024 09:21:38 +0800 Subject: [PATCH] Add relay all feature --- jsonrpc/api_relay_xlayer.go | 31 +++++++++++++++++++++++++------ jsonrpc/apollo_xlayer.go | 1 + 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/jsonrpc/api_relay_xlayer.go b/jsonrpc/api_relay_xlayer.go index b2d3d78e05..09a4c652e1 100644 --- a/jsonrpc/api_relay_xlayer.go +++ b/jsonrpc/api_relay_xlayer.go @@ -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() { @@ -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 { @@ -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) +} diff --git a/jsonrpc/apollo_xlayer.go b/jsonrpc/apollo_xlayer.go index bcbd7ab9c8..4788c431ae 100644 --- a/jsonrpc/apollo_xlayer.go +++ b/jsonrpc/apollo_xlayer.go @@ -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) }