lease

package
Version: v0.0.0-...-6f06747 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const MinimumRefresh = time.Second

MinimumRefresh is the minimum amount of time that should pass between lease refresh attempts. It is a hard limit intended to avoid hammering the server when policies are misconfigured.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action uint32

Action is a type of lease operation

const (
	None Action = iota
	Create
	Update
	Delete
)

Lease action types

func (Action) String

func (t Action) String() string

String returns a string representation of the action type.

type Effect

type Effect struct {
	Action Action
	Lease
}

Effect is an effect of a transaction.

func (*Effect) String

func (e *Effect) String() string

String returns a string representation of the effect.

type Instance

type Instance struct {
	Host string `json:"host,omitempty"`
	User string `json:"user,omitempty"`
	ID   string `json:"id,omitempty"`
}

An Instance identifies a specific instance of a lease consumer.

func (Instance) Empty

func (i Instance) Empty() bool

Empty returns true if the instance is its zero value.

func (Instance) String

func (i Instance) String() string

String returns a string representation of the instance.

type Iter

type Iter struct {
	Lease
	// contains filtered or unexported fields
}

Iter is a lease iterator that allows changes to be recorded in a transaction.

func (*Iter) Delete

func (iter *Iter) Delete()

Delete will delete the lease and record the deletion in the transaction.

func (*Iter) Update

func (iter *Iter) Update()

Update will update the lease and record the update in the transaction.

type Lease

type Lease struct {
	Subject
	Properties Properties        `json:"properties"` // Properties of the lease
	Status     Status            `json:"status"`
	Started    time.Time         `json:"started,omitempty"`
	Renewed    time.Time         `json:"renewed,omitempty"`
	Released   time.Time         `json:"released,omitempty"`
	Strategy   strategy.Strategy `json:"strategy,omitempty"`
	Limit      uint              `json:"limit"`
	Duration   time.Duration     `json:"duration"`
	Decay      time.Duration     `json:"decay"`
	Refresh    Refresh           `json:"refresh,omitempty"`
}

Lease describes a single assignment of a leased resource.

func Clone

func Clone(from Lease) (to Lease)

Clone returns a deep copy of the lease.

func (*Lease) Consumptive

func (ls *Lease) Consumptive() (matched bool)

Consumptive returns true if the lease is active or released.

func (*Lease) DecayTime

func (ls *Lease) DecayTime() time.Time

DecayTime returns the time at which the lease decays.

func (*Lease) Decayed

func (ls *Lease) Decayed(at time.Time) bool

Decayed returns true if the lease will be fully decayed at the given time.

func (*Lease) EffectiveRefresh

func (ls *Lease) EffectiveRefresh() (interval time.Duration)

EffectiveRefresh returns the effective refresh interval for the lease.

If the lease refresh interval is non-zero, it will be returned. If the refresh interval is zero a computed interval of half the lease duration will will be returned instead.

The returned value will always be greater than or equal to the minimum refresh rate defined by MinimumRefresh.

func (*Lease) ExpirationTime

func (ls *Lease) ExpirationTime() time.Time

ExpirationTime returns the time at which the lease expires.

func (*Lease) Expired

func (ls *Lease) Expired(at time.Time) bool

Expired returns true if the lease will be expired at the given time.

func (*Lease) MatchHostUser

func (ls *Lease) MatchHostUser(resource, host, user string) (matched bool)

MatchHostUser returns true if the lease is for the given resource, host and user.

func (*Lease) MatchInstance

func (ls *Lease) MatchInstance(resource string, inst Instance) (matched bool)

MatchInstance returns true if the lease is for the given resource and instance.

func (*Lease) MatchStatus

func (ls *Lease) MatchStatus(status Status) (matched bool)

MatchStatus returns true if the lease has the given status.

func (*Lease) ResourceName

func (ls *Lease) ResourceName() string

ResourceName returns the user-friendly name of the resource.

type Op

type Op struct {
	Type     Action
	Previous Lease
	Lease    Lease
}

Op is a lease operation describing a create, update or delete action

func (*Op) Consumptive

func (op *Op) Consumptive() bool

Consumptive returns true if the operation affects a consumptive lease.

func (*Op) Effects

func (op *Op) Effects() (effects []Effect)

Effects returns a set of strings describing the effects of the operation.

func (*Op) UpdateType

func (op *Op) UpdateType() UpdateType

UpdateType returns the type of update for update operations.

type Processor

type Processor func(iter *Iter)

Processor is a function that processes a lease iterator.

type Properties

type Properties map[string]string

