From 82d07646eda1f7c08736df5d6e0203153fabadee Mon Sep 17 00:00:00 2001 From: aikin Date: Thu, 7 Apr 2022 14:31:00 +0800 Subject: [PATCH] feat(request-id): add algorithm support nanoid (#6779) --- 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 | 68 ++++++++++++++++++++++++++++ 5 files changed, 76 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 aeb161e7b585..d0cbcc12e50d 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", + "nanoid = 0.1-1" } build = { diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index fb7343708331..af083e28e7c7 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -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