github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/fortress/manifold.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  	"gopkg.in/juju/worker.v1"
     9  	"gopkg.in/juju/worker.v1/dependency"
    10  )
    11  
    12  // Manifold returns a dependency.Manifold that runs a fortress.
    13  //
    14  // Clients should access the fortress resource via Guard and/or Guest pointers.
    15  // Guest.Visit calls will block until a Guard.Unlock call is made; Guard.Lockdown
    16  // calls will block new Guest.Visits and wait until all active Visits complete.
    17  //
    18  // If multiple clients act as guards, the fortress' state at any time will be
    19  // determined by whichever guard last ran an operation; that is to say, it will
    20  // be impossible to reliably tell from outside. So please don't do that.
    21  func Manifold() dependency.Manifold {
    22  	return dependency.Manifold{
    23  		Start: func(_ dependency.Context) (worker.Worker, error) {
    24  			return newFortress(), nil
    25  		},
    26  		Output: func(in worker.Worker, out interface{}) error {
    27  			inFortress, _ := in.(*fortress)
    28  			if inFortress == nil {
    29  				return errors.Errorf("in should be %T; is %T", inFortress, in)
    30  			}
    31  			switch outPointer := out.(type) {
    32  			case *Guard:
    33  				*outPointer = inFortress
    34  			case *Guest:
    35  				*outPointer = inFortress
    36  			default:
    37  				return errors.Errorf("out should be *fortress.Guest or *fortress.Guard; is %T", out)
    38  			}
    39  			return nil
    40  		},
    41  	}
    42  }