Help us shape the future of Go! Take the 2026 Go Developer Survey

rpcstream

package
v0.114.7 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package rpcstream holds helpers shared by the engine's streaming RPC handlers.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("stream is closed")

ErrClosed is returned by Send once the handler that owns the stream has returned.

Functions

func WithAbort

func WithAbort(ctx context.Context, abort func()) context.Context

WithAbort returns a context carrying a function that interrupts a write blocked on the stream's transport, for example by expiring its write deadline. The server sets it for every request; NewSender picks it up.

Types

type ChannelStream added in v0.114.1

type ChannelStream struct {
	// Requests takes the caller's messages on a bidi stream; closing it half-closes the
	// stream, the way a client's CloseSend does. It is nil on a server stream, whose one
	// request the handler was served with.
	Requests chan<- proto.Message

	// Responses carries the handler's messages and closes once the handler returned.
	Responses <-chan proto.Message

	// Err is the handler's result: nil when it finished the stream, the error it refused or
	// failed with otherwise. It is written before Responses closes and is only meaningful
	// once it has.
	Err error
}

ChannelStream is a streaming handler served over channels in place of a transport, to a caller in the same process. The handler runs on its own goroutine with the context it was served with, which must carry what the transport's middleware would put there (the tenant, for the engine's handlers). It ends when that context is cancelled, when Requests is closed, or when it finishes on its own; Responses closes once it returned and Err then reports how it ended.

func ServeBidi added in v0.114.1

func ServeBidi[Req any, Resp any](ctx context.Context, first proto.Message, handler func(context.Context, func() (*Req, error), *Sender[Resp]) error) (*ChannelStream, error)

ServeBidi runs handler over a request channel and a response channel. first, when set, is delivered as the stream's first request before anything the caller sends; a first message that is not a Req is refused before the handler runs.

func ServeServer added in v0.114.1

func ServeServer[Resp any](ctx context.Context, handler func(context.Context, *Sender[Resp]) error) *ChannelStream

ServeServer runs handler over a response channel only.

type Sender

type Sender[T any] struct {
	// contains filtered or unexported fields
}

Sender serializes sends on a server stream and rejects sends once the owning handler has returned.

Handlers hand their stream to other goroutines (the dispatcher sends assigned actions from message queue consumers, subscriptions send from fan-out goroutines). The HTTP/2 server panics on a write that arrives after the handler has returned, so every handler that shares its stream must wrap it in a Sender and defer Close: Close turns every later Send into ErrClosed and does not return while a Send is still writing. A Send to a peer that has stopped reading is interrupted after a short grace period, so a handler can always return.

func NewSender

func NewSender[T any](ctx context.Context, stream Stream[T]) *Sender[T]

NewSender wraps stream. ctx is the handler's context.

func (*Sender[T]) Close

func (s *Sender[T]) Close()

Close marks the stream as finished. It must be called before the handler returns, normally with defer.

func (*Sender[T]) Send

func (s *Sender[T]) Send(msg *T) error

type Stream

type Stream[T any] interface {
	Send(*T) error
}

Stream is the sending half of a server stream, satisfied by *connect.ServerStream[T] and *connect.BidiStream[Req, T].

Jump to

Keyboard shortcuts

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