github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/lease/claim.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lease
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/juju/core/lease"
    10  )
    11  
    12  // claim is used to deliver lease-claim requests to a manager's loop
    13  // goroutine on behalf of ClaimLeadership.
    14  type claim struct {
    15  	leaseName  string
    16  	holderName string
    17  	duration   time.Duration
    18  	response   chan bool
    19  	abort      <-chan struct{}
    20  }
    21  
    22  // invoke sends the claim on the supplied channel and waits for a response.
    23  func (c claim) invoke(ch chan<- claim) error {
    24  	for {
    25  		select {
    26  		case <-c.abort:
    27  			return errStopped
    28  		case ch <- c:
    29  			ch = nil
    30  		case success := <-c.response:
    31  			if !success {
    32  				return lease.ErrClaimDenied
    33  			}
    34  			return nil
    35  		}
    36  	}
    37  }
    38  
    39  // respond causes the supplied success value to be sent back to invoke.
    40  func (c claim) respond(success bool) {
    41  	select {
    42  	case <-c.abort:
    43  	case c.response <- success:
    44  	}
    45  }