flowtable

package
v0.0.0-...-466b026 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package flowtable manages datagram flow registration, acceptance and expiry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteAddrPort

func WriteAddrPort(h *maphash.Hash, ap netip.AddrPort)

WriteAddrPort writes an endpoint to a shard hash. The address family and length of the zone distinguish components when several endpoints are hashed.

Types

type Membership

type Membership[K, P comparable] struct {
	// contains filtered or unexported fields
}

Membership records a flow's registration and expiry position. The table owns it. A flow supplies its own zero Membership and keeps it for life.

type Result

type Result uint8

Result is the outcome of adding a flow.

const (
	Added  Result = iota // the flow was added
	Exists               // an existing flow took precedence
	Full                 // the limit or the backlog refused the flow
	Closed               // the table is closed
)

The results of adding a flow.

type Table

type Table[K, P, S comparable] struct {
	// contains filtered or unexported fields
}

Table holds flows by primary key, optionally with a secondary peer index. Get uses a sync.Map and takes no lock. Mutations to a peer's keys and index take the same shard lock. Without an index, mutations shard by primary key. A zero Table is empty and cannot register flows. Accept waits for Close, and Close releases it with net.ErrClosed.

func (*Table[K, P, S]) Accept

func (t *Table[K, P, S]) Accept() (S, error)

Accept waits for a new flow, skipping any closed meanwhile. It returns net.ErrClosed once the table is closed.

func (*Table[K, P, S]) Add

func (t *Table[K, P, S]) Add(k K, s S, backlog bool) (S, Result)

Add registers s under k and, with backlog, queues it for Accept.

func (*Table[K, P, S]) AddOrBind

func (t *Table[K, P, S]) AddOrBind(k K, s S, prepare func(S)) (S, Result)

AddOrBind looks up k, then binds a peer's unbound flow if prepare is supplied, then tries to add s to the backlog. It returns Exists when either lookup or binding found the flow. See Resolve for prepare.

func (*Table[K, P, S]) AddUnbound

func (t *Table[K, P, S]) AddUnbound(p P, s S) (S, Result)

AddUnbound registers a flow that does not have a primary key yet. It succeeds only if p has no flows. An unbound flow holds a slot and is not delivered by Accept. A table without a peer index refuses it with Closed.

func (*Table[K, P, S]) Close

func (t *Table[K, P, S]) Close(err error)

Close refuses new flows, stops the idle timeout and shuts down all flows with err for reads. Calls after the first do nothing.

func (*Table[K, P, S]) Done

func (t *Table[K, P, S]) Done() <-chan struct{}

Done returns a channel that Close closes.

func (*Table[K, P, S]) Get

func (t *Table[K, P, S]) Get(k K) (S, bool)

Get returns the flow under k.

func (*Table[K, P, S]) Init

func (t *Table[K, P, S]) Init(o TableOptions[K, P, S])

Init sets t up. Call it once, before the other methods.

func (*Table[K, P, S]) Len

func (t *Table[K, P, S]) Len() int

Len returns the number of flows held, including unbound flows.

func (*Table[K, P, S]) Pending

func (t *Table[K, P, S]) Pending() int

Pending returns the number of flows waiting for Accept.

func (*Table[K, P, S]) Remove

func (t *Table[K, P, S]) Remove(s S)

Remove removes s whether it has a primary key or is awaiting one. The registration record identifies its shard, peer and current key.

func (*Table[K, P, S]) Resolve

func (t *Table[K, P, S]) Resolve(k K, prepare func(S)) S

Resolve returns the flow under k, or binds a peer's unbound flow to k. prepare configures that flow before it becomes visible under k. It runs under the shard lock and must be an internal operation that does not call back into the table or any user code.

func (*Table[K, P, S]) Room

func (t *Table[K, P, S]) Room() bool

Room reports whether a new flow could wait for Accept now. Add checks the limit and backlog again when it registers a flow.

func (*Table[K, P, S]) SetIdle

func (t *Table[K, P, S]) SetIdle(d time.Duration)

SetIdle changes the idle timeout for the flows already held and for new flows. A non-positive duration stops it. It does nothing before Init or after Close.

type TableOptions

type TableOptions[K, P, S comparable] struct {
	Queue func(S) *dgram.Queue
	// Shutdown closes a flow with an error for reads and removes it.
	// It must be safe to call more than once.
	Shutdown func(S, error)
	// Membership returns the flow's registration record.
	Membership func(S) *Membership[K, P]
	// HashKey selects the shard for a table without a peer index.
	HashKey func(maphash.Seed, K) uint64
	// Peer and HashPeer enable indexing. Supply both or neither. Keys for
	// the same peer are assigned to the same shard.
	Peer     func(K) P
	HashPeer func(maphash.Seed, P) uint64
	// Epoch is the time base of the queues and their idle timeout. Nil
	// uses the system clock's epoch.
	Epoch *clock.Epoch
	// Backlog is the number of connections waiting for Accept, at least one.
	Backlog int
	// Limit bounds the number of connections held, including unbound flows.
	// A non-positive limit adds no bound.
	Limit int
	// Idle shuts down accepted flows with io.EOF after this much inactivity.
	// A non-positive duration keeps idle flows.
	Idle time.Duration
}

TableOptions configures Table.Init.

Jump to

Keyboard shortcuts

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