wsutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: BSD-2-Clause Imports: 7 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 wsutil holds small shared helpers for the provider WebSocket clients. The streaming STT and TTS providers each dial a vendor WebSocket the same way; this package factors out that dial boilerplate so a provider only writes the request and reads the vendor's message frames.

Index

Constants

View Source
const DefaultCloseTimeout = 2 * time.Second

DefaultCloseTimeout bounds how long a close waits for the peer to answer. Disconnecting happens while a service handles the EndFrame, before the frame carries on downstream, so a close the peer never acknowledges holds up the end of the pipeline for that long. The library waits 5s, which is long enough to be felt once several WebSocket services tear down one after another.

View Source
const DefaultReadLimit = 16 << 20

DefaultReadLimit bounds a single inbound WebSocket message. It guards against a server that streams without end, not against large messages: a TTS provider generating a long sentence in one go sends base64 audio well past a megabyte, and a limit tight enough to cut that off fails the synthesis mid-reply.

Variables

This section is empty.

Functions

func Permanent added in v0.1.0

func Permanent(err error) bool

Permanent reports whether err is a refusal the server will repeat, so there is nothing to gain by dialing again.

Types

type Conn added in v0.1.0

type Conn struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

Conn is a WebSocket connection whose closing handshake is bounded, so a peer that never answers cannot hold up a teardown. Everything else is the underlying connection's.

func Adopt added in v0.1.0

func Adopt(conn *websocket.Conn, readLimit int64) *Conn

Adopt wraps a connection that was accepted rather than dialed, so a server side gets the same bounded close a dialed one has. It applies readLimit when that is positive.

The caller owns the connection either way: Accept hands it over, and this only takes over how it is closed.

func Dial

func Dial(ctx context.Context, url string, header http.Header, readLimit int64) (*Conn, error)

Dial opens a WebSocket to url with the given headers, closes the handshake response body, and applies readLimit when it is positive. The caller owns the returned connection and must Close it. A handshake the server refused comes back as a *HandshakeError carrying the status.

func (*Conn) Close added in v0.1.0

func (c *Conn) Close(code websocket.StatusCode, reason string) error

Close closes the connection, giving up on the closing handshake once the timeout passes rather than waiting the library's own deadline out. The peer not answering is logged, since a teardown that quietly cost the full timeout would otherwise go unnoticed; the library drops the connection behind us.

func (*Conn) SetCloseTimeout added in v0.1.0

func (c *Conn) SetCloseTimeout(d time.Duration)

SetCloseTimeout sets how long Close waits for the peer to answer. Raise it for a peer that needs longer to close gracefully.

type HandshakeError added in v0.1.0

type HandshakeError struct {
	// StatusCode is the HTTP status the server refused the handshake with.
	StatusCode int
	// Err is the underlying dial failure.
	Err error
}

HandshakeError is returned when the server refused the WebSocket handshake. It carries the status it refused with, so a caller can tell a request the server will go on refusing (a rejected key, a model the account cannot use) from a failure that may not repeat.

func (*HandshakeError) Error added in v0.1.0

func (e *HandshakeError) Error() string

Error implements error.

func (*HandshakeError) HTTPStatusCode added in v0.1.0

func (e *HandshakeError) HTTPStatusCode() int

HTTPStatusCode implements the interface the shared error classification looks for, so a refused handshake is classified by the status the server refused with: rejected credentials from a server that is merely unwell.

func (*HandshakeError) Permanent added in v0.1.0

func (e *HandshakeError) Permanent() bool

Permanent reports whether the server refused the request itself, rather than failing in a way that might not repeat. Retrying a refusal only spends the time before the caller learns of it.

func (*HandshakeError) Unwrap added in v0.1.0

func (e *HandshakeError) Unwrap() error

Unwrap returns the underlying dial failure.

Jump to

Keyboard shortcuts

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