Skip to content

fix(api-breaker): allow 101 as a healthy upstream status - #14304

Open
hanzhenfang wants to merge 2 commits into
apache:masterfrom
hanzhenfang:fix/api-breaker-websocket-101
Open

hanzhenfang wants to merge 2 commits into
apache:masterfrom
hanzhenfang:fix/api-breaker-websocket-101

Conversation

@hanzhenfang

@hanzhenfang hanzhenfang commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

A successful HTTP/1.1 WebSocket upgrade returns 101. The api-breaker schema only accepts status codes from 200 through 499 in healthy.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 101 to 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

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible

@hanzhenfang
hanzhenfang marked this pull request as ready for review December 2, 2026 02:44
AlinsRan
AlinsRan previously approved these changes Dec 2, 2026

@moonming moonming left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.log has not run yet, unhealthy_count (which has no TTL in shared_buffer) is never cleared, and a later transient failure escalates breaker_time (TEST 22 only passes right now because it calls ws:close() immediately after receiving the first frame).
  2. 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 via 503s—those 3 old WebSocket connections terminating during the outage will enter _M.log with upstream_status == 101 and immediately delete lasttime_key and unhealthy_key, reopening traffic to the failing upstream mid-backoff.

Could we update the PR as follows?

  1. apisix/plugins/api-breaker.lua: Record the upstream status in _M.header_filter(conf, ctx) (where ctx.var.upstream_status already holds the final upstream response code, including 101) and set a flag on ctx (e.g. ctx.api_breaker_recorded = true) so _M.log(conf, ctx) skips duplicate accounting while still acting as a fallback if header_filter did not run.
  2. docs/en/latest/plugins/api-breaker.md & docs/zh/latest/plugins/api-breaker.md: Update the healthy.http_statuses description accordingly so it reflects counting successful 101 upgrades at handshake response time rather than on connection close.
  3. t/plugin/api-breaker.t:
    • Update TEST 22 (e.g. with lib.server.websocket_hold) so the 3 WebSocket connections stay open when verifying that the next backoff resets to 2 seconds.
    • Add a test verifying that closing 3 WebSocket connections opened before a breaker trip does not clear an active breaker window.

Thanks again!

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.

4 participants