-
Notifications
You must be signed in to change notification settings - Fork 61
/
api.go
267 lines (222 loc) · 6.04 KB
/
api.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package qrpc
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"go.uber.org/zap"
"github.com/zhiqiangxu/util"
)
/* api provides utilities for make nonblocking api calls with qrpc.Connection */
// API for non blocking roundtrip calls
type API interface {
// call random endpoint
Call(ctx context.Context, cmd Cmd, payload []byte) (*Frame, error)
// call specified endpoint
CallOne(ctx context.Context, endpoint string, cmd Cmd, payload []byte) (*Frame, error)
// call all endpoint
CallAll(ctx context.Context, cmd Cmd, payload []byte) map[string]*APIResult
// Close all connections
Close() error
}
// NewAPI creates an API instance
func NewAPI(endpoints []string, conf ConnectionConfig, weights []int) API {
var totalWeight int
if weights == nil {
weights = make([]int, len(endpoints))
for idx := range weights {
weights[idx] = 1
}
totalWeight = len(endpoints)
} else {
for _, weight := range weights {
totalWeight += weight
}
}
ep := make([]string, len(endpoints))
copy(ep, endpoints)
idxMap := make(map[string]int)
d := &defaultAPI{endpoints: ep, weights: weights, totalWeight: totalWeight, conf: conf}
for idx, endpoint := range endpoints {
idxMap[endpoint] = idx
conn, err := NewConnection(endpoint, conf, nil)
if err != nil {
l.Error("NewConnection fail", zap.String("endpoint", endpoint), zap.Error(err))
continue
}
d.conns.Store(idx, conn)
d.activeConns.Store(conn, idx)
}
d.idxMap = idxMap
return d
}
type defaultAPI struct {
// imutable
endpoints []string
weights []int
idxMap map[string]int
totalWeight int
conf ConnectionConfig
activeConns sync.Map // map[*Connection]int
conns sync.Map // map[int]*Connection
mu sync.Mutex
closed int32
}
// call with random endpoint
func (api *defaultAPI) Call(ctx context.Context, cmd Cmd, payload []byte) (result *Frame, err error) {
idx := api.getIdx()
result, err = api.callViaIdx(ctx, idx, cmd, payload)
if err != nil {
result, err = api.callViaActiveConns(ctx, cmd, payload)
}
return
}
// APIResult is response for each endpoint
type APIResult struct {
Frame *Frame
Err error
}
func (api *defaultAPI) CallAll(ctx context.Context, cmd Cmd, payload []byte) map[string]*APIResult {
result := make(map[string]*APIResult)
mu := sync.Mutex{}
var wg sync.WaitGroup
for i := range api.endpoints {
idx := i
util.GoFunc(&wg, func() {
frame, err := api.callViaIdx(ctx, idx, cmd, payload)
mu.Lock()
result[api.endpoints[idx]] = &APIResult{Frame: frame, Err: err}
mu.Unlock()
})
}
wg.Wait()
return result
}
var (
// ErrEndPointNotExists when call non exist endpoint
ErrEndPointNotExists = errors.New("endpoint not exists")
// ErrClosed when calling closed api
ErrClosed = errors.New("api closed")
// ErrNotActiveConn when no active connection available
ErrNotActiveConn = errors.New("no active conn")
)
func (api *defaultAPI) CallOne(ctx context.Context, endpoint string, cmd Cmd, payload []byte) (*Frame, error) {
idx, ok := api.idxMap[endpoint]
if !ok {
return nil, ErrEndPointNotExists
}
return api.callViaIdx(ctx, idx, cmd, payload)
}
func (api *defaultAPI) callViaIdx(ctx context.Context, idx int, cmd Cmd, payload []byte) (result *Frame, err error) {
c, ok := api.conns.Load(idx)
if !ok {
return api.callWithoutConn(ctx, idx, cmd, payload)
}
conn := c.(*Connection)
_, resp, err := conn.Request(cmd, NBFlag, payload)
if err != nil {
api.safeCloseDeleteConn(idx, conn)
return api.callWithoutConn(ctx, idx, cmd, payload)
}
return resp.GetFrameWithContext(ctx)
}
func (api *defaultAPI) callWithoutConn(ctx context.Context, idx int, cmd Cmd, payload []byte) (result *Frame, err error) {
api.mu.Lock()
c, ok := api.conns.Load(idx)
if !ok {
conn, err := api.reconnectIdx(idx)
if err != nil {
api.mu.Unlock()
return nil, err
}
api.safeStoreConnLocked(idx, conn)
api.mu.Unlock()
_, resp, err := conn.Request(cmd, NBFlag, payload)
if err != nil {
api.safeCloseDeleteConn(idx, conn)
return nil, err
}
return resp.GetFrameWithContext(ctx)
}
api.mu.Unlock()
conn := c.(*Connection)
_, resp, err := conn.Request(cmd, NBFlag, payload)
if err != nil {
api.safeCloseDeleteConn(idx, conn)
return nil, err
}
return resp.GetFrameWithContext(ctx)
}
func (api *defaultAPI) safeCloseDeleteConn(idx int, conn *Connection) {
conn.Close()
api.mu.Lock()
defer api.mu.Unlock()
c, ok := api.conns.Load(idx)
if ok && c.(*Connection) == conn {
api.conns.Delete(idx)
}
api.activeConns.Delete(conn)
}
func (api *defaultAPI) safeStoreConnLocked(idx int, conn *Connection) {
_, ok := api.conns.Load(idx)
if ok {
panic(fmt.Sprintf("bug when safeStoreConnLocked:%d", idx))
}
api.conns.Store(idx, conn)
api.activeConns.Store(conn, idx)
}
func (api *defaultAPI) reconnectIdx(idx int) (*Connection, error) {
if atomic.LoadInt32(&api.closed) != 0 {
return nil, ErrClosed
}
conn, err := NewConnection(api.endpoints[idx], api.conf, nil)
if err != nil {
l.Error("NewConnection fail", zap.Error(err))
return nil, err
}
return conn, nil
}
func (api *defaultAPI) callViaActiveConns(ctx context.Context, cmd Cmd, payload []byte) (result *Frame, err error) {
err = ErrNotActiveConn
var resp Response
api.activeConns.Range(func(k, v interface{}) bool {
ac := k.(*Connection)
_, resp, err = ac.Request(cmd, NBFlag, payload)
if err != nil {
idx := v.(int)
api.safeCloseDeleteConn(idx, ac)
result, err = api.callWithoutConn(ctx, idx, cmd, payload)
} else {
result, err = resp.GetFrameWithContext(ctx)
}
return false
})
return
}
func (api *defaultAPI) getIdx() int {
targetWeight := rand.Intn(api.totalWeight)
sumWeight := 0
for idx, weight := range api.weights {
sumWeight += weight
if sumWeight > targetWeight {
return idx
}
}
l.Error("getIdx bug")
return 0
}
func (api *defaultAPI) Close() error {
swapped := atomic.CompareAndSwapInt32(&api.closed, 0, 1)
if !swapped {
return ErrClosed
}
api.mu.Lock()
defer api.mu.Unlock()
api.activeConns.Range(func(k, v interface{}) bool {
k.(*Connection).Close()
return true
})
return nil
}