forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lease.go
38 lines (32 loc) · 831 Bytes
/
lease.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
package lease
import "time"
// Leaser manages resources via leases
type Leaser interface {
// lease the resource r for duration d. When the lease expires, invoke `revoke`
// a lease can be refreshed by calling Lease() again on the same resource
Lease(r string, d time.Duration, revoke func())
// return the resource r.
Return(r string)
}
type leaser struct {
timers map[string]*time.Timer
}
// NewLeaser creates a new lease leaser
func NewLeaser() Leaser {
return &leaser{
timers: make(map[string]*time.Timer),
}
}
func (l *leaser) Lease(r string, d time.Duration, revoke func()) {
timer := time.AfterFunc(d, revoke)
// cancel the old timer and add the new one
if t, ok := l.timers[r]; ok {
t.Stop()
}
l.timers[r] = timer
}
func (l *leaser) Return(r string) {
if t, ok := l.timers[r]; ok {
t.Stop()
}
}