Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/brpc/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const Controller* GetSubControllerOfSelectiveChannel(

DECLARE_bool(usercode_in_pthread);
DECLARE_bool(usercode_in_coroutine);
DECLARE_bool(concurrency_remover_manages_after_rpc_resp);
static const int MAX_RETRY_COUNT = 1000;
static bvar::Adder<int64_t>* g_ncontroller = NULL;

Expand Down Expand Up @@ -298,6 +299,7 @@ void Controller::ResetPods() {
_response_streams.clear();
_remote_stream_settings = NULL;
_auth_flags = 0;
_concurrency_remover_manages_after_rpc_resp = false;
_rpc_received_us = 0;
}

Expand Down Expand Up @@ -1593,6 +1595,12 @@ int Controller::GetSockOption(int level, int optname, void* optval, socklen_t* o
}
}

void Controller::set_after_rpc_resp_fn(AfterRpcRespFnType&& fn) {
_after_rpc_resp_fn = fn;
// Set the flag from global gflag when after_rpc_resp_fn is set
_concurrency_remover_manages_after_rpc_resp = FLAGS_concurrency_remover_manages_after_rpc_resp;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not rely on gflag, but rely on the Controller's _flags.

uint32_t _flags; // all boolean fields inside Controller

brpc/src/brpc/controller.h

Lines 133 to 154 in f97f23e

static const uint32_t FLAGS_IGNORE_EOVERCROWDED = 1;
static const uint32_t FLAGS_SECURITY_MODE = (1 << 1);
static const uint32_t FLAGS_ADDED_CONCURRENCY = (1 << 2);
static const uint32_t FLAGS_READ_PROGRESSIVELY = (1 << 3);
static const uint32_t FLAGS_PROGRESSIVE_READER = (1 << 4);
static const uint32_t FLAGS_BACKUP_REQUEST = (1 << 5);
// Let _done delete the correlation_id, used by combo channels to
// make lifetime of the correlation_id more flexible.
static const uint32_t FLAGS_DESTROY_CID_IN_DONE = (1 << 7);
static const uint32_t FLAGS_CLOSE_CONNECTION = (1 << 8);
static const uint32_t FLAGS_LOG_ID = (1 << 9); // log_id is set
static const uint32_t FLAGS_REQUEST_CODE = (1 << 10);
static const uint32_t FLAGS_PB_BYTES_TO_BASE64 = (1 << 11);
static const uint32_t FLAGS_ALLOW_DONE_TO_RUN_IN_PLACE = (1 << 12);
static const uint32_t FLAGS_USED_BY_RPC = (1 << 13);
static const uint32_t FLAGS_PB_JSONIFY_EMPTY_ARRAY = (1 << 16);
static const uint32_t FLAGS_ENABLED_CIRCUIT_BREAKER = (1 << 17);
static const uint32_t FLAGS_ALWAYS_PRINT_PRIMITIVE_FIELDS = (1 << 18);
static const uint32_t FLAGS_HEALTH_CHECK_CALL = (1 << 19);
static const uint32_t FLAGS_PB_SINGLE_REPEATED_TO_ARRAY = (1 << 20);
static const uint32_t FLAGS_MANAGE_HTTP_BODY_ON_ERROR = (1 << 21);
static const uint32_t FLAGS_WRITE_TO_SOCKET_IN_BACKGROUND = (1 << 22);

}

void Controller::CallAfterRpcResp(const google::protobuf::Message* req, const google::protobuf::Message* res) {
if (_after_rpc_resp_fn) {
_after_rpc_resp_fn(this, req, res);
Expand Down
8 changes: 7 additions & 1 deletion src/brpc/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,15 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
const google::protobuf::Message* req,
const google::protobuf::Message* res)>;

void set_after_rpc_resp_fn(AfterRpcRespFnType&& fn) { _after_rpc_resp_fn = fn; }
void set_after_rpc_resp_fn(AfterRpcRespFnType&& fn);

void CallAfterRpcResp(const google::protobuf::Message* req, const google::protobuf::Message* res);

// Check whether ConcurrencyRemover should manage the lifecycle of CallAfterRpcResp.
bool concurrency_remover_manages_after_rpc_resp() const {
return _concurrency_remover_manages_after_rpc_resp;
}

void set_request_content_type(ContentType type) {
_request_content_type = type;
}
Expand Down Expand Up @@ -921,6 +926,7 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
uint32_t _auth_flags;

AfterRpcRespFnType _after_rpc_resp_fn;
bool _concurrency_remover_manages_after_rpc_resp;

// The point in time when the rpc is read from the socket
int64_t _rpc_received_us;
Expand Down
18 changes: 15 additions & 3 deletions src/brpc/policy/baidu_rpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ DEFINE_bool(baidu_protocol_use_fullname, true,
DEFINE_bool(baidu_std_protocol_deliver_timeout_ms, false,
"If this flag is true, baidu_std puts timeout_ms in requests.");

DEFINE_bool(concurrency_remover_manages_after_rpc_resp, false,
"If this flag is true, ConcurrencyRemover will manage the lifecycle "
"of CallAfterRpcResp, ensuring concurrency control covers the entire "
"response processing including after-response callbacks.");

DECLARE_bool(pb_enum_as_number);

// Notes:
Expand Down Expand Up @@ -285,9 +290,14 @@ void SendRpcResponse(int64_t correlation_id, Controller* cntl,

// Recycle resources at the end of this function.
BRPC_SCOPE_EXIT {
{
// Remove concurrency and record latency at first.
ConcurrencyRemover concurrency_remover(method_status, cntl, received_us);
std::unique_ptr<ConcurrencyRemover> concurrency_remover_ptr(
new ConcurrencyRemover(method_status, cntl, received_us));

// Only manage CallAfterRpcResp lifecycle if the flag is set
// (which happens when set_after_rpc_resp_fn is called with the gflag enabled)
if (!cntl->concurrency_remover_manages_after_rpc_resp()) {
// Original behavior: remove concurrency before CallAfterRpcResp
concurrency_remover_ptr.reset();
}

std::unique_ptr<Controller, LogErrorTextAndDelete> recycle_cntl(cntl);
Expand All @@ -302,6 +312,8 @@ void SendRpcResponse(int64_t correlation_id, Controller* cntl,
} else {
BaiduProxyPBMessages::Return(static_cast<BaiduProxyPBMessages*>(messages));
}
// If concurrency_remover_manages_after_rpc_resp() is true,
// concurrency_remover_ptr will be destroyed here
};

StreamIds response_stream_ids = accessor.response_streams();
Expand Down
Loading