github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/lease/config.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/errors"
    10  	"github.com/juju/utils/clock"
    11  
    12  	"github.com/juju/juju/core/lease"
    13  )
    14  
    15  // Secretary is reponsible for validating the sanity of lease and holder names
    16  // before bothering the manager with them.
    17  type Secretary interface {
    18  
    19  	// CheckLease returns an error if the supplied lease name is not valid.
    20  	CheckLease(name string) error
    21  
    22  	// CheckHolder returns an error if the supplied holder name is not valid.
    23  	CheckHolder(name string) error
    24  
    25  	// CheckDuration returns an error if the supplied duration is not valid.
    26  	CheckDuration(duration time.Duration) error
    27  }
    28  
    29  // ManagerConfig contains the resources and information required to create a
    30  // Manager.
    31  type ManagerConfig struct {
    32  
    33  	// Secretary is responsible for validating lease names and holder names.
    34  	Secretary Secretary
    35  
    36  	// Client is responsible for recording, retrieving, and expiring leases.
    37  	Client lease.Client
    38  
    39  	// Clock is reponsible for reporting the passage of time.
    40  	Clock clock.Clock
    41  
    42  	// MaxSleep is the longest time the Manager should sleep before
    43  	// refreshing its client's leases and checking for expiries.
    44  	MaxSleep time.Duration
    45  }
    46  
    47  // Validate returns an error if the configuration contains invalid information
    48  // or missing resources.
    49  func (config ManagerConfig) Validate() error {
    50  	if config.Secretary == nil {
    51  		return errors.NotValidf("nil Secretary")
    52  	}
    53  	if config.Client == nil {
    54  		return errors.NotValidf("nil Client")
    55  	}
    56  	if config.Clock == nil {
    57  		return errors.NotValidf("nil Clock")
    58  	}
    59  	if config.MaxSleep <= 0 {
    60  		return errors.NotValidf("non-positive MaxSleep")
    61  	}
    62  	return nil
    63  }