-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjustgateway.go
138 lines (121 loc) · 3.88 KB
/
adjustgateway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package bridge
import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"github.com/deltaswapio/swaprouter/v3/cmd/utils"
"github.com/deltaswapio/swaprouter/v3/log"
"github.com/deltaswapio/swaprouter/v3/router"
"github.com/deltaswapio/swaprouter/v3/tokens"
"github.com/deltaswapio/swaprouter/v3/tools"
)
var (
adjustInterval = 60 // seconds
adjustGatewayChains = new(sync.Map)
)
type gatewayQuality struct {
ContinueFailues uint64 `json:",omitempty"`
}
type gatewayQualityMap map[string]*gatewayQuality
func (m gatewayQualityMap) MarshalJSON() ([]byte, error) {
t := make(map[string]*gatewayQuality)
for k, v := range m {
if v.ContinueFailues == 0 {
continue
}
t[k] = v
}
return json.Marshal(t)
}
func (m gatewayQualityMap) String() string {
data, _ := json.Marshal(m)
return strings.ReplaceAll(string(data), `"`, "")
}
type adjustContext struct {
WeightedAPIs tools.WeightedStringSlice
GatewayQuality gatewayQualityMap
}
// AdjustGatewayOrder adjust gateway order once
func AdjustGatewayOrder(bridge tokens.IBridge, chainID string) {
// use block number as weight
var weightedAPIs tools.WeightedStringSlice
gateway := bridge.GetGatewayConfig()
if gateway == nil {
return
}
var adjustCtx *adjustContext
if gateway.AdjustContext == nil {
gateway.AdjustContext = &adjustContext{
GatewayQuality: make(map[string]*gatewayQuality),
}
}
adjustCtx = gateway.AdjustContext.(*adjustContext)
var maxHeight uint64
originURLs := gateway.OriginAllGatewayURLs
for i := len(originURLs); i > 0; i-- { // query in reverse order
if utils.IsCleanuping() {
return
}
apiAddress := originURLs[i-1]
if adjustCtx.GatewayQuality[apiAddress] == nil {
adjustCtx.GatewayQuality[apiAddress] = &gatewayQuality{}
}
height, err := bridge.GetLatestBlockNumberOf(apiAddress)
if err != nil {
adjustCtx.GatewayQuality[apiAddress].ContinueFailues++
if adjustCtx.GatewayQuality[apiAddress].ContinueFailues >= 3 {
if adjustCtx.GatewayQuality[apiAddress].ContinueFailues == 3 {
log.Warn("remove low quality gateway", "url", apiAddress, "chainID", chainID)
}
continue
}
} else {
if adjustCtx.GatewayQuality[apiAddress].ContinueFailues > 0 {
log.Info("recover low quality gateway", "url", apiAddress, "chainID", chainID)
adjustCtx.GatewayQuality[apiAddress].ContinueFailues = 0
}
}
weightedAPIs = weightedAPIs.Add(apiAddress, height)
if height > maxHeight {
maxHeight = height
}
}
if len(originURLs) == 0 { // update for bridges only use grpc apis
maxHeight, _ = bridge.GetLatestBlockNumber()
}
if maxHeight > 0 {
router.CachedLatestBlockNumber.Store(chainID, maxHeight)
}
if weightedAPIs.Len() > 0 {
weightedAPIs.Reverse() // reverse as iter in reverse order in the above
weightedAPIs = weightedAPIs.Sort()
gateway.AllGatewayURLs = weightedAPIs.GetStrings()
} else if len(originURLs) > 0 {
// no one is usable, then recover to the original state
gateway.AllGatewayURLs = gateway.OriginAllGatewayURLs
log.Info("reset to original gateways", "chainID", chainID, "count", len(gateway.AllGatewayURLs))
}
adjustCtx.WeightedAPIs = weightedAPIs
if _, exist := adjustGatewayChains.Load(chainID); !exist {
log.Info(fmt.Sprintf("adjust gateways of chain %v", chainID), "result", adjustCtx.WeightedAPIs, "gatewayQuality", adjustCtx.GatewayQuality)
adjustGatewayChains.Store(chainID, struct{}{})
go adjustGatewayOrder(bridge, chainID)
}
}
func adjustGatewayOrder(bridge tokens.IBridge, chainID string) {
for adjustCount := 0; ; adjustCount++ {
for i := 0; i < adjustInterval; i++ {
if utils.IsCleanuping() {
return
}
time.Sleep(1 * time.Second)
}
AdjustGatewayOrder(bridge, chainID)
if adjustCount%3 == 0 {
adjustCtx := bridge.GetGatewayConfig().AdjustContext.(*adjustContext)
log.Info(fmt.Sprintf("adjust gateways of chain %v", chainID), "result", adjustCtx.WeightedAPIs, "gatewayQuality", adjustCtx.GatewayQuality)
}
}
}