relay

package
v0.0.0-...-9268282 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 16 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 relay is the WebSocket transport boundary for the relay.

Router owns connection lifecycle, authentication messages, subscriptions, request and count queries, event publication and NIP-77 negotiation. A Backend supplies durable publication and query behavior; optional interfaces add reader privacy, query hints, policy invalidation and sync snapshots. Router serializes publication with subscription registration and snapshots, so a live event cannot pass a historical query boundary. Close stops every connection and waits for its reader and writer goroutines to finish.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	Publish(ctx context.Context, event event.Event, session Session) (reason string, err error)
	Query(ctx context.Context, filters []event.Filter, session Session) ([]event.Event, error)
	Count(ctx context.Context, filters []event.Filter, session Session) (any, error)
}

Backend publishes while the Router's write gate is held. Publish performs the backend's admission, persistence, duplicate handling or control action before returning. A nil error allows Router to fan out the event unless the returned reason identifies a duplicate. Persistence and delivery planning remain backend responsibilities for event kinds that require them.

type Config

type Config struct {
	// RelayURL is the default relay identity included in sessions.
	RelayURL string
	// RequestRelayURL optionally derives the relay identity from each request.
	RequestRelayURL func(*http.Request) string
	// OnAuthenticate checks the authenticated session candidate and may persist
	// authentication bookkeeping. The Session argument is a value snapshot;
	// Router records the newly authenticated public key after this callback.
	OnAuthenticate func(context.Context, Session) error
	// MaxMessageBytes limits websocket text messages. Zero leaves the limit
	// unlimited; a negative value selects the 512 KiB default.
	MaxMessageBytes int64
	// MaxPendingBytes bounds queued output per connection.
	MaxPendingBytes int
	// ReadLimit, WriteLimit and PingInterval control websocket liveness.
	ReadLimit    time.Duration
	WriteLimit   time.Duration
	PingInterval time.Duration
	// OriginPatterns is passed to the websocket origin check.
	OriginPatterns []string
	// OnConnection and OnSubscription receive +1 and -1 lifecycle deltas.
	OnConnection   func(int)
	OnSubscription func(int)
	// ChangesAccess identifies host-specific events that invalidate existing
	// subscriptions. A nil value leaves subscriptions intact.
	ChangesAccess func(event.Event) bool
}

type FilterReader

type FilterReader interface {
	CanReadFilter(event event.Event, session Session, filter *event.Filter) bool
}

FilterReader applies reader privacy with the subscription filter available to the policy decision.

type PolicyChanges

type PolicyChanges interface {
	CloseSubscriptions(session Session)
}

PolicyChanges can invalidate subscriptions in storage after an access policy revision. Router removes client state before invoking the callback.

type QueryHints

type QueryHints interface {
	QueryHints(ctx context.Context, filters []event.Filter, session Session) ([]event.Event, []string, error)
}

QueryHints can return query results together with completion hints. Router places these values in the EOSE response; the relay backend uses values such as "finish", "more" and "auth".

type Reader

type Reader interface {
	CanRead(event event.Event, session Session) bool
}

Reader-side privacy can be stricter than the query filter. It is checked for both historical and live delivery, including encrypted signer traffic.

type Router

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

func New

func New(backend Backend, cfg Config) *Router

func (*Router) ApplyACLChange

func (r *Router) ApplyACLChange(ctx context.Context, change func(context.Context) (any, error), reason string) (any, error)

ApplyACLChange serializes an access-control mutation with REQ snapshots, NEG sessions, and live publication. A successful mutation invalidates both subscription families before another transport operation can begin.

func (*Router) BroadcastGenerated

func (r *Router) BroadcastGenerated(e event.Event)

BroadcastGenerated delivers an event already durably accepted by an out-of-band producer.

func (*Router) Close

func (r *Router) Close(ctx context.Context) error

func (*Router) CloseSubscriptions

func (r *Router) CloseSubscriptions(reason string)

CloseSubscriptions invalidates all live subscriptions after a policy revision. Existing sockets remain usable for AUTH and a subsequent REQ.

func (*Router) CommitGenerated

func (r *Router) CommitGenerated(ctx context.Context, commit func(context.Context) (event.Event, error)) (event.Event, error)

CommitGenerated runs an imported or server-generated event commit under the same historical/live fence as client EVENT publication. The callback must durably commit the event before returning; a returned error suppresses fanout. This prevents a REQ snapshot from racing an out-of-band broadcast.

func (*Router) HandleHTTP

func (r *Router) HandleHTTP(w http.ResponseWriter, req *http.Request)

func (*Router) Listen

func (r *Router) Listen(fn func(event.Event)) func()

Listen registers an in-process observer of live fan-out. It sees every event delivered to websocket subscribers, before the reader's own access check. The callback runs under the publish fence and must not block; the returned function removes it.

func (*Router) Publish

func (r *Router) Publish(ctx context.Context, e event.Event, s Session) (string, error)

Publish is the shared ingress path for websocket EVENT and HTTP-generated events. It applies the same historical/live fence and fan-out rules.

type Session

type Session = auth.Session

type SyncBackend

type SyncBackend interface {
	Sync(ctx context.Context, filter event.Filter, session Session) ([]syncprotocol.Item, error)
}

SyncBackend supplies the authorized event snapshot for a NIP-77 session. The Router holds its read gate while calling Sync, so a concurrent Publish cannot create an opening between snapshot selection and NEG-OPEN.

Jump to

Keyboard shortcuts

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