-
Notifications
You must be signed in to change notification settings - Fork 25
/
lock.go
59 lines (47 loc) · 1.44 KB
/
lock.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
package lock
import (
"github.com/aldor007/mort/pkg/response"
)
// Lock is responding for collapsing request for same object
type Lock interface {
// Lock try get a lock for given key
Lock(key string) (observer LockResult, acquired bool)
// Release remove lock for given key
Release(key string)
// NotifyAndRelease remove lock for given key and notify all clients waiting for result
NotifyAndRelease(key string, res *response.Response)
}
// LockResult contain struct
type LockResult struct {
ResponseChan chan *response.Response // channel on which you get response
Cancel chan bool // channel for notify about cancel of waiting
}
type lockData struct {
Key string
notifyQueue []LockResult
}
// AddWatcher add next request waiting for lock to expire or return result
func (l *lockData) AddWatcher() LockResult {
d := LockResult{}
d.ResponseChan = make(chan *response.Response, 1)
d.Cancel = make(chan bool, 1)
l.notifyQueue = append(l.notifyQueue, d)
return d
}
// NewNopLock create lock that do nothing
func NewNopLock() *NopLock {
return &NopLock{}
}
// NopLock will never collapse any request
type NopLock struct {
}
// Lock always return that lock was acquired
func (l *NopLock) Lock(_ string) (LockResult, bool) {
return LockResult{}, true
}
// Release do nothing
func (l *NopLock) Release(_ string) {
}
// NotifyAndRelease do nothing
func (l *NopLock) NotifyAndRelease(_ string, _ *response.Response) {
}