github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/lease/block.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package lease 5 6 // block is used to deliver lease-expiry-notification requests to a manager's 7 // loop goroutine on behalf of BlockUntilLeadershipReleased. 8 type block struct { 9 leaseName string 10 unblock chan struct{} 11 abort <-chan struct{} 12 } 13 14 // invoke sends the block request on the supplied channel, and waits for the 15 // unblock channel to be closed. 16 func (b block) invoke(ch chan<- block) error { 17 for { 18 select { 19 case <-b.abort: 20 return errStopped 21 case ch <- b: 22 ch = nil 23 case <-b.unblock: 24 return nil 25 } 26 } 27 } 28 29 // blocks is used to keep track of expiry-notification channels for 30 // each lease name. 31 type blocks map[string][]chan struct{} 32 33 // add records the block's unblock channel under the block's lease name. 34 func (b blocks) add(block block) { 35 b[block.leaseName] = append(b[block.leaseName], block.unblock) 36 } 37 38 // unblock closes all channels added under the supplied name and removes 39 // them from blocks. 40 func (b blocks) unblock(leaseName string) { 41 unblocks := b[leaseName] 42 delete(b, leaseName) 43 for _, unblock := range unblocks { 44 close(unblock) 45 } 46 }