This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.go
242 lines (215 loc) · 5.39 KB
/
http.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package edgec
import (
"fmt"
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/internal/log"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"sync"
"sync/atomic"
"time"
)
/*
Creation Time: 2020 - Dec - 27
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
// HttpConfig holds the configurations for the Http client.
type HttpConfig struct {
Name string
SeedHostPort string
HeaderFunc func() map[string]string
ReadTimeout time.Duration
WriteTimeout time.Duration
ContextTimeout time.Duration
RequestMaxRetry int
Router Router
Secure bool
}
// Http connects to edge servers with HTTP transport.
type Http struct {
cfg HttpConfig
reqID uint64
c *fasthttp.Client
mtx sync.RWMutex
sessionReplica uint64
hosts map[uint64]map[string]*httpConn // holds host by replicaSet/hostID
leaders map[uint64]string
}
func NewHttp(config HttpConfig) *Http {
h := &Http{
cfg: config,
c: &fasthttp.Client{
Name: config.Name,
MaxIdemponentCallAttempts: 10,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
MaxResponseBodySize: 0,
},
hosts: make(map[uint64]map[string]*httpConn, 32),
leaders: make(map[uint64]string, 32),
}
if h.cfg.Router == nil {
h.cfg.Router = &httpRouter{
c: h,
}
}
if h.cfg.RequestMaxRetry == 0 {
h.cfg.RequestMaxRetry = requestRetry
}
if h.cfg.ContextTimeout == 0 {
h.cfg.ContextTimeout = requestTimeout
}
return h
}
func (h *Http) addConn(serverID string, replicaSet uint64, leader bool, c *httpConn) {
h.mtx.Lock()
defer h.mtx.Unlock()
if h.hosts[replicaSet] == nil {
h.hosts[replicaSet] = make(map[string]*httpConn, 16)
}
h.hosts[replicaSet][serverID] = c
if leader || replicaSet == 0 {
h.leaders[replicaSet] = serverID
}
}
func (h *Http) getConn(replicaSet uint64, onlyLeader bool) *httpConn {
h.mtx.RLock()
defer h.mtx.RUnlock()
if onlyLeader {
leaderID := h.leaders[replicaSet]
if leaderID == "" {
return nil
}
m := h.hosts[replicaSet]
if m != nil {
c := m[leaderID]
return c
}
} else {
m := h.hosts[replicaSet]
for _, c := range m {
return c
}
}
return nil
}
func (h *Http) newConn(id string, replicaSet uint64, hostPorts ...string) *httpConn {
return &httpConn{
id: id,
h: h,
replicaSet: replicaSet,
hostPorts: hostPorts,
secure: h.cfg.Secure,
}
}
func (h *Http) Start() error {
err := h.initConn()
if err != nil {
return err
}
return nil
}
func (h *Http) initConn() error {
initConn := h.newConn("", 0, h.cfg.SeedHostPort)
req := rony.PoolMessageEnvelope.Get()
defer rony.PoolMessageEnvelope.Put(req)
res := rony.PoolMessageEnvelope.Get()
defer rony.PoolMessageEnvelope.Put(res)
req.Fill(h.GetRequestID(), rony.C_GetNodes, &rony.GetNodes{})
sessionReplica, err := initConn.send(req, res, requestTimeout)
if err != nil {
return err
}
h.sessionReplica = sessionReplica
switch res.Constructor {
case rony.C_Edges:
x := &rony.Edges{}
_ = x.Unmarshal(res.Message)
found := false
for _, n := range x.Nodes {
if ce := log.Check(log.DebugLevel, "NodeInfo"); ce != nil {
ce.Write(
zap.String("ServerID", n.ServerID),
zap.Uint64("RS", n.ReplicaSet),
zap.Bool("Leader", n.Leader),
zap.Strings("HostPorts", n.HostPorts),
)
}
httpc := h.newConn(n.ServerID, n.ReplicaSet, n.HostPorts...)
if !found {
for _, hp := range n.HostPorts {
if hp == initConn.hostPorts[0] {
httpc = initConn
httpc.hostPorts = n.HostPorts
found = true
}
}
}
h.addConn(n.ServerID, n.ReplicaSet, n.Leader, httpc)
if n.Leader {
h.sessionReplica = n.ReplicaSet
}
}
default:
fmt.Println(res)
return ErrUnknownResponse
}
return nil
}
func (h *Http) Send(req *rony.MessageEnvelope, res *rony.MessageEnvelope, leaderOnly bool) error {
return h.SendWithDetails(req, res, h.cfg.RequestMaxRetry, h.cfg.ContextTimeout, leaderOnly)
}
func (h *Http) SendWithDetails(req *rony.MessageEnvelope, res *rony.MessageEnvelope, retry int, timeout time.Duration, leaderOnly bool) (err error) {
rs := h.cfg.Router.GetRoute(req)
hc := h.getConn(rs, leaderOnly)
if hc == nil {
return ErrNoConnection
}
SendLoop:
if ce := log.Check(log.DebugLevel, "Send"); ce != nil {
ce.Write(
zap.Uint64("ReqID", req.RequestID),
zap.Uint64("RS", rs),
zap.Bool("LeaderOnly", leaderOnly),
zap.Int("Retry", retry),
)
}
rs, err = hc.send(req, res, timeout)
switch err {
case nil:
return nil
case ErrReplicaMaster:
leaderOnly = true
hc = h.getConn(rs, leaderOnly)
case ErrReplicaSetSession, ErrReplicaSetRequest:
rs = h.sessionReplica
}
// If we exceeds the maximum retry then we return
if retry--; retry < 0 {
err = rony.ErrRetriesExceeded(err)
return
}
goto SendLoop
}
// Close implements Client interface
func (h *Http) Close() error {
h.c.CloseIdleConnections()
return nil
}
// GetRequestID implements Client interface
func (h *Http) GetRequestID() uint64 {
return atomic.AddUint64(&h.reqID, 1)
}
type httpRouter struct {
c *Http
}
func (d *httpRouter) UpdateRoute(req *rony.MessageEnvelope, replicaSet uint64) {
// TODO:: implement cache maybe
}
func (d *httpRouter) GetRoute(req *rony.MessageEnvelope) (replicaSet uint64) {
return d.c.sessionReplica
}