-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathack.go
55 lines (47 loc) · 1.05 KB
/
ack.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 shadiaosocketio
import (
"errors"
"sync"
)
var (
ErrorWaiterNotFound = errors.New("Waiter not found")
)
/**
Processes functions that require answers, also known as acknowledge or ack
*/
type ackProcessor struct {
counter int
counterLock sync.Mutex
resultWaitersMap sync.Map
}
/**
get next id of ack call
*/
func (a *ackProcessor) getNextId() int {
a.counterLock.Lock()
defer a.counterLock.Unlock()
a.counter++
return a.counter
}
/**
Just before the ack function called, the waiter should be added
to wait and receive response to ack call
*/
func (a *ackProcessor) addWaiter(id int, w chan interface{}) {
a.resultWaitersMap.Store(id, w)
}
/**
removes waiter that is unnecessary anymore
*/
func (a *ackProcessor) removeWaiter(id int) {
a.resultWaitersMap.Delete(id)
}
/**
check if waiter with given ack id is exists, and returns it
*/
func (a *ackProcessor) getWaiter(id int) (chan interface{}, error) {
if waiter, ok := a.resultWaitersMap.Load(id); ok {
return waiter.(chan interface{}), nil
}
return nil, ErrorWaiterNotFound
}