server/plugin: add CloseUserConn operation to server plugin system#5228
Open
xb-bxy wants to merge 2 commits intofatedier:devfrom
Open
server/plugin: add CloseUserConn operation to server plugin system#5228xb-bxy wants to merge 2 commits intofatedier:devfrom
CloseUserConn operation to server plugin system#5228xb-bxy wants to merge 2 commits intofatedier:devfrom
Conversation
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (7 files)
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a CloseUserConn lifecycle hook to the frp server plugin system, allowing plugins to be notified when a proxied user TCP connection terminates. This mirrors the existing NewUserConn hook and enables plugins to track connection duration, emit per-connection metrics, and perform cleanup.
Changes:
- Adds
OpCloseUserConnconstant,CloseUserConnContentstruct,closeUserConnPluginsslice, andManager.CloseUserConn()method to the server plugin package - Registers
OpCloseUserConnin the supported plugin ops validation list - Adds a
defer CloseUserConn(...)call inhandleUserTCPConnectionand includes a minor improvement to the debug log for closed connections
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
pkg/plugin/server/plugin.go |
Adds OpCloseUserConn = "CloseUserConn" operation constant |
pkg/plugin/server/types.go |
Adds CloseUserConnContent struct (identical fields to NewUserConnContent) |
pkg/plugin/server/manager.go |
Adds closeUserConnPlugins slice, registers plugins for the new op, and implements CloseUserConn() fan-out method |
pkg/config/v1/validation/validation.go |
Adds OpCloseUserConn to SupportedHTTPPluginOps validation list |
server/proxy/proxy.go |
Adds defer CloseUserConn(...) after successful NewUserConn; also changes err = … to err := … (shadowing bug) and removes workConn = nil |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
xb-bxy
commented
Mar 17, 2026
Author
xb-bxy
left a comment
There was a problem hiding this comment.
add CloseUserConn test cases for plugin server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WHY --> Feature Request
When a user connection is accepted, the
NewUserConnplugin operation is triggered. However, there is no corresponding lifecycle hook when the user connection is closed. This makes it impossible for plugins to track connection duration, perform cleanup, emit metrics per connection, or audit user session end-events.Without
CloseUserConn, plugins have no way to know when a proxied user connection has terminated.Changes
OpCloseUserConn = "CloseUserConn"operation constant to the plugin op setCloseUserConnContentstruct (mirrorsNewUserConnContent) withuser,proxy_name,proxy_type, andremote_addrfieldscloseUserConnPlugins []Pluginfield toManagerand register plugins that supportOpCloseUserConnManager.CloseUserConn()method that fans out the event to all registered plugins (errors are collected and logged as warnings, not blocking)handleUserTCPConnection, add adeferthat callsCloseUserConnafter the user connection closes, passing the same content asNewUserConnworkConn = nilafterworkConn.Close()inGetWorkConnFromPool, and improve the closing log withuserConn.RemoteAddrServer Plugin Config Example
Plugin handler receives
CloseUserConnPOST request with body:{ "version": "0.1.0", "op": "CloseUserConn", "content": { "user": { "user": "alice", "metas": {}, "run_id": "xxx" }, "proxy_name": "my-proxy", "proxy_type": "tcp", "remote_addr": "1.2.3.4:12345" } }Test plan
CloseUserConnop receives the event after each user connection closesCloseUserConnis not called (zero-cost fast path whencloseUserConnPluginsis empty)CloseUserConnerrors are logged as warnings and do not affect the data transfer resultNewUserConn→ data transfer →CloseUserConnlifecycle is correct end-to-endgo vet plugin.cleango buildcompiles cleanlygo test plugin.)