github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/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  	leaseKey   lease.Key
    16  	holderName string
    17  	duration   time.Duration
    18  	response   chan error
    19  	stop       <-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.stop:
    27  			return errStopped
    28  		case ch <- c:
    29  			ch = nil
    30  		case err := <-c.response:
    31  			return err
    32  		}
    33  	}
    34  }
    35  
    36  // respond causes the supplied success value to be sent back to invoke.
    37  func (c claim) respond(err error) {
    38  	select {
    39  	case <-c.stop:
    40  	case c.response <- err:
    41  	}
    42  }
    43  
    44  // revoke is used to deliver lease-revoke requests to a manager's loop
    45  // goroutine on behalf of RevokeLeadership.
    46  type revoke struct {
    47  	leaseKey   lease.Key
    48  	holderName string
    49  	response   chan error
    50  	stop       <-chan struct{}
    51  }
    52  
    53  // invoke sends the revocation on the supplied channel and waits for a response.
    54  func (c revoke) invoke(ch chan<- revoke) error {
    55  	for {
    56  		select {
    57  		case <-c.stop:
    58  			return errStopped
    59  		case ch <- c:
    60  			ch = nil
    61  		case err := <-c.response:
    62  			return err
    63  		}
    64  	}
    65  }
    66  
    67  // respond causes the supplied success value to be sent back to invoke.
    68  func (c revoke) respond(err error) {
    69  	select {
    70  	case <-c.stop:
    71  	case c.response <- err:
    72  	}
    73  }