Documentation ¶
Index ¶
- Variables
- type ErrNotInRange
- type Interface
- type PortAllocationOperation
- func (op *PortAllocationOperation) Allocate(port int) error
- func (op *PortAllocationOperation) AllocateNext() (int, error)
- func (op *PortAllocationOperation) Commit() []error
- func (op *PortAllocationOperation) Finish()
- func (op *PortAllocationOperation) ReleaseDeferred(port int)
- func (op *PortAllocationOperation) Rollback() []error
- type PortAllocator
- func (r *PortAllocator) Allocate(port int) error
- func (r *PortAllocator) AllocateNext() (int, error)
- func (r *PortAllocator) Destroy()
- func (r *PortAllocator) EnableMetrics()
- func (r *PortAllocator) ForEach(fn func(int))
- func (r *PortAllocator) Free() int
- func (r *PortAllocator) Has(port int) bool
- func (r *PortAllocator) Release(port int) error
- func (r *PortAllocator) Restore(pr net.PortRange, data []byte) error
- func (r *PortAllocator) Snapshot(dst *api.RangeAllocation) error
- func (r *PortAllocator) Used() int
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type ErrNotInRange ¶
type ErrNotInRange struct {
ValidPorts string
}
func (*ErrNotInRange) Error ¶
func (e *ErrNotInRange) Error() string
type Interface ¶
type Interface interface { Allocate(int) error AllocateNext() (int, error) Release(int) error ForEach(func(int)) Has(int) bool Destroy() EnableMetrics() }
Interface manages the allocation of ports out of a range. Interface should be threadsafe.
type PortAllocationOperation ¶ added in v1.8.0
type PortAllocationOperation struct {
// contains filtered or unexported fields
}
Encapsulates the semantics of a port allocation 'transaction': It is better to leak ports than to double-allocate them, so we allocate immediately, but defer release. On commit we best-effort release the deferred releases. On rollback we best-effort release any allocations we did.
Pattern for use:
op := StartPortAllocationOperation(...) defer op.Finish ... write(updatedOwner)
/ op.Commit()
func StartOperation ¶
func StartOperation(pa Interface, dryRun bool) *PortAllocationOperation
Creates a portAllocationOperation, tracking a set of allocations & releases If dryRun is specified, never actually allocate or release anything
func (*PortAllocationOperation) Allocate ¶ added in v1.8.0
func (op *PortAllocationOperation) Allocate(port int) error
Allocates a port, and record it for future rollback
func (*PortAllocationOperation) AllocateNext ¶ added in v1.8.0
func (op *PortAllocationOperation) AllocateNext() (int, error)
Allocates a port, and record it for future rollback
func (*PortAllocationOperation) Commit ¶ added in v1.8.0
func (op *PortAllocationOperation) Commit() []error
(Try to) perform any deferred operations. Note that even if this fails, we don't rollback; we always want to err on the side of over-allocation, and Commit should be called _after_ the owner is written
func (*PortAllocationOperation) Finish ¶ added in v1.8.0
func (op *PortAllocationOperation) Finish()
Will rollback unless marked as shouldRollback = false by a Commit(). Call from a defer block
func (*PortAllocationOperation) ReleaseDeferred ¶ added in v1.8.0
func (op *PortAllocationOperation) ReleaseDeferred(port int)
Marks a port so that it will be released if this operation Commits
func (*PortAllocationOperation) Rollback ¶ added in v1.8.0
func (op *PortAllocationOperation) Rollback() []error
(Try to) undo any operations we did
type PortAllocator ¶
type PortAllocator struct {
// contains filtered or unexported fields
}
func New ¶ added in v1.23.0
func New(pr net.PortRange, allocatorFactory allocator.AllocatorWithOffsetFactory) (*PortAllocator, error)
New creates a PortAllocator over a net.PortRange, calling allocatorFactory to construct the backing store.
func NewFromSnapshot ¶ added in v1.5.2
func NewFromSnapshot(snap *api.RangeAllocation) (*PortAllocator, error)
NewFromSnapshot allocates a PortAllocator and initializes it from a snapshot.
func NewInMemory ¶ added in v1.23.0
func NewInMemory(pr net.PortRange) (*PortAllocator, error)
NewInMemory creates an in-memory allocator.
func (*PortAllocator) Allocate ¶
func (r *PortAllocator) Allocate(port int) error
Allocate attempts to reserve the provided port. ErrNotInRange or ErrAllocated will be returned if the port is not valid for this range or has already been reserved. ErrFull will be returned if there are no ports left.
func (*PortAllocator) AllocateNext ¶
func (r *PortAllocator) AllocateNext() (int, error)
AllocateNext reserves one of the ports from the pool. ErrFull may be returned if there are no ports left.
func (*PortAllocator) Destroy ¶ added in v1.25.0
func (r *PortAllocator) Destroy()
Destroy shuts down internal allocator.
func (*PortAllocator) EnableMetrics ¶ added in v1.27.0
func (r *PortAllocator) EnableMetrics()
EnableMetrics enables metrics recording.
func (*PortAllocator) ForEach ¶ added in v1.5.2
func (r *PortAllocator) ForEach(fn func(int))
ForEach calls the provided function for each allocated port.
func (*PortAllocator) Free ¶
func (r *PortAllocator) Free() int
Free returns the count of port left in the range.
func (*PortAllocator) Has ¶
func (r *PortAllocator) Has(port int) bool
Has returns true if the provided port is already allocated and a call to Allocate(port) would fail with ErrAllocated.
func (*PortAllocator) Release ¶
func (r *PortAllocator) Release(port int) error
Release releases the port back to the pool. Releasing an unallocated port or a port out of the range is a no-op and returns no error.
func (*PortAllocator) Restore ¶
func (r *PortAllocator) Restore(pr net.PortRange, data []byte) error
Restore restores the pool to the previously captured state. ErrMismatchedNetwork is returned if the provided port range doesn't exactly match the previous range.
func (*PortAllocator) Snapshot ¶
func (r *PortAllocator) Snapshot(dst *api.RangeAllocation) error
Snapshot saves the current state of the pool.
func (*PortAllocator) Used ¶ added in v1.5.2
func (r *PortAllocator) Used() int
Used returns the count of ports used in the range.