Copilot commented on code in PR #3494:
URL: https://github.com/apache/brpc/pull/3494#discussion_r3865000734


##########
src/brpc/stream.cpp:
##########
@@ -333,57 +333,71 @@ int Stream::AppendIfNotFull(const butil::IOBuf &data,
     return 0;
 }
 
-void Stream::SetRemoteConsumed(size_t new_remote_consumed) {
-    CHECK(_cur_buf_size > 0);
+int Stream::SetRemoteConsumed(size_t new_remote_consumed) {
     bthread_id_list_t tmplist;
     CHECK_EQ(0, bthread_id_list_init(&tmplist, 0, 0));
-    bthread_mutex_lock(&_congestion_control_mutex);
-    if (_remote_consumed >= new_remote_consumed) {
-        bthread_mutex_unlock(&_congestion_control_mutex);
-        return;
-    }
-    const bool was_full = _produced >= _remote_consumed + _cur_buf_size;
-
-    if (FLAGS_socket_max_streams_unconsumed_bytes > 0 && _host_socket != 
nullptr) {
-        const size_t consumed_delta = new_remote_consumed - _remote_consumed;
-        const size_t accounted_delta =
-            std::min(consumed_delta, _socket_unconsumed_size);
-        if (accounted_delta != 0) {
-            _host_socket->_total_streams_unconsumed_size.fetch_sub(
-                accounted_delta, butil::memory_order_relaxed);
-            _socket_unconsumed_size -= accounted_delta;
+    BRPC_SCOPE_EXIT { bthread_id_list_destroy(&tmplist); };
+    {
+        BAIDU_SCOPED_LOCK(_congestion_control_mutex);
+        if (_cur_buf_size == 0) {
+            return -1;
         }
-        const int64_t total_unconsumed = 
_host_socket->_total_streams_unconsumed_size.load(
-                butil::memory_order_relaxed);
-        if (total_unconsumed > FLAGS_socket_max_streams_unconsumed_bytes) {
-            if (_options.min_buf_size > 0) {
-                _cur_buf_size = _options.min_buf_size;
-            } else {
-                _cur_buf_size /= 2;
+        if (_remote_consumed >= new_remote_consumed) {
+            return 0;
+        }
+        const bool was_full = _produced >= _remote_consumed + _cur_buf_size;
+
+        if (FLAGS_socket_max_streams_unconsumed_bytes > 0 &&
+            _host_socket != nullptr) {
+            const size_t consumed_delta =
+                new_remote_consumed - _remote_consumed;
+            const size_t accounted_delta =
+                std::min(consumed_delta, _socket_unconsumed_size);
+            if (accounted_delta != 0) {
+                _host_socket->_total_streams_unconsumed_size.fetch_sub(
+                    accounted_delta, butil::memory_order_relaxed);
+                _socket_unconsumed_size -= accounted_delta;
             }
-            LOG(INFO) << "stream consumers on socket " << _host_socket->id()
-                      << " is crowded, cut stream " << id()
-                      << " buffer to " << _cur_buf_size;
-        } else if (_produced >= new_remote_consumed + _cur_buf_size &&
-                   (_options.max_buf_size <= 0 || _cur_buf_size < 
(size_t)_options.max_buf_size)) {
-            if (_options.max_buf_size > 0 && _cur_buf_size * 2 > 
(size_t)_options.max_buf_size) {
-                _cur_buf_size = _options.max_buf_size;
-            } else {
-                _cur_buf_size *= 2;
+            const int64_t total_unconsumed =
+                _host_socket->_total_streams_unconsumed_size.load(
+                    butil::memory_order_relaxed);
+            if (total_unconsumed >
+                FLAGS_socket_max_streams_unconsumed_bytes) {
+                if (_options.min_buf_size > 0) {
+                    _cur_buf_size = _options.min_buf_size;
+                } else {
+                    _cur_buf_size /= 2;

Review Comment:
   When socket-level unconsumed bytes exceed the limit and min_buf_size is 
unset, shrinking `_cur_buf_size` via `/= 2` can eventually reduce it to 0 
(e.g., 1/2 -> 0). Once `_cur_buf_size` hits 0, `SetRemoteConsumed()` will start 
returning -1 and valid future FEEDBACK frames (still expected by the 
already-negotiated `need_feedback=true`) will close the stream with `EPROTO`. 
Consider clamping the minimum congestion window to 1 (or another small positive 
floor) to avoid turning feedback into a protocol error under prolonged 
congestion.



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