From a91f67f7c754ab20f43c96105e42bd42ac51a8db Mon Sep 17 00:00:00 2001 From: aikin Date: Fri, 1 Apr 2022 18:00:45 +0800 Subject: [PATCH 1/6] plugin(request-id) algorithm support nanoid --- apisix/plugins/request-id.lua | 6 ++++- docs/en/latest/plugins/request-id.md | 2 +- docs/zh/latest/plugins/request-id.md | 2 +- rockspec/apisix-master-0.rockspec | 1 + t/plugin/request-id.t | 33 ++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/request-id.lua b/apisix/plugins/request-id.lua index 183374e8f73d..6f1ab7b0cc9e 100644 --- a/apisix/plugins/request-id.lua +++ b/apisix/plugins/request-id.lua @@ -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 @@ -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"} } } @@ -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 diff --git a/docs/en/latest/plugins/request-id.md b/docs/en/latest/plugins/request-id.md index dadf8ddf8674..4d8e58f8ae94 100644 --- a/docs/en/latest/plugins/request-id.md +++ b/docs/en/latest/plugins/request-id.md @@ -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 diff --git a/docs/zh/latest/plugins/request-id.md b/docs/zh/latest/plugins/request-id.md index 130f66f8337f..5482672ea224 100644 --- a/docs/zh/latest/plugins/request-id.md +++ b/docs/zh/latest/plugins/request-id.md @@ -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 生成算法 | ## 如何启用 diff --git a/rockspec/apisix-master-0.rockspec b/rockspec/apisix-master-0.rockspec index d1ce6652a314..dc1901072d69 100644 --- a/rockspec/apisix-master-0.rockspec +++ b/rockspec/apisix-master-0.rockspec @@ -76,6 +76,7 @@ dependencies = { "opentelemetry-lua = 0.1-3", "net-url = 0.9-1", "xml2lua = 1.5-2", + "lua-resty-nanoid = 0.0.1-2" } build = { diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index fb7343708331..793a056f2786 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -646,3 +646,36 @@ 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 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.status = code + end + ngx.say(body) + } + } +--- response_body +passed From cfd6a2f2de965631793fdb44d4be316be04a3e83 Mon Sep 17 00:00:00 2001 From: aikin Date: Sat, 2 Apr 2022 17:39:34 +0800 Subject: [PATCH 2/6] fix dependencies installation --- rockspec/apisix-master-0.rockspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rockspec/apisix-master-0.rockspec b/rockspec/apisix-master-0.rockspec index dc1901072d69..2b3c830a4e3e 100644 --- a/rockspec/apisix-master-0.rockspec +++ b/rockspec/apisix-master-0.rockspec @@ -76,7 +76,7 @@ dependencies = { "opentelemetry-lua = 0.1-3", "net-url = 0.9-1", "xml2lua = 1.5-2", - "lua-resty-nanoid = 0.0.1-2" + "nanoid = 0.1-1" } build = { From 6b3ccceacc65dea8e1b3c3c699b478cc1a592160 Mon Sep 17 00:00:00 2001 From: aikin Date: Sat, 2 Apr 2022 18:07:51 +0800 Subject: [PATCH 3/6] add test --- t/plugin/request-id.t | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index 793a056f2786..54bfa8b096ce 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -648,12 +648,14 @@ X-Request-ID: 123 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, [[{ @@ -675,7 +677,40 @@ X-Request-ID: 123 ngx.status = code end ngx.say(body) + 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 -passed +passed \ No newline at end of file From 1ef9b766766e850c64ca8d446af55f566f241fae Mon Sep 17 00:00:00 2001 From: aikin Date: Wed, 6 Apr 2022 16:13:28 +0800 Subject: [PATCH 4/6] fix test --- t/plugin/request-id.t | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index 54bfa8b096ce..5307d4d43e75 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -674,9 +674,8 @@ X-Request-ID: 123 }]] ) if code >= 300 then - ngx.status = code + ngx.say("algorithm nanoid is error") end - ngx.say(body) for i = 1, 180 do local th = assert(ngx.thread.spawn(function() local httpc = http.new() @@ -711,6 +710,6 @@ X-Request-ID: 123 ngx.say("true") } } ---- wait: 5 +--- wait: 5 --- response_body -passed \ No newline at end of file +true \ No newline at end of file From 27a6b4c98544e18e11ef632af52f0881e2b370a7 Mon Sep 17 00:00:00 2001 From: aikin Date: Wed, 6 Apr 2022 16:38:51 +0800 Subject: [PATCH 5/6] fix lint --- t/plugin/request-id.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index 5307d4d43e75..cd87f4de4c8a 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -712,4 +712,4 @@ X-Request-ID: 123 } --- wait: 5 --- response_body -true \ No newline at end of file +true From 8304f3a15203df89c7ecdedf4704a79333f3493a Mon Sep 17 00:00:00 2001 From: aikin Date: Thu, 7 Apr 2022 11:01:59 +0800 Subject: [PATCH 6/6] fix code style --- t/plugin/request-id.t | 1 + 1 file changed, 1 insertion(+) diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index cd87f4de4c8a..af083e28e7c7 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -648,6 +648,7 @@ X-Request-ID: 123 X-Request-ID: 123 + === TEST 20: add plugin with algorithm nanoid (default uuid) --- config location /t {