Streaming handlers: imperative writer API for bidi and server-streaming
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- grpc, rust
- Domain
- api, backend-api-design
Research direction
Start by tracing the current response-stream dispatcher and the generated streaming handler signatures in connectrpc-codegen, then review the existing examples and guide. Resolve the open questions about API coexistence, handler scope, channel bounds, and OwnedView lifetimes before implementing the writer and dispatcher plumbing. Done includes updated codegen, examples, and guide coverage.
Written by the indexing model from the issue text.
Description
Server-streaming and bidirectional handlers must construct an impl Stream up front and return it. For finite, precomputable streams that's fine — Response::stream_ok(stream::iter(items)) reads well. But for handlers that interleave reads and writes, or that produce items as a side effect of looping over the request stream, the only options today are futures::stream::unfold with an explicit state tuple, or hand-rolling a tokio::sync::mpsc channel + ReceiverStream and spawning a task to drive it.
The eliza bidi handler is the canonical example of the friction:
async fn converse(
&self,
_ctx: RequestContext,
requests: ServiceStream<OwnedView<ConverseRequestView<'static>>>,
) -> ServiceResult<ServiceStream<ConverseResponse>> {
let response_stream = futures::stream::unfold(
(requests, false),
|(mut requests, session_ended)| async move {
if session_ended {
return None;
}
match requests.next().await {
None => None,
Some(Err(e)) => Some((Err(e), (requests, true))),
Some(Ok(req)) => {
let (reply, end_session) = eliza::reply(req.sentence);
Some((
Ok(ConverseResponse { sentence: reply, ..Default::default() }),
(requests, end_session),
))
}
}
},
);
Response::stream_ok(response_stream)
}
unfold is one of the harder futures combinators to read and write — it inverts control flow into a (state) -> Future<Option<(item, state)>> shape. Compare to the same handler in connect-go, where the runtime hands the handler both halves of the stream as plain method calls:
func (e *elizaServer) Converse(ctx context.Context, stream *connect.BidiStream[..., ...]) error {
for {
req, err := stream.Receive()
if errors.Is(err, io.EOF) { return nil }
if err != nil { return err }
reply, end := eliza.Reply(req.GetSentence())
if err := stream.Send(&elizav1.ConverseResponse{Sentence: reply}); err != nil { return err }
if end { return nil }
}
}
gRPC-Go (stream.Send on a generated stream type) and smithy-rs (EventStreamSender) take the same imperative-writer shape. tonic forces a type FooStream associated type plus a channel-and-spawn pattern, which is widely cited as one of its biggest ergonomics complaints — we should not converge toward that.
Proposed direction
Add a ServerStreamWriter<T> handle and an alternative handler signature where the runtime owns the response channel:
async fn converse(
&self,
ctx: RequestContext,
mut requests: ServiceStream<OwnedConverseRequestView>,
responses: ServerStreamWriter<ConverseResponse>,
) -> Result<(), ConnectError> {
while let Some(req) = requests.next().await {
let (reply, end) = eliza::reply(req?.sentence);
responses.send(ConverseResponse { sentence: reply, ..Default::default() }).await?;
if end { break; }
}
Ok(())
}
ServerStreamWriter<T> is a thin wrapper over a bounded mpsc::Sender:
async fn send(&self, item: T) -> Result<(), ConnectError>— back-pressure on a full channel; error if the client has gone away.async fn send_err(&self, err: ConnectError) -> Result<(), ConnectError>— push a stream-terminal error.fn send_headers(&self, headers: HeaderMap) -> Result<(), ConnectError>— flush response headers before the first item; no-op (or error) after.
The dispatcher spawns the handler future and drains the channel into HTTP/2 flow control, the same way it polls the returned Stream today.
Open design questions
- Whether the writer signature replaces, complements, or is an opt-in alternative to the current
-> ServiceResult<ServiceStream<T>>form. TheStream-returning form is genuinely better for transformation handlers (stream::iter(...).map(...)) and pure server-streaming. Coexistence (two trait methods with one delegating to the other, or a codegenstreaming_style=option) may be the right answer, but doubling the trait surface has costs. - Whether the same writer applies to plain server-streaming handlers, or only bidi.
- Channel bound default and configurability.
- How the writer interacts with the request
OwnedViewlifetime when the handler holds both across.await.
Scope
ServerStreamWriter<T>type + dispatcher plumbingconnectrpc-codegenchanges to emit the alternative signature- examples + guide updates
- Dominant language
- Rust
- Stars
- 544
- Forks
- 77
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from connectrpc/connect-rust
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
connectrpc/connect-rust#360 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
connectrpc/connect-rust#358 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 53/100
connectrpc/connect-rust#355 ·
-
Streaming request body errors are always reported as `internal`Possibly taken @AhmedElBanna80 claimed this 18 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 72/100
connectrpc/connect-rust#352 ·
-
Difficulty 3/5 Half a day Newbie friendliness 48/100
connectrpc/connect-rust#349 ·
All issues in connectrpc/connect-rust
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Choochmeque/tauri-plugin-webdriver#134 ·
Maintainers usually reply within 17 days
-
bug rust SDK & CLI & REST API
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
boxlite-ai/boxlite#3292 ·
Maintainers usually reply within 1 day
-
Android bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
anefzaoui/misterdownloadmanager#2516 ·
Maintainers usually reply within 1 day
-
editor ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
state:triage tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 89/100
agent8-cash/ethpayserver#731 ·
Maintainers usually reply within 1 day