ports

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: PostgreSQL Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinValidPort = 1
	MaxValidPort = 65535
)

Variables

View Source
var (
	ErrFull      = errors.New("range is full")
	ErrAllocated = errors.New("provided port is already allocated")
)

Functions

func DefaultPortChecker

func DefaultPortChecker(port int) bool

DefaultPortChecker returns true if the given TCP port is not currently bound by any process on the local host.

func Provide

func Provide(i *do.Injector)

Types

type ErrNotInRange

type ErrNotInRange struct {
	Min int
	Max int
}

func (*ErrNotInRange) Error

func (e *ErrNotInRange) Error() string

type PortChecker

type PortChecker func(port int) bool

PortChecker reports whether a port is available for binding.

type PortRange

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

PortRange tracks allocated ports across the full valid range [1, 65535] using a big.Int bitmap, where bit N represents port N. Random allocation draws only from [min, max], but any port in [1, 65535] can be recorded via Allocate. This means min and max can be reconfigured without affecting previously stored state.

func NewPortRange

func NewPortRange(min, max int) (*PortRange, error)

NewPortRange creates a PortRange for the given spec.

func (*PortRange) Allocate

func (r *PortRange) Allocate(port int) error

Allocate reserves the given port. Any port in [1, 65535] may be recorded, including ports outside [min, max]. ErrAllocated is returned if the port is already reserved.

func (*PortRange) AllocateNext

func (r *PortRange) AllocateNext() (int, error)

AllocateNext reserves a random unallocated port from [min, max]. ErrFull is returned if all ports in the range are allocated.

func (*PortRange) ForEach

func (r *PortRange) ForEach(fn func(int))

ForEach calls fn for every allocated port across the full valid range [1, 65535].

func (*PortRange) Free

func (r *PortRange) Free() int

Free returns the count of unallocated ports within [min, max].

func (*PortRange) Has

func (r *PortRange) Has(port int) bool

Has returns true if the given port is currently allocated.

func (*PortRange) Release

func (r *PortRange) Release(port int) error

Release clears the port's allocated bit. Out-of-range or unallocated ports are silently ignored.

func (*PortRange) Restore

func (r *PortRange) Restore(data []byte) error

Restore loads a previously saved bitmap. The spec in specStr must be valid JSON, but a min/max mismatch does not cause an error — the current range's min/max are preserved, allowing the configuration to be changed without losing allocation history.

func (*PortRange) Snapshot

func (r *PortRange) Snapshot() []byte

Snapshot saves the current allocation state. The spec string encodes the current min/max, and the data is a fixed-size big-endian bitmap of all 65535 ports.

func (*PortRange) Used

func (r *PortRange) Used() int

Used returns the count of allocated ports within [min, max].

type Service

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

func NewService

func NewService(
	cfg config.Config,
	loggerFactory *logging.Factory,
	store *Store,
	portChecker PortChecker,
	hostSvc *host.Service,
) *Service

func (*Service) AllocatePort

func (s *Service) AllocatePort(ctx context.Context, hostID string) (int, error)

AllocatePort allocates the next available port in [min, max] that is not already recorded in the persistent range and is not currently bound on the local host.

func (*Service) ReleasePort

func (s *Service) ReleasePort(ctx context.Context, hostID string, ports ...int) error

ReleasePort releases the given port back to the pool, persisting the updated state to storage.

func (*Service) ReleasePortIfDefined

func (s *Service) ReleasePortIfDefined(ctx context.Context, hostID string, ports ...*int) error

type Store

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

func NewStore

func NewStore(client *clientv3.Client, root string) *Store

func (*Store) ExistsByKey

func (s *Store) ExistsByKey(allocatorName string) storage.ExistsOp

func (*Store) GetByKey

func (s *Store) GetByKey(allocatorName string) storage.GetOp[*StoredPortRange]

func (*Store) Key

func (s *Store) Key(allocatorName string) string

func (*Store) Prefix

func (s *Store) Prefix() string

func (*Store) Update

func (s *Store) Update(item *StoredPortRange) storage.PutOp[*StoredPortRange]

type StoredPortRange

type StoredPortRange struct {
	storage.StoredValue
	Name     string `json:"name"`
	Snapshot []byte `json:"snapshot"`
}

Jump to

Keyboard shortcuts

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