matchmaking

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package matchmaking provides group formation: members enter a queue tagged with attributes; a Matcher periodically searches for satisfying subsets of a configured group size; matches are emitted to subscribers.

Supports a solo-fallback policy: if no full group forms within a TTL, the member is emitted in a solo Match so the caller can offer a single- player path.

Domain-agnostic; common applications:

  • Game matchmaking (mission + difficulty + region).
  • Collaborative editing — assemble a doc-edit session of N peers.
  • Group call / video conferencing — find available callees.
  • Support routing — pair a ticket with N agents with the right skills.
  • Carpooling — match riders by route + time + capacity.

The package ships an in-memory Pool; Redis-backed and Open Match adapters are pluggable via the Backend interface.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyEnqueued = errors.New("matchmaking: member already enqueued")
	ErrNotEnqueued     = errors.New("matchmaking: member not enqueued")
)

Errors surfaced by Pool.

Functions

This section is empty.

Types

type Attr

type Attr map[string]any

Attr is the bag of attributes the matcher reads to assess fit.

type FirstN

type FirstN struct {
	N int
}

FirstN is a Matcher that pairs the first N members in queue order regardless of attributes. Useful for fastest-match modes.

func (FirstN) Match

func (m FirstN) Match(candidates []Member) []Member

type Match

type Match struct {
	Members []Member
	Solo    bool
}

Match is the result of a successful pairing. Solo is true when the match was emitted via the solo-fallback policy rather than full group formation.

type Matcher

type Matcher interface {
	Match(candidates []Member) []Member
}

Matcher inspects the current candidate pool and returns a satisfying subset, or nil if no match is currently possible. Implementations should be cheap — the Pool calls Match per tick.

type MatcherFunc

type MatcherFunc func(candidates []Member) []Member

MatcherFunc adapts a function to the Matcher interface.

func (MatcherFunc) Match

func (f MatcherFunc) Match(c []Member) []Member

type Member

type Member struct {
	ID       MemberID
	Attrs    Attr
	Enqueued time.Time
}

Member is one queued participant.

type MemberID

type MemberID string

MemberID is opaque caller-supplied identity for a queued participant.

type Pool

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

Pool holds the queue and emits matches via Subscribe.

func NewPool

func NewPool(cfg PoolConfig) *Pool

NewPool constructs a Pool. Panics if config is invalid.

func (*Pool) Dequeue

func (p *Pool) Dequeue(_ context.Context, id MemberID) error

Dequeue removes a member from the pool.

func (*Pool) Enqueue

func (p *Pool) Enqueue(_ context.Context, m Member) error

Enqueue adds a member to the pool.

func (*Pool) Run

func (p *Pool) Run(ctx context.Context) error

Run drives the matching loop until ctx is cancelled. Returns ctx.Err() on exit.

func (*Pool) Size

func (p *Pool) Size() int

Size returns the current queue depth.

func (*Pool) Subscribe

func (p *Pool) Subscribe() <-chan Match

Subscribe returns a channel that receives Matches. Caller drains the channel; missed sends (buffer full) are dropped silently. Buffer size is 16; tune if you expect bursts.

type PoolConfig

type PoolConfig struct {
	// Name is an optional label used in logs / metrics.
	Name string

	// GroupSize is the exact party size produced by a full match. The
	// Matcher is expected to return slices of this length.
	GroupSize int

	// Matcher decides who pairs with whom. Required.
	Matcher Matcher

	// SoloFallback is the duration after which a queued member is emitted
	// as a Match with Solo=true. Zero disables solo fallback.
	SoloFallback time.Duration

	// TickInterval controls how often Run scans for matches. Defaults to
	// 100ms if unset.
	TickInterval time.Duration

	// Clock supplies the current time; defaults to time.Now if nil.
	Clock func() time.Time
}

PoolConfig configures a Pool.

type SameAttribute

type SameAttribute struct {
	N       int
	AttrKey string
}

SameAttribute is a Matcher that pairs the first N candidates sharing the same value for AttrKey (e.g. region, mission, difficulty).

func (SameAttribute) Match

func (m SameAttribute) Match(candidates []Member) []Member

Jump to

Keyboard shortcuts

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