Skip to content

prevent indefinite defer-close by checking last_active_time#3216

Merged
chenBright merged 1 commit into
apache:masterfrom
jenrryyou:refine_defer_close_second
Mar 10, 2026
Merged

prevent indefinite defer-close by checking last_active_time#3216
chenBright merged 1 commit into
apache:masterfrom
jenrryyou:refine_defer_close_second

Conversation

@jenrryyou

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

在 SocketMap 里决定“是否进入 defer_close 倒计时”时,不再无条件
defer,而是先看该连接最近一次读/写的活跃时间。

• 具体改动:当 ref_count 归零准备移除连接时,只有在“连接最近仍有活跃(last_active_time_us 距今不超过 defer_close_second)”的情况下,才把 no_ref_us
置为当前时间、开始倒计时;如果连接其实已经闲置超过 defer_close_second,则直接从 socket_map 删除并释放 socket 引用(src/brpc/socket_map.cpp:293)。
• 意图:避免老 socket 在“channel 频繁创建/析构导致 ref_count 归零又很快被重新引用”的场景下反复刷新倒计时,从而把一个已经长期不活跃/可能已失败的连接在
socket_map 里拖很久,导致新连接迟迟不创建、请求持续复用老连接并依赖其健康检查重连(进而在重连慢时持续失败)。

What is changed and the side effects?

Changed:

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

@wwbmmm

wwbmmm commented Feb 6, 2026

Copy link
Copy Markdown
Contributor

You have changed the default behavior of SocketMap. Although it may be a better practice, it may cause some side effects to existing users. I suggest to add a new gflag to control this behavior, and update the document about defer_close_second.

@jenrryyou

Copy link
Copy Markdown
Contributor Author

You have changed the default behavior of SocketMap. Although it may be a better practice, it may cause some side effects to existing users. I suggest to add a new gflag to control this behavior, and update the document about defer_close_second.

Good suggestion, I will refine this MR.

@jenrryyou jenrryyou force-pushed the refine_defer_close_second branch from 71732b3 to c1bdb77 Compare February 14, 2026 08:02
@wwbmmm

wwbmmm commented Feb 15, 2026

Copy link
Copy Markdown
Contributor

LGTM

@jenrryyou

Copy link
Copy Markdown
Contributor Author

LGTM

If no other questions, could you help merge it?

@chenBright chenBright left a comment

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.

LGTM

@chenBright chenBright merged commit a103b4b into apache:master Mar 10, 2026
17 checks passed
zchuango pushed a commit to zchuango/brpc that referenced this pull request May 9, 2026
)

Co-authored-by: youzhiyuan <youzhiyuan@bytedance.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants