Repository navigation
fix(api-breaker): allow 101 as a healthy upstream status - #14304
Open
hanzhenfang wants to merge 2 commits into
Open
hanzhenfang wants to merge 2 commits into
hanzhenfang wants to merge 2 commits into
Conversation
hanzhenfang
marked this pull request as ready for review
December 2, 2026 02:44
shreemaan-abhishek
previously approved these changes
Dec 2, 2026
AlinsRan
previously approved these changes
Dec 2, 2026
moonming
requested changes
Dec 3, 2026
moonming
left a comment
Member
There was a problem hiding this comment.
Thanks for working on this, @hanzhenfang! Scoping the schema change specifically to 101 via anyOf (while rejecting 100/102–199) and keeping [200] as the default is clean and matches RFC 9110 §15.2.2.
Before merging, there is a lifecycle issue with counting 101 in _M.log: in OpenResty, log_by_lua only runs when the upgraded WebSocket tunnel closes, whereas $upstream_status (101) is fixed at handshake time. That causes two issues for real long-lived WebSocket sessions:
- Healthy open connections do not heal the breaker: After a transient outage, healthy WebSocket clients connect, receive
101, and keep their connections open for minutes or hours. Because_M.loghas not run yet,unhealthy_count(which has no TTL inshared_buffer) is never cleared, and a later transient failure escalatesbreaker_time(TEST 22only passes right now because it callsws:close()immediately after receiving the first frame). - Pre-existing connections dropping during an outage falsely reset an open breaker: If 3 WebSocket connections were established while the upstream was healthy (
101) and the upstream later crashes/overloads—tripping the breaker via503s—those 3 old WebSocket connections terminating during the outage will enter_M.logwithupstream_status == 101and immediately deletelasttime_keyandunhealthy_key, reopening traffic to the failing upstream mid-backoff.
Could we update the PR as follows?
apisix/plugins/api-breaker.lua: Record the upstream status in_M.header_filter(conf, ctx)(wherectx.var.upstream_statusalready holds the final upstream response code, including101) and set a flag onctx(e.g.ctx.api_breaker_recorded = true) so_M.log(conf, ctx)skips duplicate accounting while still acting as a fallback ifheader_filterdid not run.docs/en/latest/plugins/api-breaker.md&docs/zh/latest/plugins/api-breaker.md: Update thehealthy.http_statusesdescription accordingly so it reflects counting successful101upgrades at handshake response time rather than on connection close.t/plugin/api-breaker.t:- Update
TEST 22(e.g. withlib.server.websocket_hold) so the 3 WebSocket connections stay open when verifying that the next backoff resets to2seconds. - Add a test verifying that closing 3 WebSocket connections opened before a breaker trip does not clear an active breaker window.
- Update
Thanks again!
hanzhenfang
dismissed stale reviews from AlinsRan and shreemaan-abhishek
via
December 3, 2026 02:07
1452b42
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
A successful HTTP/1.1 WebSocket upgrade returns
101. Theapi-breakerschema only accepts status codes from 200 through 499 inhealthy.http_statuses. Even after a route starts accepting WebSocket connections again, those successful upgrades cannot count toward clearing the breaker's previous failures.Allow operators to add
101to the healthy status list. The default stays[200]. The plugin records the upstream status when the response headers arrive, so an upgraded connection can count toward recovery while the tunnel is open. The log phase handles requests whose status was unavailable earlier and skips outcomes already recorded.Local verification
With a failure threshold of five and a recovery threshold of three, the WebSocket regression test holds three connections open after a breaker window expires. Their successful handshakes clear the previous failures. The next five upstream 503s trigger a two-second backoff. The test also waits for those connections to close during a subsequent breaker window and checks that the breaker still returns its configured response.
The complete plugin test file and a local schema check covering HTTP status codes 100 through 599 passed. The accepted healthy statuses are 101 and 200 through 499.
Which issue(s) this PR fixes
No public issue linked.
Checklist