forked from goat-systems/go-tezos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goTezos.go
178 lines (155 loc) · 4.01 KB
/
goTezos.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package goTezos
import (
"math/rand"
"time"
"sync"
"log"
)
type TezClientWrapper struct {
healthy bool // isHealthy
client *TezosRPCClient
}
/*
* GoTezos manages multiple Clients
* each Client represents a Connection to a Tezos Node
* GoTezos manages failover if one Node is down, there
* are 2 Strategies:
* failover: always use the same unless it is down -> go to the next - default
* random: send to each Node equally
*/
type GoTezos struct {
clientLock sync.Mutex
RpcClients []*TezClientWrapper
ActiveRPCCient *TezClientWrapper
balancerStrategy string
rand *rand.Rand
logger *log.Logger
}
func NewGoTezos() *GoTezos {
a := GoTezos{}
a.UseBalancerStrategyFailover()
a.rand = rand.New(rand.NewSource(time.Now().Unix()))
go func(a *GoTezos){
for {
time.Sleep(15*time.Second)
a.checkUnhealthyClients()
}
}(&a)
return &a
}
func (this *GoTezos) SetLogger(log *log.Logger) {
this.logger = log
}
func (this *GoTezos) AddNewClient(client *TezosRPCClient) {
this.clientLock.Lock()
this.RpcClients = append(this.RpcClients, &TezClientWrapper{true, client})
this.clientLock.Unlock()
}
func (this *GoTezos) UseBalancerStrategyFailover(){
this.balancerStrategy = "failover"
}
func (this *GoTezos) UseBalancerStrategyRandom(){
this.balancerStrategy = "random"
}
func (this *GoTezos) checkHealthStatus(){
this.clientLock.Lock()
wg := sync.WaitGroup{}
for _,a := range this.RpcClients {
wg.Add(1)
go func(wg *sync.WaitGroup, client *TezClientWrapper){
res := a.client.Healthcheck()
if a.healthy && res == false {
this.logger.Println("Client State swithished to unhealthy", this.ActiveRPCCient.client.Host + this.ActiveRPCCient.client.Port)
}
if !a.healthy && res {
this.logger.Println("Client State swithished to healthy", this.ActiveRPCCient.client.Host + this.ActiveRPCCient.client.Port)
}
a.healthy = res
wg.Done()
}(&wg, a)
}
wg.Wait()
this.clientLock.Unlock()
}
func (this *GoTezos) checkUnhealthyClients(){
this.clientLock.Lock()
wg := sync.WaitGroup{}
for _,a := range this.RpcClients {
wg.Add(1)
go func(wg *sync.WaitGroup, client *TezClientWrapper){
if a.healthy == false {
res := a.client.Healthcheck()
if !a.healthy && res {
this.logger.Println("Client State swithished to healthy", this.ActiveRPCCient.client.Host+this.ActiveRPCCient.client.Port)
}
a.healthy = res
}
wg.Done()
}(&wg, a)
}
wg.Wait()
this.clientLock.Unlock()
}
func (this *GoTezos) getFirstHealthyClient() *TezClientWrapper{
this.clientLock.Lock()
defer this.clientLock.Unlock()
for _,a := range this.RpcClients {
if a.healthy == true {
return a
}
}
return nil
}
func (this *GoTezos) getRandomHealthyClient() *TezClientWrapper{
this.clientLock.Lock()
defer this.clientLock.Unlock()
clients := []int{}
for i,_ := range this.RpcClients {
clients = append(clients, i)
}
for _, i := range this.rand.Perm(len(clients)) {
return this.RpcClients[i]
}
return nil
}
func (this *GoTezos) setActiveclient() error {
if this.balancerStrategy == "failover" {
c := this.getFirstHealthyClient()
if c == nil {
this.checkHealthStatus()
c = this.getFirstHealthyClient()
if c == nil {
return NoClientError{}
}
}
this.ActiveRPCCient = c
}
if this.balancerStrategy == "random" {
c := this.getRandomHealthyClient()
if c == nil {
this.checkHealthStatus()
c = this.getRandomHealthyClient()
if c == nil {
return NoClientError{}
} else {
this.ActiveRPCCient = c
}
}
this.ActiveRPCCient = c
}
return nil
}
func (this *GoTezos) GetResponse(method string, args string) (ResponseRaw, error) {
e := this.setActiveclient()
if e != nil {
this.logger.Println("goTezos","could not find any healthy Clients")
return ResponseRaw{},e
}
r, err := this.ActiveRPCCient.client.GetResponse(method,args)
if err != nil {
this.ActiveRPCCient.healthy = false
this.logger.Println( this.ActiveRPCCient.client.Host + this.ActiveRPCCient.client.Port, "Client State switched to unhealthy")
return this.GetResponse(method,args)
}
return r,err
}