package pool

import "aqwari.net/net/styx/internal/pool"

Package pool manages pools of integer identifiers.

The pool package provides a thread-safe allocator for unique 32-bit integers. It is used to manage fid and tag pools for 9P clients.

Index

Constants

const (
	FidPoolCeiling = 1<<32 - 1
	TagPoolCeiling = 1<<16 - 1
)

Types

type FidPool

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

A FidPool maintains a pool of free identifiers. It is safe for concurrent use. The zero value of a FidPool is an empty pool that will provide identifiers in the range [0, DefaultFidPoolSize).

func (*FidPool) Free

func (p *FidPool) Free(old uint32)

Free releases a fid. After Free returns, it is valid for subsequent calls to Get on the same pool to return old. Free may only be called once for any given fid.

func (*FidPool) Get

func (p *FidPool) Get() (fid uint32, ok bool)

Get retrieves a free identifier from a FidPool. If the pool is full, the second return value of Get will be false. Once an identifier is no longer needed, it must be released using the Free method. The return value is guaranteed to be less than FidPoolCeiling.

func (*FidPool) MustGet

func (p *FidPool) MustGet() uint32

MustGet is like Get, but panics if the pool is full.

type TagPool

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

A TagPool is suitable for allocating tags for 9P messages.

func (*TagPool) Free

func (p *TagPool) Free(old uint16)

Free releases a tag. After Free returns, it is valid for subsequent calls to Get on the same pool to return old. Free may only be called once for any given tag.

func (*TagPool) Get

func (p *TagPool) Get() (tag uint16, ok bool)

Get retrieves a free identifier from a TagPool. If the pool is full, the second return value of Get will be false. Once an identifier is no longer needed, it must be released using the Free method. The return value is guaranteed to be less than TagPoolCeiling.

func (*TagPool) MustGet

func (p *TagPool) MustGet() uint16

MustGet is like Get, but panics if the pool is full.

Bugs

☞ The pool implementation allocates numbers in a contiguous sequence from [0, max). When a number X is Free'd, but is not at the end of the sequence, the FidPool implementation cannot use it until all allocated numbers greater than X have also been freed. While this can result in FidPools becoming full prematurely for certain pathological workloads, this tradeoff allows a FidPool to be simple, and small, and allows the Get implementation to be lock-free. Because clients are responsible for managing their own pools of identifiers, it cannot exploit this implementation to harm a server.

Source Files

pool.go

Version
v0.0.0-20221011015736-bf55d759d56b (latest)
Published
Oct 11, 2022
Platform
linux/amd64
Imports
3 packages
Last checked
4 days ago

Tools for package owners.