collab

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 8 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 collab tracks who is connected to each terminal, what each participant may do, and announces presence and permission changes.

Index

Constants

View Source
const (
	EventPresenceSnapshot  = "presence_snapshot"
	EventParticipantJoined = "participant_joined"
	EventParticipantLeft   = "participant_left"
	EventPermissionChanged = "permission_changed"
)

Server event types.

View Source
const (
	ReasonDisconnected = "disconnected"
	ReasonSlowConsumer = "slow_consumer"
	ReasonExpired      = "expired"
)

Participant-left reasons besides revocation reasons.

Variables

View Source
var (
	ErrClosed       = errors.New("collab: hub closed")
	ErrFull         = errors.New("collab: participant limit reached")
	ErrSlowConsumer = errors.New("collab: slow consumer")
	ErrRevoked      = errors.New("collab: access revoked")
	ErrInvalidRole  = errors.New("collab: invalid role")
)

Functions

This section is empty.

Types

type Action

type Action string

Action is something a participant asks the terminal to do.

const (
	ActionInput  Action = "input"
	ActionResize Action = "resize"
	ActionPing   Action = "ping"
)

type Config

type Config struct {
	Now       func() time.Time
	AfterFunc func(time.Duration, func()) Timer
	// QueueLimit bounds the events waiting for one participant; a
	// participant that falls further behind is dropped.
	QueueLimit int
	// MaxParticipants bounds the participants of one terminal.
	MaxParticipants int
	// Observer, if set, is told about every join, leave, and permission
	// change.
	Observer Observer
}

Config tunes a Hub. Zero fields receive defaults.

type Event

type Event struct {
	Type         string            `json:"type"`
	Version      uint64            `json:"version"`
	Self         string            `json:"self,omitempty"`
	Participants []ParticipantInfo `json:"participants,omitempty"`
	Participant  *ParticipantInfo  `json:"participant,omitempty"`
	Permissions  *Permissions      `json:"permissions,omitempty"`
	Reason       string            `json:"reason,omitempty"`
}

Event is a presence or permission message. Version increases with every change to a terminal's participants, so all participants observe changes in the same order.

type Hub

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

Hub holds the live participants of every terminal.

func NewHub

func NewHub(cfg Config) *Hub

NewHub returns an empty Hub.

func (*Hub) Close

func (h *Hub) Close()

Close ends every participant with ErrClosed and rejects later joins. It is idempotent.

func (*Hub) EndTerminal

func (h *Hub) EndTerminal(terminalID string)

EndTerminal forgets a terminal whose session ended. Its participants are detached silently; their connections end with the session.

func (*Hub) GrantsRevoked

func (h *Hub) GrantsRevoked(terminalID string, grantIDs []string, reason string)

GrantsRevoked removes the terminal's participants admitted by grantIDs.

func (*Hub) Join

func (h *Hub) Join(req JoinRequest) (*Participant, error)

Join adds a participant at once: Reserve followed by Activate.

func (*Hub) ParticipantCount

func (h *Hub) ParticipantCount(terminalID string) int

ParticipantCount reports how many announced participants terminalID has now; reservations are not counted.

func (*Hub) Reserve

func (h *Hub) Reserve(req JoinRequest) (*Participant, error)

Reserve holds a place for a participant without announcing it. The reservation counts toward the participant limit and can be revoked or expire, but nobody, observers included, learns about it until Activate. Leave abandons it without a trace.

func (*Hub) SessionRevoked

func (h *Hub) SessionRevoked(sessionID int64, reason string)

SessionRevoked removes every participant admitted by the access session.

func (*Hub) Stats

func (h *Hub) Stats() Stats

Stats reports the hub's current size.

type JoinRequest

type JoinRequest struct {
	TerminalID string
	Role       Role
	GrantID    string
	SessionID  int64
	ExpiresAt  time.Time
}

JoinRequest identifies a connecting participant. GrantID and SessionID tie a guest to its capability so revocations can find it; a zero ExpiresAt never expires.

type Observer

type Observer interface {
	PresenceChanged(terminalID string, e Event)
}

Observer receives presence changes in the order participants see them. It is called with the hub's lock held, so it must return without blocking and must not call back into the hub.

type Participant

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

Participant is one connection's presence in a terminal.

func (*Participant) Activate

func (p *Participant) Activate() error

Activate announces a reserved participant: it first receives a presence snapshot, then everyone else is told it joined. It returns the *RevokedError of a reservation revoked or expired meanwhile, and ErrClosed once the hub is closed. A reservation whose terminal ended stays detached and Activate does nothing; its connection ends with the session.

func (*Participant) Allowed

func (p *Participant) Allowed(action Action) bool

Allowed reports whether the participant may take action now. Revocation and expiry take effect here immediately, independently of event delivery.

func (*Participant) ID

func (p *Participant) ID() string

ID is the participant's public, non-secret identifier.

func (*Participant) Leave

func (p *Participant) Leave()

Leave removes the participant and tells the others it disconnected. It is idempotent.

func (*Participant) Next

func (p *Participant) Next(ctx context.Context) (Event, error)

Next returns the next queued event. Once the participant is removed it returns the remaining events and then the reason: *RevokedError, ErrSlowConsumer, or ErrClosed. A detached participant just waits for ctx.

func (*Participant) Role

func (p *Participant) Role() Role

Role is the participant's role.

type ParticipantInfo

type ParticipantInfo struct {
	ID   string `json:"id"`
	Role Role   `json:"role"`
}

ParticipantInfo is the public identity of a participant. It never carries credentials or network addresses.

type Permissions

type Permissions struct {
	Input  bool `json:"input"`
	Resize bool `json:"resize"`
}

Permissions are the actions a participant may currently take.

type RevokedError

type RevokedError struct{ Reason string }

RevokedError reports why a participant lost access. It matches ErrRevoked.

func (*RevokedError) Error

func (e *RevokedError) Error() string

func (*RevokedError) Is

func (e *RevokedError) Is(target error) bool

type Role

type Role string

Role is a participant's authority over a terminal.

const (
	RoleOwner  Role = "owner"
	RoleEditor Role = "editor"
	RoleViewer Role = "viewer"
)

func (Role) Allows

func (r Role) Allows(action Action) bool

Allows reports whether the role permits action.

type Stats

type Stats struct {
	Rooms, Participants int
}

Stats counts live terminals and participants.

type Timer

type Timer interface {
	Stop() bool
}

Timer is a stoppable pending callback.

Jump to

Keyboard shortcuts

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