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 ¶
- type Backend
- type Config
- type FilterReader
- type PolicyChanges
- type QueryHints
- type Reader
- type Router
- func (r *Router) ApplyACLChange(ctx context.Context, change func(context.Context) (any, error), reason string) (any, error)
- func (r *Router) BroadcastGenerated(e event.Event)
- func (r *Router) Close(ctx context.Context) error
- func (r *Router) CloseSubscriptions(reason string)
- func (r *Router) CommitGenerated(ctx context.Context, commit func(context.Context) (event.Event, error)) (event.Event, error)
- func (r *Router) HandleHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Listen(fn func(event.Event)) func()
- func (r *Router) Publish(ctx context.Context, e event.Event, s Session) (string, error)
- type Session
- type SyncBackend
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 ¶
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 (*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 ¶
BroadcastGenerated delivers an event already durably accepted by an out-of-band producer.
func (*Router) CloseSubscriptions ¶
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)
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.