Skip to content

Commit 4595c35

Browse files
committed
mod: optimize aggregator
1 parent 7b44fc3 commit 4595c35

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

internal/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
defaultPhishingDb = "https://cfg.rpchub.io/agg/scam-addresses.json"
2323

2424
_Config = &Config{
25-
RequestTimeout: 60,
25+
RequestTimeout: 90,
2626
MaxRetries: 3,
2727
PhishingDb: []string{defaultPhishingDb},
2828
PhishingDbUpdateInterval: 3600,

internal/middleware/plugins/http_proxy.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ func (m *HttpProxyMiddleware) OnProcess(session *rpc.Session) error {
7676
if ctx, ok := session.RequestCtx.(*fasthttp.RequestCtx); ok {
7777
log.Debug("relay rpc -> "+session.RpcMethod(), "sid", session.SId(), "node", session.NodeName, "isTx", session.IsWriteRpcMethod, "tries", session.Tries)
7878

79+
var node *aggregator.Node
7980
if _, ok := m.disableEndpoints.Get(session.NodeName); ok {
8081
log.Debug("disabled endpoint", "node", session.NodeName)
8182
retries := 3
8283
for {
8384
if retries == 0 {
8485
return aggregator.ErrServerError
8586
}
86-
node := loadbalance.NextNode(session.Chain)
87+
node = loadbalance.NextNode(session.Chain)
8788
if node != nil {
8889
session.NodeName = node.Name
8990
ctx.Request.SetRequestURI(node.Endpoint)
@@ -114,13 +115,16 @@ func (m *HttpProxyMiddleware) OnProcess(session *rpc.Session) error {
114115
}, policies...)
115116

116117
// TODO: add response headers
117-
//if ctx, ok := session.RequestCtx.(*fasthttp.RequestCtx); ok {
118-
// ctx.Response.Header.Set("Access-Control-Max-Age", "86400")
119-
// ctx.Response.Header.Set("Access-Control-Allow-Origin", "*")
120-
// ctx.Response.Header.Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
121-
// ctx.Response.Header.Set("Access-Control-Allow-Credentials", "true")
122-
// ctx.Response.Header.Set("X-Do-Node", session.NodeName)
123-
//}
118+
if node != nil && strings.Contains(node.Endpoint, "coin98.com") {
119+
if ctx, ok := session.RequestCtx.(*fasthttp.RequestCtx); ok {
120+
ctx.Response.Header.Set("origin", "https://dex.saros.xyz")
121+
// ctx.Response.Header.Set("Access-Control-Max-Age", "86400")
122+
// ctx.Response.Header.Set("Access-Control-Allow-Origin", "*")
123+
// ctx.Response.Header.Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
124+
// ctx.Response.Header.Set("Access-Control-Allow-Credentials", "true")
125+
// ctx.Response.Header.Set("X-Do-Node", session.NodeName)
126+
}
127+
}
124128

125129
var shouldDisableEndpoint = false
126130
// alert if error
@@ -177,7 +181,13 @@ func (m *HttpProxyMiddleware) OnProcess(session *rpc.Session) error {
177181

178182
// check response body
179183
var response map[string]interface{}
180-
if err1 := json.Unmarshal(ctx.Response.Body(), &response); err1 == nil {
184+
if strings.Contains(string(ctx.Response.Body()), "error status code 429") {
185+
log.Error("error response", "node", session.NodeName, "response", string(ctx.Response.Body()))
186+
err = fmt.Errorf("error response %s", string(ctx.Response.Body()))
187+
shouldDisableEndpoint = true
188+
ctx.SetStatusCode(429)
189+
return err
190+
} else if err1 := json.Unmarshal(ctx.Response.Body(), &response); err1 == nil {
181191
if _, ok := response["error"]; ok {
182192
log.Error("error response", "node", session.NodeName, "response", string(ctx.Response.Body()))
183193
err = fmt.Errorf("error response %s", string(ctx.Response.Body()))

0 commit comments

Comments
 (0)