chenBright commented on code in PR #3163:
URL: https://github.com/apache/brpc/pull/3163#discussion_r2584809275
##########
src/brpc/controller.cpp:
##########
@@ -174,6 +175,86 @@ class IgnoreAllRead : public ProgressiveReader {
void OnEndOfMessage(const butil::Status&) {}
};
+class ProgressiveTimeoutReader : public ProgressiveReader {
+public:
+ explicit ProgressiveTimeoutReader(SocketId id, int32_t read_timeout_ms,
ProgressiveReader* reader):
+ _socket_id(id),
+ _read_timeout_ms(read_timeout_ms),
+ _reader(reader),
+ _timeout_id(0),
+ _latest_add_timer_ms(0),
+ _add_timer_delay_ms(1000),
+ _is_read_timeout(false) {
+ AddIdleReadTimeoutMonitor();
+ }
+ butil::Status OnReadOnePart(const void* data, size_t length) {
+ auto status = _reader->OnReadOnePart(data, length);
+ AddIdleReadTimeoutMonitor();
+ return status;
+ }
+ void OnEndOfMessage(const butil::Status& status) {
+ if (_is_read_timeout) {
+ _reader->OnEndOfMessage(butil::Status(ECONNRESET, "The progressive
read timeout"));
+ } else {
+ _reader->OnEndOfMessage(status);
+ }
+ if(_timeout_id > 0) {
+ bthread_timer_del(_timeout_id);
+ }
+ }
+
+ SocketId GetSocketId() {
+ return _socket_id;
+ }
+
+ int32_t read_timeout_ms() {
+ return _read_timeout_ms;
+ }
+
+ void set_read_timeout(bool read_timeout = true) {
+ _is_read_timeout = read_timeout;
+ }
+
+private:
+ void AddToTimer() {
+ if (_timeout_id > 0) {
+ bthread_timer_del(_timeout_id);
+ }
+ bthread_timer_add(&_timeout_id,
+ butil::milliseconds_from_now(_read_timeout_ms),
+ Controller::HandleIdleProgressiveReader,
+ this
+ );
+ }
+
+ void AddIdleReadTimeoutMonitor() {
+ if (_read_timeout_ms <= 0) {
+ return;
+ }
+ if(_read_timeout_ms < _add_timer_delay_ms) {
+ AddToTimer();
+ return;
+ }
+ auto current_ms = butil::cpuwide_time_ms();
+ if (current_ms - _latest_add_timer_ms < _add_timer_delay_ms) {
+ return;
+ }
+ _latest_add_timer_ms = current_ms;
+ AddToTimer();
Review Comment:
Why do we need to add multiple timers?
##########
src/brpc/policy/http_rpc_protocol.h:
##########
@@ -122,6 +130,7 @@ class HttpContext : public ReadableProgressiveAttachment
private:
bool _is_stage2;
+ SocketId _socket_id;
Review Comment:
Initialization is missed.
##########
src/brpc/controller.cpp:
##########
@@ -174,6 +175,86 @@ class IgnoreAllRead : public ProgressiveReader {
void OnEndOfMessage(const butil::Status&) {}
};
+class ProgressiveTimeoutReader : public ProgressiveReader {
+public:
+ explicit ProgressiveTimeoutReader(SocketId id, int32_t read_timeout_ms,
ProgressiveReader* reader):
+ _socket_id(id),
+ _read_timeout_ms(read_timeout_ms),
+ _reader(reader),
+ _timeout_id(0),
+ _latest_add_timer_ms(0),
+ _add_timer_delay_ms(1000),
+ _is_read_timeout(false) {
+ AddIdleReadTimeoutMonitor();
+ }
+ butil::Status OnReadOnePart(const void* data, size_t length) {
+ auto status = _reader->OnReadOnePart(data, length);
+ AddIdleReadTimeoutMonitor();
+ return status;
+ }
+ void OnEndOfMessage(const butil::Status& status) {
+ if (_is_read_timeout) {
+ _reader->OnEndOfMessage(butil::Status(ECONNRESET, "The progressive
read timeout"));
+ } else {
+ _reader->OnEndOfMessage(status);
+ }
+ if(_timeout_id > 0) {
+ bthread_timer_del(_timeout_id);
+ }
+ }
+
+ SocketId GetSocketId() {
+ return _socket_id;
+ }
+
+ int32_t read_timeout_ms() {
+ return _read_timeout_ms;
+ }
+
+ void set_read_timeout(bool read_timeout = true) {
+ _is_read_timeout = read_timeout;
+ }
+
+private:
+ void AddToTimer() {
+ if (_timeout_id > 0) {
+ bthread_timer_del(_timeout_id);
+ }
+ bthread_timer_add(&_timeout_id,
+ butil::milliseconds_from_now(_read_timeout_ms),
+ Controller::HandleIdleProgressiveReader,
Review Comment:
Controller::HandleIdleProgressiveReader ->
ProgressiveTimeoutReader::HandleIdleProgressiveReader
##########
src/brpc/controller.cpp:
##########
@@ -1028,6 +1118,44 @@ void Controller::SubmitSpan() {
_span = NULL;
}
+void Controller::HandleIdleProgressiveReader(void* arg) {
+ if(arg == nullptr){
+ LOG(ERROR) << "Controller::HandleIdleProgressiveReader arg is null.";
+ return;
+ }
+ ProgressiveTimeoutReader* reader =
static_cast<ProgressiveTimeoutReader*>(arg);
+ SocketUniquePtr s;
+ if (Socket::Address(reader->GetSocketId(), &s) != 0) {
+ LOG(ERROR) << "not found the socket id : " << reader->GetSocketId();
+ return;
+ }
+ auto log_idle = FLAGS_log_idle_progressive_read_close;
+ int64_t progressive_read_timeout_us = reader->read_timeout_ms() * 1000;
+ int64_t pre_idle_duration_us = 0;
+ int64_t idle_duration_us = butil::cpuwide_time_us() -
s->last_active_time_us();
+ while (progressive_read_timeout_us > idle_duration_us && idle_duration_us
> pre_idle_duration_us) {
+ auto sleep_ms = (progressive_read_timeout_us - idle_duration_us) /
1000;
+ bthread_usleep(sleep_ms > 0 ? sleep_ms : 1);
+ pre_idle_duration_us = idle_duration_us;
+ idle_duration_us = butil::cpuwide_time_us() - s->last_active_time_us();
+ }
+ if (idle_duration_us <= pre_idle_duration_us) {
+ LOG_IF(INFO, log_idle) << "stop progressive read timeout checking
process!"
+ << " progressive_read_timeout_us : " << progressive_read_timeout_us
+ << " idle_duration_us : " << idle_duration_us
+ << " pre_idle_duration_us : " << pre_idle_duration_us;
+ return;
+ }
+ reader->set_read_timeout();
Review Comment:
There is a thread safety issue here. The process will crash if the reader
has already been destructed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]