-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoker.go
62 lines (54 loc) · 973 Bytes
/
invoker.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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package raft
import (
"sync"
)
var (
invokerPool *sync.Pool
donePool *sync.Pool
)
func init() {
invokerPool = &sync.Pool{
New: func() interface{} {
return &invoker{}
},
}
invokerPool.Put(invokerPool.Get())
donePool = &sync.Pool{
New: func() interface{} {
return make(chan *invoker, 10)
},
}
donePool.Put(donePool.Get())
}
func newInvoker() *invoker {
var i = invokerPool.Get().(*invoker)
i.Done = donePool.Get().(chan *invoker)
return i
}
func freeInvoker(i *invoker) {
done := i.Done
for len(done) > 0 {
select {
case <-done:
default:
}
}
donePool.Put(done)
*i = invoker{}
invokerPool.Put(i)
}
type invoker struct {
index uint64
Command Command
Reply interface{}
Error error
Done chan *invoker
}
func (i *invoker) done() {
select {
case i.Done <- i:
default:
}
}