-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_instance_pool.go
96 lines (77 loc) · 2.26 KB
/
message_instance_pool.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
package bristle
import (
"sync"
"google.golang.org/protobuf/reflect/protoreflect"
)
// Similar to a sync.Pool but is more strictly sized
type MessageInstancePool struct {
sync.Mutex
wakeup *sync.Cond
pool []protoreflect.Message
}
func NewMessageInstancePool(messageType protoreflect.MessageType, size int) *MessageInstancePool {
pool := make([]protoreflect.Message, size)
for i := 0; i < size; i++ {
pool[i] = messageType.New()
}
if size < 1 {
panic("MessageInstancePool cannot have size less than 1")
}
p := &MessageInstancePool{
pool: pool,
}
p.wakeup = sync.NewCond(p)
return p
}
// This function has some important concurrency invariants so its thorougly documented
func (m *MessageInstancePool) Get() protoreflect.Message {
// We acquire the pool lock to search for an existing instance that we can
// utilize and return to the user.
m.Lock()
// This variable is used to indicate the position of our selected instance
// within the pool. If this is -1 it indicates that we've not yet found a
// existing instance.
var selectedIndex int = -1
// We'll keep trying until we can get one
for selectedIndex == -1 {
// Search for an existing instance (e.g. an instance slot that is not null)
for idx, instance := range m.pool {
if instance != nil {
selectedIndex = idx
break
}
}
// We've found a valid instance, we can now break and return it
if selectedIndex != -1 {
break
}
// Otherwise we need to wait until a instance is returned to the pool via
// the wakeup sync.Cond
// We call Wait on the condition which **releases** our underlying embedded
// sync.Mutex, and blocks until the condition is hit.
m.wakeup.Wait()
// Coming out of this wakeup we yet again have the sync.Mutex acquired,
// so repeating the loop is safe.
continue
}
instance := m.pool[selectedIndex]
m.pool[selectedIndex] = nil
m.Unlock()
return instance
}
func (m *MessageInstancePool) Release(instance protoreflect.Message) {
m.Lock()
defer m.Unlock()
var selectedIndex int = -1
for idx, instance := range m.pool {
if instance == nil {
selectedIndex = idx
break
}
}
if selectedIndex == -1 {
panic("invariant error: MessageInstancePool is full upon Release")
}
m.pool[selectedIndex] = instance
m.wakeup.Signal()
}