github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/fortress/interface.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package fortress
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  )
     9  
    10  // Guard manages Guest access to a fortress.
    11  type Guard interface {
    12  
    13  	// Unlock unblocks all Guest.Visit calls.
    14  	Unlock() error
    15  
    16  	// Lockdown blocks new Guest.Visit calls, and waits for existing calls to
    17  	// complete; it will return ErrAborted if the supplied Abort is closed
    18  	// before lockdown is complete. In this situation, the fortress will
    19  	// remain closed to new visits, but may still be executing pre-existing
    20  	// ones; you need to wait for a Lockdown to complete successfully before
    21  	// you can infer exclusive access.
    22  	Lockdown(Abort) error
    23  }
    24  
    25  // Guest allows clients to Visit a fortress when it's unlocked; that is, to
    26  // get non-exclusive access to whatever resource is being protected for the
    27  // duration of the supplied Visit func.
    28  type Guest interface {
    29  
    30  	// Visit waits until the fortress is unlocked, then runs the supplied
    31  	// Visit func. It will return ErrAborted if the supplied Abort is closed
    32  	// before the Visit is started.
    33  	Visit(Visit, Abort) error
    34  }
    35  
    36  // Visit is an operation that can be performed by a Guest.
    37  type Visit func() error
    38  
    39  // Abort is a channel that can be closed to abort a blocking operation.
    40  type Abort <-chan struct{}
    41  
    42  // ErrAborted is used to confirm clean termination of a blocking operation.
    43  var ErrAborted = errors.New("fortress operation aborted")
    44  
    45  // ErrShutdown is used to report that the fortress worker is shutting down.
    46  var ErrShutdown = errors.New("fortress worker shutting down")
    47  
    48  // IsFortressError returns true if the error provided is fortress related.
    49  func IsFortressError(err error) bool {
    50  	switch errors.Cause(err) {
    51  	case ErrAborted, ErrShutdown:
    52  		return true
    53  	default:
    54  		return false
    55  	}
    56  }