membphis commented on PR #13909:
URL: https://github.com/apache/apisix/pull/13909#issuecomment-5520928764

   ### [P2] Keep the request-variable cache in sync when classifying WebSocket 
sessions
   
   The new header-filter assignment updates `ngx.var.request_type` directly, 
but `ctx.var` caches values on first read. If an earlier plugin has read 
`ctx.var.request_type`, the Prometheus exporter still receives 
`traditional_http` after a successful `101` upgrade.
   
   A supported trigger is a WebSocket route whose `proxy-rewrite.headers.set` 
contains `"X-Request-Type": "$request_type"`. The rewrite phase caches the old 
value, and the header filter then updates only the NGINX variable. As a result, 
`apisix_http_status`, `apisix_http_latency`, and `apisix_bandwidth` can still 
classify the session as ordinary HTTP. Filtering with 
`request_type!="websocket"` therefore leaves the long session in the latency 
statistics. This should be fixed before merge.
   
   This is deterministic when the earlier read occurs. WebSocket requests 
without that earlier read are unaffected, and forwarding itself is unchanged.
   
   Please write through `api_ctx.var.request_type = "websocket"` when the 
request context is available, preserving the no-context fallback. Add a 
regression that reads the variable in `rewrite` before a successful upgrade, 
then checks that both variable views and all three metric families use 
`websocket`.
   
   Validation: a focused LuaJIT reproduction using the repository's actual 
variable metatable and this PR's added statements, with a simulated NGINX 
variable interface, produced `ngx.var.request_type=websocket` and 
`ctx.var.request_type=traditional_http`. Writing through the existing context 
setter made both views agree.
   
   Evidence at `e7a399130955948d57ea3cb48a964a27372e695a`:
   
   - [Classification 
assignment](https://github.com/apache/apisix/blob/e7a399130955948d57ea3cb48a964a27372e695a/apisix/init.lua#L1027)
   - [Variable cache and 
setter](https://github.com/apache/apisix/blob/e7a399130955948d57ea3cb48a964a27372e695a/apisix/core/ctx.lua#L392)
   - [Earlier read in 
proxy-rewrite](https://github.com/apache/apisix/blob/e7a399130955948d57ea3cb48a964a27372e695a/apisix/plugins/proxy-rewrite.lua#L302)
   - [Prometheus label 
collection](https://github.com/apache/apisix/blob/e7a399130955948d57ea3cb48a964a27372e695a/apisix/plugins/prometheus/exporter.lua#L762)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to