sse

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 21 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 sse is the client of the console's event stream, GET {base}/api/events: one push-only text/event-stream response that carries the events of a set of streams for one team. It sends nothing after the request. Changing the stream set or the team opens a new stream, and the new one replaces the old only once its welcome has arrived, so a planned change loses no event; an event both carry is delivered once, by its id.

The credential is the process's auth.Authorizer, sent as the Authorization header of an ordinary request through the shared HTTP client (internal/httpclient), so it never rides a URL.

Every stream a client opens carries the same Console-Session header, a random value made once per client. The server ends a client's earlier streams a few seconds after a later one's welcome and leaves them out of its per-user cap, since it learns that a closed stream is gone only when a write to it fails.

Index

Constants

View Source
const (
	// ReconnectingEvent is sent before each attempt to reopen a stream that
	// ended, with data {"attempt": n, "next_attempt_at": RFC 3339}.
	ReconnectingEvent = "reconnecting"
	// ReconnectedEvent is sent once a stream that ended is open again,
	// with the new stream's welcome as its data.
	ReconnectedEvent = "rejoined"
	// TokenRefreshEvent is sent when the server ended the stream at the
	// OAuth access token's expiry and the client is refreshing the token
	// before it reopens the stream.
	TokenRefreshEvent = "token_refreshing"
	// AuthFailedEvent is sent when the credential can no longer open a
	// stream, with data {"reason": ...}: "session_expired" (the OAuth
	// session is dead), "unauthorized" (the server refused the credential)
	// or "revoked" (the server ended the stream because the credential was
	// revoked). The client stops reopening the stream.
	AuthFailedEvent = "auth_failed"
	// RefusedEvent is sent when the server refused to reopen a stream for
	// a reason that waiting will not change, such as the loss of the team
	// the stream was opened for. Its Err is the refusal, a
	// *jsonapi.APIError. The client stops reopening the stream.
	RefusedEvent = "refused"
)

Synthetic event names the client puts on Client.Events beside the server's stream events. The server sends none of them.

View Source
const StreamEvent = "stream"

StreamEvent is the server's event that carries one console push, with data {"stream", "kind", "at", "data"}.

Variables

View Source
var ErrNotEventStream = errors.New("the server did not open an event stream")

ErrNotEventStream is the error of a 200 answer that is not a text/event-stream, which a proxy or a server without the route gives.

Functions

This section is empty.

Types

type Client

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

Client holds one live event stream at a time and reopens it when it ends. It is safe for concurrent use.

func New

func New(opts Options) (*Client, error)

New returns a client; nothing is sent until Client.Connect.

func (*Client) ClockOffset

func (c *Client) ClockOffset() (time.Duration, bool)

ClockOffset returns the server's clock minus the local clock, read from the Date header of the last stream opened, and false before one opened. The header has whole seconds, so the offset is good to about half a second.

func (*Client) Close

func (c *Client) Close()

Close ends the stream and every goroutine of the client.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, sub Subscription) ([]byte, error)

Connect opens the first stream for sub and returns its welcome. ctx bounds the wait for the welcome, not the stream. A refusal is a *jsonapi.APIError.

func (*Client) Done

func (c *Client) Done() <-chan struct{}

Done is closed once the client is closed.

func (*Client) Events

func (c *Client) Events() <-chan Event

Events is the channel of server stream events and synthetic events. It is never closed; Client.Done is closed by Client.Close.

func (*Client) Subscription

func (c *Client) Subscription() Subscription

Subscription returns what the current stream was opened for.

func (*Client) Update

func (c *Client) Update(ctx context.Context, fn func(Subscription) Subscription) ([]byte, error)

Update opens a stream for fn applied to the current subscription and, once its welcome has arrived, makes it the current stream and closes the one it replaces. On an error the current stream is left as it was. It returns the new stream's welcome.

type Event

type Event struct {
	Name string
	ID   string
	Data []byte
	Err  error
}

Event is one event on Client.Events: a server stream event (Name StreamEvent, its id and its data, a JSON text) or one of the synthetic events, whose data, when they have any, is JSON too.

type Options

type Options struct {
	// URL is the event stream's URL, {base}/api/events.
	URL string
	// Authorizer supplies the credential of every request.
	Authorizer auth.Authorizer
	Logger     *slog.Logger

	// BufferSize is the capacity of the Events channel (256). A server
	// event that finds it full is dropped and logged.
	BufferSize int
	// IdleTimeout ends a stream that has sent no byte for this long (45s);
	// the server sends a comment every 15s.
	IdleTimeout time.Duration
	// OpenTimeout bounds one attempt to reopen a stream, up to its welcome
	// (15s).
	OpenTimeout time.Duration
	// Backoff is the wait before each reopen attempt, the last repeating
	// (1, 2, 5, 10, 30s).
	Backoff []time.Duration
	// RefreshLead and RefreshPoll set when an OAuth access token is
	// refreshed and the stream reopened with it: within RefreshLead of its
	// expiry (60s), checked every RefreshPoll (15s).
	RefreshLead time.Duration
	RefreshPoll time.Duration
	// SeenIDs is how many event ids are remembered to drop repeats (1024).
	SeenIDs int
}

Options configures a Client. URL and Authorizer are required; every other field has a default and is set only by tests.

type Subscription

type Subscription struct {
	Streams []string
	Tenant  string
}

Subscription is what a stream is opened for: the stream ids, sent as the streams query parameter, and the team, sent as the tenant header the JSON:API reads (empty for the server's default team).

func (Subscription) With

func (s Subscription) With(id string) Subscription

With returns s with id added, if it is not there already.

func (Subscription) Without

func (s Subscription) Without(id string) Subscription

Without returns s with id removed.

Jump to

Keyboard shortcuts

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