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 ¶
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.
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 ¶
Types ¶
type Conn ¶ added in v0.1.0
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
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 ¶
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
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.