-
Notifications
You must be signed in to change notification settings - Fork 29
/
rpcFailover.go
129 lines (116 loc) · 3.17 KB
/
rpcFailover.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
package main
import (
"fmt"
"log"
"sort"
"strings"
"sync"
"github.com/blocto/solana-go-sdk/client"
)
var failoverMutex sync.Mutex
type FailoverEndpointList []FailoverEndpoint
type FailoverEndpoint struct {
Endpoint string
AccessToken string
Piority int
MaxRetry int
Retry int
}
type RPCFailover struct {
curIndex int
Endpoints []FailoverEndpoint
}
func (f FailoverEndpointList) Len() int { return len(f) }
func (f FailoverEndpointList) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f FailoverEndpointList) Less(i, j int) bool { return f[i].Piority < f[j].Piority }
func NewRPCFailover(endpoints []RPCEndpoint) RPCFailover {
fList := []FailoverEndpoint{}
for _, e := range endpoints {
endpointExist := strings.TrimRight(e.Endpoint, " /")
if len(endpointExist) == 0 {
continue
}
tokenExist := strings.Trim(e.AccessToken, " ")
fList = append(fList,
FailoverEndpoint{
Endpoint: endpointExist,
AccessToken: tokenExist,
Piority: e.Piority,
MaxRetry: e.MaxRetry})
}
sort.Sort(FailoverEndpointList(FailoverEndpointList(fList)))
return RPCFailover{
Endpoints: fList,
}
}
func (f *RPCFailover) IsFail() bool {
if f.Endpoints[f.curIndex].Retry >= f.Endpoints[f.curIndex].MaxRetry {
return true
}
return false
}
func (f *RPCFailover) GetNext() string {
failoverMutex.Lock()
defer failoverMutex.Unlock()
f.curIndex = f.curIndex + 1
return f.Endpoints[f.curIndex].Endpoint
}
func (f *RPCFailover) GoNext(cur *client.Client, config ClusterConfig, workerNum int) *client.Client {
failoverMutex.Lock()
defer failoverMutex.Unlock()
var next *client.Client
retries := f.GetEndpoint().Retry
if retries < f.GetEndpoint().MaxRetry { // Go Next
if cur != nil {
return cur
} else {
connectionEndpoint := f.GetEndpoint().Endpoint
if len(f.GetEndpoint().AccessToken) != 0 {
connectionEndpoint = fmt.Sprintf("%s/%s", f.GetEndpoint().Endpoint, f.GetEndpoint().AccessToken)
}
return client.NewClient(connectionEndpoint)
}
}
idx := f.GetNextIndex()
if len(f.Endpoints[idx].AccessToken) != 0 {
next = client.NewClient(fmt.Sprintf("%s/%s", f.Endpoints[idx].Endpoint, f.Endpoints[idx].AccessToken))
} else {
next = client.NewClient(f.Endpoints[idx].Endpoint)
}
log.Println("GoNext!!! New Endpoint:", f.GetEndpoint())
if config.AlternativeEnpoint.SlackAlert.Enabled {
var slack SlackPayload
slack.FailoverAlertPayload(config, *f.GetEndpoint(), workerNum)
SlackSend(config.AlternativeEnpoint.SlackAlert.Webhook, &slack)
}
return next
}
func (f *RPCFailover) GetEndpoint() *FailoverEndpoint {
return &f.Endpoints[f.curIndex]
}
func (f *FailoverEndpoint) RetryResult(err PingResultError) {
failoverMutex.Lock()
defer failoverMutex.Unlock()
if err.HasError() {
if err.IsTooManyRequest429() ||
err.IsServiceUnavilable() ||
err.IsErrGatewayTimeout504() ||
err.IsNoSuchHost() ||
err.IsConnectionRefused() {
f.Retry += 1
}
} else {
f.Retry = 0
}
}
func (f *RPCFailover) GetNextIndex() int {
if f.curIndex < 0 {
log.Panic("current Index of FailoverEndpoint < 0")
}
if f.curIndex+1 > len(f.Endpoints)-1 {
f.curIndex = 0
} else {
f.curIndex = f.curIndex + 1
}
return f.curIndex
}