inproc

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package inproc is the home of the in-process io tier: communication between goroutines of one process. Queue is its first primitive: the one typed FIFO an in-process store is built on, so no service hand-rolls a queue.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCycle reports an edge that would make an acyclic relation cyclic.
	ErrCycle = errors.New("inproc: edge would close a cycle")
	// ErrUnknownNode reports an edge end that was never added.
	ErrUnknownNode = errors.New("inproc: unknown node")
	// ErrDuplicateNode reports a node added twice.
	ErrDuplicateNode = errors.New("inproc: node already present")
	// ErrUnknownRelation reports an edge kind the graph was not built with.
	ErrUnknownRelation = errors.New("inproc: unknown relation")
	// ErrNoRelations reports a graph built with no edge kinds.
	ErrNoRelations = errors.New("inproc: a graph needs at least one relation")
)
View Source
var ErrQueueClosed = errors.New("inproc: queue is closed")

ErrQueueClosed reports a Push after Close, or a Pop on a closed queue that has drained.

Functions

This section is empty.

Types

type Graph

type Graph[N comparable, E comparable] struct {
	// contains filtered or unexported fields
}

Graph is a store of nodes N joined by edges of kinds E, each kind with its own Relation. It is the one graph shape in-process stores are built on: a slice graph's depends_on (directed, acyclic) and contends (undirected) are two kinds on one graph. Nodes keep insertion order, which is the tie-break every query uses, so results are deterministic.

The lock guards the adjacency and every mutation is a single step that calls nothing it does not control, so it is a leaf (house rule CS-5). Queries that take a caller's function run it on a snapshot, outside the lock.

func NewGraph

func NewGraph[N comparable, E comparable](relations map[E]Relation) (*Graph[N, E], error)

NewGraph returns an empty graph with the given edge kinds.

func (*Graph[N, E]) AddEdge

func (graph *Graph[N, E]) AddEdge(kind E, from N, to N) error

AddEdge joins from and to with an edge of kind. A directed kind reads from -> to; an undirected kind joins both ways. It rejects an unknown kind or end, a self-edge, and, for an acyclic kind, an edge that would close a cycle; a repeated edge is accepted once.

func (*Graph[N, E]) AddNode

func (graph *Graph[N, E]) AddNode(n N) error

AddNode adds n with no edges.

func (*Graph[N, E]) Frontier

func (graph *Graph[N, E]) Frontier(kind E, done func(n N) bool) []N

Frontier is the ready set of a directed kind: every node that is not done and whose kind predecessors are all done, in insertion order. It is a query over the store, not a copy of it.

func (*Graph[N, E]) Has

func (graph *Graph[N, E]) Has(n N) bool

Has reports whether n is a node.

func (*Graph[N, E]) HasEdge

func (graph *Graph[N, E]) HasEdge(kind E, from N, to N) bool

HasEdge reports an edge of kind from -> to; for an undirected kind, that the two are joined.

func (*Graph[N, E]) In

func (graph *Graph[N, E]) In(kind E, n N) []N

In returns the nodes with a kind edge to n: its predecessors, or for an undirected kind its neighbors, in edge order.

func (*Graph[N, E]) Len

func (graph *Graph[N, E]) Len() int

Len is the number of nodes.

func (*Graph[N, E]) Nodes

func (graph *Graph[N, E]) Nodes() []N

Nodes returns every node in insertion order.

func (*Graph[N, E]) Out

func (graph *Graph[N, E]) Out(kind E, n N) []N

Out returns the nodes n has a kind edge to: its successors, or for an undirected kind its neighbors, in edge order.

func (*Graph[N, E]) Reaches

func (graph *Graph[N, E]) Reaches(kind E, from N, to N) bool

Reaches reports whether to is reachable from from along one or more kind edges: for an acyclic kind, whether an edge to -> from would close a cycle.

func (*Graph[N, E]) TopologicalOrder

func (graph *Graph[N, E]) TopologicalOrder(kind E) []N

TopologicalOrder returns every node after all of its kind predecessors, taking nodes in insertion order whenever more than one is available (Kahn's algorithm). It is defined for an acyclic kind.

func (*Graph[N, E]) UpwardRank

func (graph *Graph[N, E]) UpwardRank(kind E, weight func(n N) float64, skip func(n N) bool) map[N]float64

UpwardRank is the longest path from each node to the end of a directed acyclic kind, counting the node's own weight and every successor along the heaviest chain: the upward rank of list scheduling (Topcuoglu, Hariri and Wu, HEFT, 2002) with no communication cost. Nodes excluded by skip contribute nothing and are not ranked; a weight that is not positive counts as zero.

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue is an unbounded FIFO of T shared by any number of producers and consumers. Push never blocks; Pop blocks until an item arrives, the context ends or the queue is closed and drained. Close stops new pushes and lets consumers drain what was queued: the items already accepted are delivered before Pop reports ErrQueueClosed.

The lock guards one datum, the backlog and its closed flag, and every section under it is a single step; nobody waits under it. Waiting happens outside the lock on the wake channel, which is why the lock is a leaf (house rule CS-5) rather than a protocol.

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue returns an empty, open queue.

func (*Queue[T]) Close

func (queue *Queue[T]) Close()

Close refuses further pushes and wakes waiting consumers; queued items stay poppable until drained. Close is idempotent.

func (*Queue[T]) Len

func (queue *Queue[T]) Len() int

Len is the number of queued items not yet popped.

func (*Queue[T]) Pop

func (queue *Queue[T]) Pop(ctx context.Context) (T, error)

Pop removes and returns the oldest item. It waits for one while the queue is open and empty; it returns ctx's cause when ctx ends first, and ErrQueueClosed once the queue is closed and empty.

func (*Queue[T]) Push

func (queue *Queue[T]) Push(item T) error

Push appends item and wakes one waiting consumer. It reports ErrQueueClosed after Close.

type Relation

type Relation struct {
	Directed bool
	Acyclic  bool
}

Relation describes one edge kind of a Graph: whether its edges have a direction, and, when they do, whether the kind must stay acyclic.

Jump to

Keyboard shortcuts

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