Skip to content

Commit d74a2ee

Browse files
iahsfacebook-github-bot
authored andcommitted
Return bool in BiDi stream callbacks
Summary: In practice each callback implementation has to track its own state, in which case the only important information captured by this return value is whether the calling object is still alive (as otherwise it can just check its state member for the rest of the information). Reviewed By: sazonovkirill Differential Revision: D79906351 fbshipit-source-id: 898b5cc44130a5ef7bb2e4e37a3bba6a2c2f5c5f
1 parent bd2466a commit d74a2ee

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

third-party/thrift/src/thrift/lib/cpp2/async/StreamCallbacks.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ struct BiDiChannelState {
422422
bool sinkAlive() const {
423423
return state_ != State::OnlyStreamOpen && state_ != State::Closed;
424424
}
425+
bool anyAlive() const { return state_ != State::Closed; }
426+
bool bothAlive() const {
427+
return state_ == State::StreamAndSinkOpen ||
428+
state_ == State::AwaitingFirstResponse;
429+
}
425430

426431
BiDiChannelState& markFirstResponseSent() {
427432
if (state_ == State::AwaitingFirstResponse) {
@@ -467,13 +472,12 @@ class BiDiServerCallback {
467472
public:
468473
virtual ~BiDiServerCallback() = default;
469474

470-
FOLLY_NODISCARD virtual BiDiChannelState onStreamRequestN(uint64_t) = 0;
471-
FOLLY_NODISCARD virtual BiDiChannelState onStreamCancel() = 0;
475+
FOLLY_NODISCARD virtual bool onStreamRequestN(uint64_t) = 0;
476+
FOLLY_NODISCARD virtual bool onStreamCancel() = 0;
472477

473-
FOLLY_NODISCARD virtual BiDiChannelState onSinkNext(StreamPayload&&) = 0;
474-
FOLLY_NODISCARD virtual BiDiChannelState onSinkError(
475-
folly::exception_wrapper) = 0;
476-
FOLLY_NODISCARD virtual BiDiChannelState onSinkComplete() = 0;
478+
FOLLY_NODISCARD virtual bool onSinkNext(StreamPayload&&) = 0;
479+
FOLLY_NODISCARD virtual bool onSinkError(folly::exception_wrapper) = 0;
480+
FOLLY_NODISCARD virtual bool onSinkComplete() = 0;
477481

478482
virtual void resetClientCallback(BiDiClientCallback&) = 0;
479483
};
@@ -482,26 +486,30 @@ class BiDiClientCallback {
482486
public:
483487
virtual ~BiDiClientCallback() = default;
484488

485-
FOLLY_NODISCARD virtual BiDiChannelState onFirstResponse(
489+
FOLLY_NODISCARD virtual bool onFirstResponse(
486490
FirstResponsePayload&&, folly::EventBase*, BiDiServerCallback*) = 0;
487491
virtual void onFirstResponseError(folly::exception_wrapper) = 0;
488492

489-
FOLLY_NODISCARD virtual BiDiChannelState onStreamNext(StreamPayload&&) = 0;
490-
FOLLY_NODISCARD virtual BiDiChannelState onStreamError(
491-
folly::exception_wrapper) = 0;
492-
FOLLY_NODISCARD virtual BiDiChannelState onStreamComplete() = 0;
493+
FOLLY_NODISCARD virtual bool onStreamNext(StreamPayload&&) = 0;
494+
FOLLY_NODISCARD virtual bool onStreamError(folly::exception_wrapper) = 0;
495+
FOLLY_NODISCARD virtual bool onStreamComplete() = 0;
493496

494-
FOLLY_NODISCARD virtual BiDiChannelState onSinkRequestN(uint64_t) = 0;
495-
FOLLY_NODISCARD virtual BiDiChannelState onSinkCancel() = 0;
497+
FOLLY_NODISCARD virtual bool onSinkRequestN(uint64_t) = 0;
498+
FOLLY_NODISCARD virtual bool onSinkCancel() = 0;
496499

497500
virtual void resetServerCallback(BiDiServerCallback&) = 0;
498501
};
499502

500503
struct BiDiServerCallbackCancel {
501504
void operator()(BiDiServerCallback* biDiServerCallback) noexcept {
502-
auto state = biDiServerCallback->onStreamCancel();
503-
// This should be handled as an early close.
504-
DCHECK(!state.sinkAlive() && !state.streamAlive());
505+
auto alive = biDiServerCallback->onStreamCancel();
506+
if (alive) {
507+
TApplicationException ex(
508+
TApplicationException::TApplicationExceptionType::INTERRUPTION,
509+
"BiDi server callback canceled");
510+
alive = biDiServerCallback->onSinkError(std::move(ex));
511+
}
512+
DCHECK(!alive);
505513
}
506514
};
507515

0 commit comments

Comments
 (0)