forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
51 lines (44 loc) · 1.25 KB
/
block.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package lease
import "github.com/juju/juju/core/lease"
// block is used to deliver lease-expiry-notification requests to a manager's
// loop goroutine on behalf of BlockUntilLeadershipReleased.
type block struct {
leaseKey lease.Key
unblock chan struct{}
stop <-chan struct{}
cancel <-chan struct{}
}
// invoke sends the block request on the supplied channel, and waits for the
// unblock channel to be closed.
func (b block) invoke(ch chan<- block) error {
for {
select {
case <-b.stop:
return errStopped
case <-b.cancel:
return lease.ErrWaitCancelled
case ch <- b:
ch = nil
case <-b.unblock:
return nil
}
}
}
// blocks is used to keep track of expiry-notification channels for
// each lease key.
type blocks map[lease.Key][]chan struct{}
// add records the block's unblock channel under the block's lease key.
func (b blocks) add(block block) {
b[block.leaseKey] = append(b[block.leaseKey], block.unblock)
}
// unblock closes all channels added under the supplied key and removes
// them from blocks.
func (b blocks) unblock(lease lease.Key) {
unblocks := b[lease]
delete(b, lease)
for _, unblock := range unblocks {
close(unblock)
}
}