portalloc

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package portalloc provides a central port allocator for Gopherstack services. Services that expose real network endpoints (e.g., ElastiCache, Lambda function URLs) can request a dedicated port from the pool and release it when the resource is deleted.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidRange = errors.New("invalid port range: start must be ≥ 1 and end > start")

ErrInvalidRange is returned when the port range is invalid.

View Source
var ErrNoPortsAvailable = errors.New("no ports available in range")

ErrNoPortsAvailable is returned when the pool has no free ports.

View Source
var ErrPortAlreadyReserved = errors.New("port already reserved or allocated")

ErrPortAlreadyReserved is returned by Reserve when port is already marked used (by a prior Reserve or Acquire call).

View Source
var ErrPortNotAllocated = errors.New("port not allocated")

ErrPortNotAllocated is returned when trying to release a port that was not allocated.

Functions

func AcquireEphemeral

func AcquireEphemeral() (int, error)

AcquireEphemeral probes the OS for a free loopback port and records it process-wide, so no other caller is handed the same number until ReleaseEphemeral.

func AcquireFrom

func AcquireFrom(alloc *Allocator, label string) (int, error)

AcquireFrom takes a port from alloc when non-nil, else a process-unique ephemeral loopback port. Pair with ReleaseTo.

func IsListening

func IsListening(addr string) bool

IsListening performs a TCP health check on the given address to detect zombie listeners — ports that are allocated but no longer serving connections. Returns true if a listener is detected.

func ListenTCP

func ListenTCP(ctx context.Context, port int) (net.Listener, int, error)

ListenTCP binds ":port" and returns the listener with the port actually bound. Port 0 asks the OS for an ephemeral port, so callers never pre-pick one.

func ReleaseEphemeral

func ReleaseEphemeral(port int)

ReleaseEphemeral makes a port from AcquireEphemeral reusable.

func ReleaseTo

func ReleaseTo(alloc *Allocator, port int)

ReleaseTo returns a port taken by AcquireFrom. Zero and unknown ports are ignored.

Types

type Allocator

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

Allocator manages a pool of ports within a configurable range. It is safe for concurrent use.

func New

func New(start, end int) (*Allocator, error)

New creates a new Allocator for the half-open range [start, end). start must be ≥ 1 and end must be > start.

func (*Allocator) Acquire

func (a *Allocator) Acquire(label string) (int, error)

Acquire returns the next free port in the range and associates it with label. Returns ErrNoPortsAvailable when the pool is exhausted.

func (*Allocator) Allocated

func (a *Allocator) Allocated() map[int]string

Allocated returns a snapshot of allocated ports and out-of-range reserved fixed ports, with labels.

func (*Allocator) Available

func (a *Allocator) Available() int

Available returns the number of unallocated ports in the range.

func (*Allocator) IsAllocated

func (a *Allocator) IsAllocated(port int) bool

IsAllocated reports whether port is currently allocated.

func (*Allocator) Release

func (a *Allocator) Release(port int) error

Release returns a previously allocated port back to the pool. Returns ErrPortNotAllocated if the port was not allocated.

func (*Allocator) Reserve

func (a *Allocator) Reserve(port int, label string) error

Reserve permanently marks port as unavailable in the pool, associating it with label, without actually binding anything -- for services that bind a fixed port of their own outside this allocator entirely (e.g. a protocol-conventional default port), so Acquire never hands that same port to a different caller and causes a surprise address-in-use failure later. Intended to be called once at startup, before any Acquire calls.

A port outside [start, end) is not tracked in the pool (Acquire never considers it) but is remembered for reporting via Allocated. Returns ErrPortAlreadyReserved if port is already marked used.

func (*Allocator) Reserved

func (a *Allocator) Reserved() map[int]string

Reserved returns the ports marked via Reserve (in or out of range), with labels.

Directories

Path Synopsis
Package portalloctest hands tests port allocators over disjoint, probed-free blocks.
Package portalloctest hands tests port allocators over disjoint, probed-free blocks.

Jump to

Keyboard shortcuts

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