Skip to content
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
13 changes: 12 additions & 1 deletion src/extensions/rppgrpc/rppgrpc/client_reactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace rppgrpc
* - to access values FROM stream you can subscribe to observable obtained via `reactor->get_observable()` (same observable WOULD emit on_completed in case of successful stream termination and on_error in case of some errors with grpc stream)
* - to pass values TO stream you can emit values to observer obtained via `reactor->get_observer()`
*
* @warning After `init()` the call may end at any moment (connection refused, deadline, cancel) and the reactor deletes itself in `OnDone`. Obtain the observer and emit the first values before `init()`: they are posted and sent once the call starts. Emitting from outside the reactor's callbacks after `init()` races with the end of the call (gRPC requires a hold for that, see `grpc::ClientBidiReactor::AddHold`).
* @warning After `init()` the call may end at any moment (connection refused, deadline, cancel) and the reactor deletes itself in `OnDone`, so obtain the observer and the observable before `init()` and do not use the reactor afterwards. The observer may be used from any thread, also after `init()`: the reactor holds the call (see `grpc::ClientBidiReactor::AddHold`) until its read side has ended; values still queued then, and values emitted after that, are dropped.
*
* @snippet client_reactor.cpp bidi_reactor
*
Expand All @@ -46,6 +46,9 @@ namespace rppgrpc

void init()
{
// The observer may write from outside the reactions after StartCall(), which gRPC allows
// only under a hold. OnReadDone(false) releases it.
Base::AddHold();
// The first read is posted before StartCall(): gRPC keeps it until the call starts and
// counts it as an outstanding operation. StartCall() is the last use of the reactor in
// init(), because a call that ends at once may run OnDone (and so `delete this`) on a
Expand Down Expand Up @@ -79,7 +82,15 @@ namespace rppgrpc
void OnReadDone(bool ok) override
{
if (!ok)
{
// One read is always posted, so each call ends its read side exactly once, here.
// A write that took the observer's lock before close_writes() has called StartWrite
// and is counted by gRPC, so OnDone comes after its OnWriteDone; later and still queued
// writes are dropped. This reaction is itself still counted, so RemoveHold() does not run OnDone.
details::base_writer<Request>::close_writes();
Base::RemoveHold();
return;
}

details::base_reader<Response>::handle_read_done();
}
Expand Down
79 changes: 57 additions & 22 deletions src/extensions/rppgrpc/rppgrpc/details/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "rpp/subjects/publish_subject.hpp"

#include <deque>
#include <memory>
#include <mutex>

namespace rppgrpc::details
Expand All @@ -29,9 +30,14 @@ namespace rppgrpc::details
public:
base_writer()
{
m_subject.get_observable().subscribe(typename details::base_writer<TData>::observer_strategy{*this});
m_state->owner = this;
m_subject.get_observable().subscribe(typename details::base_writer<TData>::observer_strategy{m_state});
}

// The observer's shared state points back at this object, so a copy would write through the original.
base_writer(const base_writer&) = delete;
base_writer& operator=(const base_writer&) = delete;

virtual ~base_writer() noexcept = default;

auto get_observer() const
Expand All @@ -45,54 +51,85 @@ namespace rppgrpc::details

void handle_on_done()
{
close_writes();
m_subject.get_disposable().dispose();
}

// Values and terminals emitted to the observer after this call, and values still queued, reach neither this object nor its stream.
// It waits for an emission that is inside start_write or finish_writes.
void close_writes()
{
std::lock_guard lock{m_state->mutex};
m_state->owner = nullptr;
}

void handle_write_done()
{
std::lock_guard lock{m_write_mutex};
m_write.pop_front();
std::lock_guard lock{m_state->mutex};
m_state->queue.pop_front();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

if (!m_state->owner)
return;

if (!m_write.empty())
if (!m_state->queue.empty())
{
start_write(m_write.front());
start_write(m_state->queue.front());
}
else if (m_finished)
else if (m_state->finished)
{
finish_writes(grpc::Status::OK);
}
}

// Shared by this object and the observer subscribed to the subject, so it outlives this object: an emission
// that has passed the subject when this object ends still locks a live mutex and finds no owner.
struct write_state
{
std::mutex mutex{};
std::deque<TData> queue{};
bool finished{};
base_writer* owner{};
};

struct observer_strategy
{
static constexpr auto preferred_disposables_mode = rpp::details::observers::disposables_mode::Auto;

std::reference_wrapper<base_writer> owner{};
std::shared_ptr<write_state> state{};

template<rpp::constraint::decayed_same_as<TData> T>
void on_next(T&& message) const
{
std::lock_guard lock{owner.get().m_write_mutex};
owner.get().m_write.push_back(std::forward<T>(message));
if (owner.get().m_write.size() == 1)
owner.get().start_write(owner.get().m_write.front());
std::lock_guard lock{state->mutex};
if (!state->owner)
return;

state->queue.push_back(std::forward<T>(message));
if (state->queue.size() == 1)
state->owner->start_write(state->queue.front());
}

void on_error(const std::exception_ptr&) const
{
std::lock_guard lock{owner.get().m_write_mutex};
owner.get().m_finished = true;
std::lock_guard lock{state->mutex};
if (!state->owner)
return;

if (owner.get().m_write.size() == 0)
owner.get().finish_writes(grpc::Status{grpc::StatusCode::INTERNAL, "Internal error happens"});
state->finished = true;

if (state->queue.size() == 0)
state->owner->finish_writes(grpc::Status{grpc::StatusCode::INTERNAL, "Internal error happens"});
}
void on_completed() const
{
std::lock_guard lock{owner.get().m_write_mutex};
owner.get().m_finished = true;
std::lock_guard lock{state->mutex};
if (!state->owner)
return;

state->finished = true;

if (owner.get().m_write.size() == 0)
owner.get().finish_writes(grpc::Status::OK);
if (state->queue.size() == 0)
state->owner->finish_writes(grpc::Status::OK);
}

static constexpr bool is_disposed() { return false; }
Expand All @@ -102,9 +139,7 @@ namespace rppgrpc::details
private:
rpp::subjects::serialized_publish_subject<TData> m_subject{};

std::mutex m_write_mutex{};
std::deque<TData> m_write{};
bool m_finished{};
std::shared_ptr<write_state> m_state = std::make_shared<write_state>();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
};

template<rpp::constraint::decayed_type TData>
Expand Down
Loading
Loading