Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stream client closers non-blocking #791

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (s *ServerStreamForClient[Res]) ResponseTrailer() http.Header {
}

// Close the receive side of the stream.
//
// Close is non-blocking. To gracefully close the stream and allow for
// connection resuse ensure all messages have been received before calling
// Close. All messages are received when Receive returns false.
func (s *ServerStreamForClient[Res]) Close() error {
if s.constructErr != nil {
return s.constructErr
Expand Down Expand Up @@ -251,6 +255,11 @@ func (b *BidiStreamForClient[Req, Res]) Receive() (*Res, error) {
}

// CloseResponse closes the receive side of the stream.
//
// CloseResponse is non-blocking. To gracefully close the stream and allow for
// connection resuse ensure all messages have been received before calling
// CloseResponse. All messages are received when Receive returns an error
// wrapping [io.EOF].
func (b *BidiStreamForClient[Req, Res]) CloseResponse() error {
if b.err != nil {
return b.err
Expand Down
8 changes: 1 addition & 7 deletions duplex_http_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,7 @@ func (d *duplexHTTPCall) CloseRead() error {
if d.response == nil {
return nil
}
_, err := discard(d.response.Body)
closeErr := d.response.Body.Close()
if err == nil ||
errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded) {
err = closeErr
}
err := d.response.Body.Close()
err = wrapIfContextDone(d.ctx, err)
return wrapIfRSTError(err)
}
Expand Down