Documentation
¶
Overview ¶
Package streamhub provides resumable LLM streaming for Go, backed by Redis.
It is meant for the case where the code producing a stream and the code consuming it don't share the same lifetime — they might not even be on the same instance. Chunks are stored in Redis Streams so reconnecting subscribers can replay what they missed, and cancel signals are delivered via Redis Pub/Sub so a generation can be stopped from anywhere.
Each producer gets a generation ID as a fencing token, and only one producer can own a session at a time.
Index ¶
- type Hub
- type LiveStream
- func (s *LiveStream) Cancel(ctx context.Context)
- func (s *LiveStream) Close()
- func (s *LiveStream) Done() bool
- func (s *LiveStream) Metadata(target any) bool
- func (s *LiveStream) Publish(chunk string)
- func (s *LiveStream) SetMetadata(v any)
- func (s *LiveStream) Subscribe(opts ...SubscribeOption) (<-chan string, func())
- type SubscribeOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub manages Redis-backed LiveStream values.
func (*Hub) Get ¶
func (h *Hub) Get(sessionID string) *LiveStream
Get returns a stream proxy for sessionID, or nil if it does not exist.
func (*Hub) Register ¶
func (h *Hub) Register(sessionID string, cancelRuntime func()) (*LiveStream, bool, error)
Register tries to create a new LiveStream for sessionID. If created is false, the caller must not start another producer.
type LiveStream ¶
type LiveStream struct {
// contains filtered or unexported fields
}
LiveStream is a Redis-backed stream for one session. generation is only set on the producer side.
func (*LiveStream) Cancel ¶
func (s *LiveStream) Cancel(ctx context.Context)
Cancel broadcasts a cancel signal and waits for the stream to finish (or the context to expire). Pass context.Background() for fire-and-forget.
func (*LiveStream) Close ¶
func (s *LiveStream) Close()
Close marks the stream as done and stops local subscribers. If this generation is stale, Close only shuts down local state.
func (*LiveStream) Done ¶
func (s *LiveStream) Done() bool
Done reports whether the stream is marked done in Redis.
func (*LiveStream) Metadata ¶
func (s *LiveStream) Metadata(target any) bool
Metadata loads stored metadata into target.
func (*LiveStream) Publish ¶
func (s *LiveStream) Publish(chunk string)
Publish appends a chunk to Redis if the generation still matches and the stream is still active. Fails silently on stale generation, closed stream, or Redis errors — producers should treat Publish as best-effort.
func (*LiveStream) SetMetadata ¶
func (s *LiveStream) SetMetadata(v any)
SetMetadata stores stream metadata as JSON. It only writes when the current generation still owns the stream. The ownership check and the write are atomic, so a late writer from a stale generation can't overwrite a new owner's metadata.
func (*LiveStream) Subscribe ¶
func (s *LiveStream) Subscribe(opts ...SubscribeOption) (<-chan string, func())
Subscribe replays existing chunks, then follows new ones. The returned unsubscribe should be called when the caller is done.
type SubscribeOption ¶
type SubscribeOption func(*subscribeConfig)
SubscribeOption configures Subscribe behaviour.
func WithBatchReplay ¶
func WithBatchReplay() SubscribeOption
WithBatchReplay makes Subscribe concatenate all existing chunks into a single string instead of sending them one by one. Useful for reconnecting clients that don't need per-chunk granularity on replay.
func WithBuffer ¶
func WithBuffer(n int) SubscribeOption
WithBuffer sets the extra channel buffer size for live chunks. Defaults to 256 if not specified or ≤ 0.