sse

package
v0.30.2 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 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 holds server-sent-events helpers for the response path. Today that is exactly one thing: KeepAlive, a response-writer wrapper that emits a caller-supplied no-op frame while the upstream is silent, so byte-watchdog clients — which abort a stream after N seconds without bytes — stay connected through a long prompt-processing or thinking pause.

Deliberately out of scope: parsing, framing or rewriting SSE. KeepAlive never inspects the bytes it forwards beyond checking that the last write ended on a frame boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeepAlive

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

KeepAlive wraps a streaming ResponseWriter and writes frame whenever nothing else has been written for the configured interval. It is an io.Writer + http.Flusher, so the copy loops can write through it unchanged.

Safety of the splice rests on two gates, both required: a frame is only written when no Write landed in the last interval (so it can never interleave with an in-progress write), and only when the last write ended on an SSE frame boundary (so a copy loop that hands over a half frame — streamCopy reads in 32 KiB blocks, not frames — is never cut in two).

It runs on a timer callback rather than a goroutine per stream, so an idle open stream holds no goroutine stack for the GC to scan.

func NewKeepAlive

func NewKeepAlive(w http.ResponseWriter, frame []byte, every time.Duration) *KeepAlive

NewKeepAlive returns a KeepAlive writing frame to w every interval of silence. every <= 0 disables it entirely: Write is a plain pass-through and Start/Stop are no-ops.

func (*KeepAlive) Flush

func (k *KeepAlive) Flush()

Flush satisfies http.Flusher so the wrapped writer keeps flushing per chunk. Writes already flush themselves; this exists for copy loops that flush explicitly.

func (*KeepAlive) Start

func (k *KeepAlive) Start(ctx context.Context)

Start arms the keepalive timer. It ends on ctx cancellation (client gone) or Stop. Call at most once, before the copy loop.

func (*KeepAlive) Stop

func (k *KeepAlive) Stop()

Stop ends the keepalive so no frame can land after the real stream finished: a tick either completed before Stop took the lock or sees stopped. Idempotent; safe when Start was never called.

func (*KeepAlive) Write

func (k *KeepAlive) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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