github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/lease/pin.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lease
     5  
     6  import (
     7  	"github.com/juju/juju/core/lease"
     8  )
     9  
    10  // pin is used to deliver lease pinning and unpinning requests to a manager's
    11  // worker loop on behalf of PinLeadership and UnpinLeadership.
    12  type pin struct {
    13  	leaseKey lease.Key
    14  	entity   string
    15  	response chan error
    16  	stop     <-chan struct{}
    17  }
    18  
    19  // invoke sends the claim on the supplied channel and waits for a response.
    20  func (c pin) invoke(ch chan<- pin) error {
    21  	for {
    22  		select {
    23  		case <-c.stop:
    24  			return errStopped
    25  		case ch <- c:
    26  			ch = nil
    27  		case err := <-c.response:
    28  			return err
    29  		}
    30  	}
    31  }
    32  
    33  // respond causes the supplied success value to be sent back to invoke.
    34  func (c pin) respond(err error) {
    35  	select {
    36  	case <-c.stop:
    37  	case c.response <- err:
    38  	}
    39  }