chenBright commented on code in PR #3163:
URL: https://github.com/apache/brpc/pull/3163#discussion_r2587244484


##########
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:
   I think a timeout for a total progressive read is sufficient. No need to pay 
attention to each read.



-- 
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]

Reply via email to