Skip to content

Commit

Permalink
feat(server): return actual error when context done (#581)
Browse files Browse the repository at this point in the history
### Motivation

Return the actual errors if the context is done. 

### Modification

- Return the actual error to client.
- normal close stream if client closed stream.
  • Loading branch information
mattisonchao authored Nov 16, 2024
1 parent 02d3f5c commit 79fea6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
6 changes: 4 additions & 2 deletions server/leader_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"github.com/streamnative/oxia/server/wal"
)

var ErrLeaderClosed = errors.New("the leader has been closed")

type GetResult struct {
Response *proto.GetResponse
Err error
Expand Down Expand Up @@ -874,9 +876,9 @@ func (lc *leaderController) WriteStream(stream proto.OxiaClient_WriteStreamServe
case err := <-closeStreamCh:
return err
case <-stream.Context().Done():
return context.Canceled
return stream.Context().Err()
case <-lc.ctx.Done():
return context.Canceled
return errors.Wrap(ErrLeaderClosed, lc.ctx.Err().Error())
}
}

Expand Down
19 changes: 11 additions & 8 deletions server/public_rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,17 @@ func (s *publicRpcServer) WriteStream(stream proto.OxiaClient_WriteStreamServer)
return err
}

err = lc.WriteStream(stream)
if err != nil &&
!errors.Is(err, io.EOF) &&
!errors.Is(err, context.Canceled) {
log.Warn(
"Write stream failed",
slog.Any("error", err),
)
if err = lc.WriteStream(stream); err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, context.Canceled) {
log.Info("Write stream has been closed")
return nil
}
if errors.Is(err, ErrLeaderClosed) {
log.Info("Write stream has been closed by leader closed")
return err
}
log.Warn("Write stream has been closed by error", slog.Any("error", err))
return err
}
return err
}
Expand Down

0 comments on commit 79fea6a

Please sign in to comment.