forked from graarh/golang-socketio
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathacks.go
52 lines (41 loc) · 966 Bytes
/
acks.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
package gosocketio
import (
"errors"
"sync"
"github.com/mtfelian/synced"
)
var (
ErrorAckWaiterNotFound = errors.New("ack waiter not found")
)
// acks represents chans needed for Ack messages to work
type acks struct {
count synced.Counter
ackC map[int]chan string
ackMu sync.RWMutex
}
// nextId of ack waiter
func (a *acks) nextId() int {
a.count.Inc()
return a.count.Get()
}
// register new ack request waiter
func (a *acks) register(id int, ackC chan string) {
a.ackMu.Lock()
a.ackC[id] = ackC
a.ackMu.Unlock()
}
// unregister a waiter by ack id that is unnecessary anymore
func (a *acks) unregister(id int) {
a.ackMu.Lock()
delete(a.ackC, id)
a.ackMu.Unlock()
}
// obtain checks that waiter at given ack id exists and returns the appropriate chan
func (a *acks) obtain(id int) (chan string, error) {
a.ackMu.RLock()
defer a.ackMu.RUnlock()
if ackC, ok := a.ackC[id]; ok {
return ackC, nil
}
return nil, ErrorAckWaiterNotFound
}