forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
120 lines (101 loc) · 2.67 KB
/
node.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
package hedera
import (
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"math"
"time"
)
type node struct {
accountID AccountID
address string
delay int64
lastUsed int64
delayUntil int64
useCount int64
channel *channel
}
type nodes struct {
nodes []*node
}
func newNode(accountID AccountID, address string) node {
return node{
accountID: accountID,
address: address,
delay: 250,
lastUsed: time.Now().UTC().UnixNano(),
delayUntil: time.Now().UTC().UnixNano(),
useCount: 0,
channel: nil,
}
}
func (node *node) inUse() {
node.useCount += 1
node.lastUsed = time.Now().UTC().UnixNano()
}
func (node *node) isHealthy() bool {
return node.delayUntil <= time.Now().UTC().UnixNano()
}
func (node *node) increaseDelay() {
node.delay = int64(math.Min(float64(node.delay)*2, 8000))
node.delayUntil = (node.delay * 100000) + time.Now().UTC().UnixNano()
}
func (node *node) decreaseDelay() {
node.delay = int64(math.Max(float64(node.delay)/2, 250))
}
func (node *node) wait() {
delay := node.delayUntil - node.lastUsed
time.Sleep(time.Duration(delay) * time.Nanosecond)
}
func (node *node) getChannel() (*channel, error) {
if node.channel != nil {
return node.channel, nil
}
var kacp = keepalive.ClientParameters{
Time: 10 * time.Second,
Timeout: time.Second,
PermitWithoutStream: true,
}
conn, err := grpc.Dial(node.address, grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp), grpc.WithBlock())
if err != nil {
return nil, errors.Wrapf(err, "error connecting to node at %s", node.address)
}
ch := newChannel(conn)
node.channel = &ch
return node.channel, nil
}
func (node *node) close() error {
if node.channel != nil {
return node.channel.client.Close()
}
return nil
}
func (nodes nodes) Len() int {
return len(nodes.nodes)
}
func (nodes nodes) Swap(i, j int) {
nodes.nodes[i], nodes.nodes[j] = nodes.nodes[j], nodes.nodes[i]
}
func (nodes nodes) Less(i, j int) bool {
if nodes.nodes[i].isHealthy() && nodes.nodes[j].isHealthy() {
if nodes.nodes[i].useCount < nodes.nodes[j].useCount {
return true
} else if nodes.nodes[i].useCount > nodes.nodes[j].useCount {
return false
} else {
return nodes.nodes[i].lastUsed < nodes.nodes[j].lastUsed
}
} else if nodes.nodes[i].isHealthy() && !nodes.nodes[j].isHealthy() {
return true
} else if !nodes.nodes[i].isHealthy() && nodes.nodes[j].isHealthy() {
return false
} else {
if nodes.nodes[i].useCount < nodes.nodes[j].useCount {
return true
} else if nodes.nodes[i].useCount > nodes.nodes[j].useCount {
return false
} else {
return nodes.nodes[i].lastUsed < nodes.nodes[j].lastUsed
}
}
}