AlinsRan opened a new pull request, #14140:
URL: https://github.com/apache/apisix/pull/14140

   ### Description
   
   `request-id` runs on every request when it is used as a global rule, and it 
did more work than it needed:
   
   - `rewrite` called `ngx.req.get_headers()` to read the configured request 
header.
   - `header_filter` called `ngx.resp.get_headers()` to read the same header on 
the response.
   - The default `uuid` algorithm called `resty.jit-uuid`, which formats every 
id with 16 `bit.tohex` calls and one `string.format` over 16 arguments.
   
   Changes:
   
   - `rewrite` reads the header from the per-request table that 
`core.request.headers(ctx)` already caches for other plugins. A repeated header 
still comes back as a table, as with `ngx.req.get_headers()`, so every value is 
still echoed. That table is copied, because later header changes (for example 
`proxy-rewrite` `headers.add`) update the cached table in place.
   - `header_filter` reads the header through `ngx.header`. A header name 
containing `_` still goes through `ngx.resp.get_headers()`, whose lookup maps 
`_` to `-` even when `lua_transform_underscores_in_response_headers` is off, 
while `ngx.header` does not.
   - `core.utils.generate_uuid_v4`, next to `generate_uuid_v7`, draws the same 
random bytes in the same order as `resty.jit-uuid`'s `generate_v4` and sets the 
same version and variant bits, but encodes each byte through a 256-entry hex 
table. For a given seed it returns the same id.
   
   Microbenchmark of v4 generation (LuaJIT, results kept alive): 
`resty.jit-uuid` 0.60 µs per id, `core.utils.generate_uuid_v4` 0.20 µs per id, 
same allocation.
   
   No behaviour change: the header name, `include_in_response`, the other 
algorithms, and an upstream that already sets the header all work as before.
   
   Tests added to `t/plugin/request-id.t`:
   
   - TEST 29: a request carrying the header twice gets both values back.
   - TEST 30–31: when the upstream response already carries the header, the 
plugin keeps it.
   - TEST 32: a value added by `proxy-rewrite` later in the same request is not 
echoed with a repeated request id.
   - TEST 33–34: with `lua_transform_underscores_in_response_headers off`, a 
`header_name` with underscores still finds the upstream's header and adds no 
second one.
   - TEST 35: `generate_uuid_v4` and `resty.jit-uuid` return the same ids over 
10,000 seeds.
   
   #### Which issue(s) this PR fixes:
   
   None.
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [ ] I have updated the documentation to reflect this change
   - [x] I have verified that this change is backward compatible (If not, 
please discuss on the [APISIX mailing 
list](https://github.com/apache/apisix/tree/master#community) first)
   


-- 
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