Properties is a key/value map of lease properties.

func MergeProperties

func MergeProperties(props ...Properties) (merged Properties)

MergeProperties merges the given properties into a single property map. The source properties are not modified.

func (Properties) Clone

func (p Properties) Clone() Properties

Clone returns a deep clone of the lease properties.

type Provider

type Provider interface {
	// ProviderName returns the name of the provider.
	ProviderName() string

	// LeaseResources returns all of the resources with lease data.
	LeaseResources() (resources []string, err error)

	// LeaseView returns the current revision and lease set for the resource.
	LeaseView(resource string) (revision uint64, leases Set, err error)

	// LeaseCommit will attempt to commit the lease transaction.
	LeaseCommit(tx *Tx) (err error)

	// Close releases any resources consumed by the provider.
	Close() error
}

Provider is a lease management interface. It provides access to transactions for specific resources.

type Refresh

type Refresh struct {
	Active time.Duration `json:"active,omitempty"` // Active lease refresh interval
	Queued time.Duration `json:"queued,omitempty"` // Queued lease refresh interval
}

Refresh defines the active and queued refresh rates for a lease.

type Set

type Set []Lease

Set is a set of leases.

func (Set) DecayDuration

func (s Set) DecayDuration(at time.Time) (duration time.Duration)

DecayDuration returns the interval between the given time and the earliest time at which a member of the set will decay.

If the decay time is before the given time a zero duration will be returned.

func (Set) DecayTime

func (s Set) DecayTime() (decay time.Time)

DecayTime returns the earliest time at which a member of the set will decay.

func (Set) ExpirationTime

func (s Set) ExpirationTime() (expiration time.Time)

ExpirationTime returns the earliest time at which a member of the set will expire.

func (Set) HostUser

func (s Set) HostUser(resource, host, user string) (matched Set)

HostUser returns the set of leases matching the requested resource, host and user.

func (Set) Index

func (s Set) Index(resource string, instance Instance) (index int)

Index returns the index of the first lease within s that matches the given parameters, or -1 if no such lease is present in s.

func (Set) Instance

func (s Set) Instance(resource string, instance Instance) (ls Lease, found bool)

Instance returns the first lease that matches the requested resource and instance.

func (Set) Len

func (s Set) Len() int

Len is the number of leases in the collection.

func (Set) Less

func (s Set) Less(i, j int) bool

Less reports whether the lease with index i should sort before the lease with index j.

Leases of greater permanence come before leases of lesser permanence.

func (Set) Property

func (s Set) Property(keys ...string) (values []string)

Property returns a slice of property values from the leases. Keys are are supplied in preferential order, and the first key in each lease that exists is returned as the value for that key.

func (Set) Stats

func (s Set) Stats() (stats Stats)

Stats returns the resource consumption statistics for each resource counting strategy.

The set must be sorted prior to calling this function.

func (Set) Status

func (s Set) Status(status Status) (matched Set)

Status returns the subset of leases with the requested status.

func (Set) Swap

func (s Set) Swap(i, j int)

Swap swaps the leases with indices i and j.

func (Set) User

func (s Set) User(user string) (matched Set)

User returns the set of leases for the given user.

type Snapshot

type Snapshot struct {
	Resource string `json:"resource"`
	Revision uint64 `json:"revision"`
	Leases   Set    `json:"leases"`
}

Snapshot includes a set of leases for a particular resource at a revision.

type State

type State struct {
	Online           bool          // Do we have a live connection to the guardian server?
	LeaseNotRequired bool          // Did the server tell us we don't need a lease?
	Acquired         bool          // Have we acquired a lease of any status?
	Lease            Lease         // The most recent lease received from the server
	Leases           Set           // All leases for our lease resource
	Retry            time.Duration // Retry interval when not holding a lease
	Err              error         // The most recent acquisition error
}

State holds state information about a lease for a lease holder.

func (*State) IsZero

func (s *State) IsZero() bool

IsZero returns true if the state holds a zero value.

type Stats

type Stats struct {
	Instance Tally // The number of leases with each lease status
	Consumer Tally // The number of consumers with each lease status
}

Stats is a set of resource consumption statistics for each resource counting strategy.

func (*Stats) Active

func (s *Stats) Active(strat strategy.Strategy) uint

Active returns the number of active resources according to the provided resource counting strategy.

func (*Stats) Consumed

func (s *Stats) Consumed(strat strategy.Strategy) uint

Consumed returns the number of consumed resources according to the provided resource counting strategy.

Both both active and released resources contribute towards consumption.

