-
Notifications
You must be signed in to change notification settings - Fork 22
/
memcache.go
497 lines (416 loc) · 9.69 KB
/
memcache.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package memcache
import (
"errors"
"sync"
"time"
)
type Memcache struct {
nodes *Nodes
manager *serverManager
sync.RWMutex //保证操作nodes的原子性
}
type serverManager struct {
serverList []*Server
badServerNotice chan bool
isRmBadServer bool
}
var (
badTryCnt = 4
defaultMaxConn = 128
defaultInitConn = 8
defaultIdleTime = time.Hour * 2
)
func NewMemcache(server_list []*Server) (mem *Memcache, err error) { /*{{{*/
if server_list == nil {
return nil, errors.New("Server is nil or address is empty")
}
mem = &Memcache{}
//create connect pool
for _, server := range server_list {
if server == nil || server.Address == "" {
return nil, errors.New("Server is nil or address is empty")
}
if server.MaxConn == 0 {
server.MaxConn = defaultMaxConn
}
if server.InitConn == 0 {
server.InitConn = defaultInitConn
}
if server.IdleTime == 0 {
server.IdleTime = defaultIdleTime
}
server.isActive = true
}
mem.manager = &serverManager{
serverList: server_list,
}
//create server hash node
mem.nodes = createServerNode(server_list)
return mem, nil
} /*}}}*/
//设置是否移除不可用server
func (this *Memcache) SetRemoveBadServer(option bool) { /*{{{*/
if option == false {
return
}
this.manager.isRmBadServer = option
this.manager.badServerNotice = make(chan bool)
go this.monitorBadServer()
} /*}}}*/
func (this *Memcache) SetTimeout(dial, read, write time.Duration) { /*{{{*/
dialTimeout = dial
readTimeout = read
writeTimeout = write
} /*}}}*/
func (this *Memcache) Get(key string, format ...interface{}) (value interface{}, cas uint64, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
var res *response
server := this.nodes.getServerByKey(key)
if server == nil {
return nil, 0, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return nil, 0, e
}
res, err = conn.get(key, format...)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
if res != nil {
return res.body, res.header.cas, err
} else {
return nil, 0, err
}
} /*}}}*/
func (this *Memcache) Set(key string, value interface{}, expire ...uint32) (res bool, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
var timeout uint32 = 0
if len(expire) > 0 {
timeout = expire[0]
}
server := this.nodes.getServerByKey(key)
if server == nil {
return false, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.store(OP_SET, key, value, timeout, 0)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Add(key string, value interface{}, expire ...uint32) (res bool, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
var timeout uint32 = 0
if len(expire) > 0 {
timeout = expire[0]
}
server := this.nodes.getServerByKey(key)
if server == nil {
return false, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.store(OP_ADD, key, value, timeout, 0)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Replace(key string, value interface{}, args ...uint64) (res bool, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
var timeout uint32 = 0
var cas uint64 = 0
switch len(args) {
case 1:
timeout = uint32(args[0])
case 2:
timeout = uint32(args[0])
cas = args[1]
}
server := this.nodes.getServerByKey(key)
if server == nil {
return false, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.store(OP_REPLACE, key, value, timeout, cas)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Delete(key string, cas ...uint64) (res bool, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
server := this.nodes.getServerByKey(key)
if server == nil {
return false, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.delete(key, cas...)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Increment(key string, args ...interface{}) (res bool, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
server := this.nodes.getServerByKey(key)
if server == nil {
return false, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.numberic(OP_INCREMENT, key, args...)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Decrement(key string, args ...interface{}) (res bool, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
server := this.nodes.getServerByKey(key)
if server == nil {
return false, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.numberic(OP_DECREMENT, key, args...)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Append(key string, value string, cas ...uint64) (res bool, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
server := this.nodes.getServerByKey(key)
if server == nil {
return false, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.appends(OP_APPEND, key, value, cas...)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Prepend(key string, value string, cas ...uint64) (res bool, err error) { /*{{{*/
this.RLock()
defer this.RUnlock()
server := this.nodes.getServerByKey(key)
if server == nil {
return false, ErrNotConn
}
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.appends(OP_PREPEND, key, value, cas...)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Flush(server *Server, delay ...uint32) (res bool, err error) { /*{{{*/
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return false, e
}
res, err = conn.flush(delay...)
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return res, err
} /*}}}*/
func (this *Memcache) Version(server *Server) (v string, err error) { /*{{{*/
for i := 0; i < badTryCnt; i++ {
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
this.sendBadServerNotice()
return "", e
}
v, err = conn.version()
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
break
}
}
return v, err
} /*}}}*/
func (this *Memcache) sendBadServerNotice() { /*{{{*/
if this.manager.isRmBadServer == false {
return
}
select {
case this.manager.badServerNotice <- true:
default:
}
} /*}}}*/
func (this *Memcache) monitorBadServer() { /*{{{*/
for {
select {
case <-this.manager.badServerNotice:
this.doDealBadServer()
case <-time.After(time.Second * 120):
this.doDealBadServer()
}
}
} /*}}}*/
func (this *Memcache) doDealBadServer() { /*{{{*/
var res map[*Server]chan bool
var isReload bool = false
res = make(map[*Server]chan bool, len(this.manager.serverList))
for _, s := range this.manager.serverList {
res[s] = make(chan bool, 1)
go this.checkServerActive(s, res[s])
}
for s, ch := range res {
switch <-ch {
case true:
if s.isActive == false {
s.pool.Close()
s.pool = nil
isReload = true
}
s.isActive = true
case false:
if s.isActive == true {
isReload = true
}
s.isActive = false
}
}
if isReload == false {
return
}
//有server状态发生变化,重新生成node
new_server_list := make([]*Server, 0)
for _, s := range this.manager.serverList {
if s.isActive == true {
new_server_list = append(new_server_list, s)
}
}
//create server hash node
new_nodes := createServerNode(new_server_list)
this.Lock()
this.nodes = new_nodes
this.Unlock()
} /*}}}*/
func (this *Memcache) checkServerActive(server *Server, ch chan bool) { /*{{{*/
conn, e := server.pool.Get()
if e != nil && e == ErrNotConn {
//can't connect to server
ch <- false
return
}
res, err := conn.noop()
if err == ErrBadConn {
server.pool.Release(conn)
} else {
server.pool.Put(conn)
}
if err == nil && res == true {
ch <- true
return
}
//noop failed ,then try dial server
if new_connection, err := connect(server.Address); err == ErrNotConn {
ch <- false
} else {
ch <- true
new_connection.Close()
}
} /*}}}*/
func (this *Memcache) Close() {
for _, s := range this.manager.serverList {
s.pool.Close()
}
}