-
Notifications
You must be signed in to change notification settings - Fork 2
/
requests.go
55 lines (46 loc) · 1.06 KB
/
requests.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
package pulsar
import (
"sync"
)
type requestCallback func(*command) error
type requests struct {
mutex sync.RWMutex
callbacks map[uint64]requestCallback
custom map[uint64]string
sequencer *sequencer
}
func newRequests() *requests {
return &requests{
callbacks: map[uint64]requestCallback{},
custom: map[uint64]string{},
sequencer: &sequencer{},
}
}
// remove the request id and return it's assigned callback if existing.
func (r *requests) remove(reqID uint64) (requestCallback, string) {
r.mutex.Lock()
f, ok := r.callbacks[reqID]
if !ok {
r.mutex.Unlock()
return nil, ""
}
delete(r.callbacks, reqID)
s := r.custom[reqID]
delete(r.custom, reqID)
r.mutex.Unlock()
return f, s
}
func (r *requests) newID() uint64 {
return r.sequencer.newID()
}
func (r *requests) addCallback(reqID uint64, f requestCallback) {
r.mutex.Lock()
r.callbacks[reqID] = f
r.mutex.Unlock()
}
func (r *requests) addCallbackCustom(reqID uint64, f requestCallback, custom string) {
r.mutex.Lock()
r.callbacks[reqID] = f
r.custom[reqID] = custom
r.mutex.Unlock()
}