Skip to content

Commit

Permalink
feat(request-id): add algorithm support nanoid (#6779)
Browse files Browse the repository at this point in the history
  • Loading branch information
aikin-vip authored Apr 7, 2022
1 parent fc3a1c9 commit 51eec3e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
6 changes: 5 additions & 1 deletion apisix/plugins/request-id.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local bit = require("bit")
local core = require("apisix.core")
local snowflake = require("snowflake")
local uuid = require("resty.jit-uuid")
local nanoid = require("nanoid")
local process = require("ngx.process")
local timers = require("apisix.timers")
local tostring = tostring
Expand All @@ -39,7 +40,7 @@ local schema = {
properties = {
header_name = {type = "string", default = "X-Request-Id"},
include_in_response = {type = "boolean", default = true},
algorithm = {type = "string", enum = {"uuid", "snowflake"}, default = "uuid"}
algorithm = {type = "string", enum = {"uuid", "snowflake", "nanoid"}, default = "uuid"}
}
}

Expand Down Expand Up @@ -205,6 +206,9 @@ local function get_request_id(algorithm)
if algorithm == "uuid" then
return uuid()
end
if algorithm == "nanoid" then
return nanoid.safe_simple()
end
return next_id()
end

Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/request-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ API request. The plugin will not add a request id if the `header_name` is alread
| ------------------- | ------- | ----------- | -------------- | ----- | -------------------------------------------------------------- |
| header_name | string | optional | "X-Request-Id" | | Request ID header name |
| include_in_response | boolean | optional | true | | Option to include the unique request ID in the response header |
| algorithm | string | optional | "uuid" | ["uuid", "snowflake"] | ID generation algorithm |
| algorithm | string | optional | "uuid" | ["uuid", "snowflake", "nanoid"] | ID generation algorithm |

## How To Enable

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/plugins/request-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ title: request-id
| ------------------- | ------- | -------- | -------------- | ------ | ------------------------------ |
| header_name | string | 可选 | "X-Request-Id" | | Request ID header name |
| include_in_response | boolean | 可选 | true | | 是否需要在返回头中包含该唯一 ID |
| algorithm | string | 可选 | "uuid" | ["uuid", "snowflake"] | ID 生成算法 |
| algorithm | string | 可选 | "uuid" | ["uuid", "snowflake", "nanoid"] | ID 生成算法 |

## 如何启用

Expand Down
1 change: 1 addition & 0 deletions rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ dependencies = {
"opentelemetry-lua = 0.1-3",
"net-url = 0.9-1",
"xml2lua = 1.5-2",
"nanoid = 0.1-1"
}

build = {
Expand Down
68 changes: 68 additions & 0 deletions t/plugin/request-id.t
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,71 @@ GET /opentracing
X-Request-ID: 123
--- response_headers
X-Request-ID: 123
=== TEST 20: add plugin with algorithm nanoid (default uuid)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local http = require "resty.http"
local v = {}
local ids = {}
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"request-id": {
"algorithm": "nanoid"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1982": 1
},
"type": "roundrobin"
},
"uri": "/opentracing"
}]]
)
if code >= 300 then
ngx.say("algorithm nanoid is error")
end
for i = 1, 180 do
local th = assert(ngx.thread.spawn(function()
local httpc = http.new()
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/opentracing"
local res, err = httpc:request_uri(uri,
{
method = "GET",
headers = {
["Content-Type"] = "application/json",
}
}
)
if not res then
ngx.log(ngx.ERR, err)
return
end
local id = res.headers["X-Request-Id"]
if not id then
return -- ignore if the data is not synced yet.
end
if ids[id] == true then
ngx.say("ids not unique")
return
end
ids[id] = true
end, i))
table.insert(v, th)
end
for i, th in ipairs(v) do
ngx.thread.wait(th)
end
ngx.say("true")
}
}
--- wait: 5
--- response_body
true

0 comments on commit 51eec3e

Please sign in to comment.