fix: [listener] make observedConnection.Close idempotent to prevent active connection metric negative drift#1019
Open
thinker0 wants to merge 1 commit into
Conversation
…ctive connection metric negative drift Signed-off-by: thinker0 <thinker0@gmail.com>
d96e8e2 to
145c6a8
Compare
crandles
reviewed
Jun 5, 2026
Comment on lines
+75
to
+78
| o.once.Do(func() { | ||
| metrics.ProxyActiveConnections.Dec() | ||
| metrics.ProxyConnectionClosed.Inc() | ||
| }) |
Contributor
There was a problem hiding this comment.
a second close should return an err
Suggested change
| o.once.Do(func() { | |
| metrics.ProxyActiveConnections.Dec() | |
| metrics.ProxyConnectionClosed.Inc() | |
| }) | |
| if err == nil { | |
| metrics.ProxyActiveConnections.Dec() | |
| metrics.ProxyConnectionClosed.Inc() | |
| } |
I think it would be simpler to skip sync.Once here if we add an err check
crandles
reviewed
Jun 5, 2026
|
|
||
| // Subsequent Closes should also succeed but NOT decrement metric further | ||
| for i := 0; i < 5; i++ { | ||
| _ = oconn.Close() |
Contributor
There was a problem hiding this comment.
you're not checking for success here, unlike what the comment says
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.
Description
This pull request solves the negative drift bug on the
trickster_proxy_active_connectionsPrometheus gauge metric.Root cause:
In
pkg/proxy/listener/listener.go, theClose()method onobservedConnectionwas not idempotent. Go'snet/httpserver can invokeClose()multiple times in different execution paths (e.g., Serve goroutine defer, timeout handlers, Hijack). Each invocation decremented the active connections gauge (metrics.ProxyActiveConnections.Dec()) and incremented the closed total, leading to monotonic negative drift over time under normal load.Solution:
Applied
sync.OnceinsideobservedConnectionto guarantee that connection close metrics are updated exactly once per connection lifecycle, ensuring gauge consistency.Also added high-stress unit testing (
TestObservedConnectionIdempotentCloseinlistener_test.go) undergo test -raceto verify idempotence and prevent regressions.Type of Change
AI Disclosure