func (*Stats) Queued

func (s *Stats) Queued(strat strategy.Strategy) uint

Queued returns the number of queued resources according to the provided resource counting strategy.

func (*Stats) Released

func (s *Stats) Released(strat strategy.Strategy) uint

Released returns the number of released resources according to the provided resource counting strategy.

func (*Stats) Users

func (s *Stats) Users(strat strategy.Strategy) map[string]uint

Users returns a map of users and the number of resources consumed by each according to the provided resource counting strategy.

type Status

type Status string

Status indicates the current condition of a lease.

const (
	// Queued indicates that a lease is pending and does not yet count against
	// the resource allocation counts.
	Queued Status = "queued"

	// Active indicates that a lease is in use and included in the resource
	// allocation counts.
	Active Status = "active"

	// Released indicates that a lease has ended but is in a state of decay.
	// Decaying leases are still included in resource allocation counts.
	Released Status = "released"
)

func (Status) Order

func (s Status) Order() int

Order returns an ordinal value reflecting the status' sort order. The order is:

0: Active
1: Released
2: Queued
3: (any invalid or unrecognized status)

type Subject

type Subject struct {
	Resource string   `json:"resource"` // The resource that is being consumed
	Instance Instance `json:"instance"` // The thing that is consuming the resource
}

Subject describes what a lease consumes and what consumes it.

func (Subject) Empty

func (s Subject) Empty() bool

Empty returns true if the subject is its zero value.

func (Subject) HostUser

func (s Subject) HostUser() string

HostUser returns a combination of host and user.

func (Subject) String

func (s Subject) String() string

String returns a string representation of the subject.

type Tally

type Tally struct {
	Active   uint
	Released uint
	Queued   uint
	Consumed uint
	Users    map[string]uint
}

Tally is a set of resource statistics for a particular resource counting strategy.

func (*Tally) Add

func (t *Tally) Add(user string, status Status)

Add will increase the tally for the specified status.

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx is a lease transaction that describes a series of operations to be atomically applied to a lease set.

func NewTx

func NewTx(resource string, revision uint64, leases Set) *Tx

NewTx creates a new transaction for the given resource, revision and lease set.

func (*Tx) Create

func (tx *Tx) Create(ls Lease) error

Create will add the given lease to the set.

func (*Tx) Delete

func (tx *Tx) Delete(instance Instance) error

Delete will remove the lease from the set.

func (*Tx) Effects

func (tx *Tx) Effects() (effects []Effect)

Effects returns a set of strings describing the effects of the transaction.

func (*Tx) Empty

func (tx *Tx) Empty() bool

Empty returns true if the transaction is empty

func (*Tx) HostUser

func (tx *Tx) HostUser(host, user string) (matched Set)

HostUser returns the set of leases matching the requested host and user.

func (*Tx) Instance

func (tx *Tx) Instance(instance Instance) (ls Lease, found bool)

Instance returns the first lease that matches the given instance.

func (*Tx) Leases

func (tx *Tx) Leases() Set

Leases returns the lease set that the transaction will produce.

func (*Tx) Ops

func (tx *Tx) Ops() []Op

Ops returns the series of operations encoded in the transaction.

func (*Tx) Process

func (tx *Tx) Process(process Processor)

Process iterates through each lease and applies the given lease processing function to it.

func (*Tx) ProcessReverse

func (tx *Tx) ProcessReverse(process Processor)

ProcessReverse iterates through each lease in reverse order and applies the given lease processing function to it.

func (*Tx) Release

func (tx *Tx) Release(instance Instance, at time.Time) error

Release will change the status of the lease to released.

func (*Tx) Resource

func (tx *Tx) Resource() string

Resource returns the resource the transaction will operate on.

func (*Tx) Revision

func (tx *Tx) Revision() uint64

Revision returns the revision of the lease set that the transaction is based on.

func (*Tx) Stats

func (tx *Tx) Stats() Stats

Stats returns the number of leases with each status.

func (*Tx) Update

func (tx *Tx) Update(instance Instance, ls Lease) error

Update will update the lease within the set.

type UpdateType

type UpdateType uint32

UpdateType is a type of lease update.

const (
	Renew UpdateType = iota
	Downgrade
	Upgrade
	Replace
	Exchange
	Transmute
)

Lease update types

func (UpdateType) String

func (t UpdateType) String() string

String returns a string representation of the update type.

Directories

Path Synopsis
Package leaseui provides a user interface for observing queued leases.
Package leaseui provides a user interface for observing queued leases.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL