dickens7 commented on a change in pull request #4559:
URL: https://github.com/apache/apisix/pull/4559#discussion_r667356228
##########
File path: apisix/plugins/request-id.lua
##########
@@ -14,36 +14,163 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-local core = require("apisix.core")
-local plugin_name = "request-id"
-local ngx = ngx
-local uuid = require("resty.jit-uuid")
+
+local ngx = ngx
+local bit = require("bit")
+local core = require("apisix.core")
+local snowflake = require("snowflake")
+local uuid = require("resty.jit-uuid")
+local process = require("ngx.process")
+local tostring = tostring
+local math_pow = math.pow
+
+local plugin_name = "request-id"
+
+local worker_number = nil
+local snowflake_init = nil
+
+local attr = nil
local schema = {
type = "object",
properties = {
header_name = {type = "string", default = "X-Request-Id"},
- include_in_response = {type = "boolean", default = true}
+ include_in_response = {type = "boolean", default = true},
+ algorithm = {type = "string", enum = {"uuid", "snowflake"}, default =
"uuid"}
}
}
+local attr_schema = {
+ type = "object",
+ properties = {
+ snowflake = {
+ type = "object",
+ properties = {
+ enable = {type = "boolean"},
+ snowflake_epoc = {type = "integer", minimum = 1, default =
1609459200000},
+ node_id_bits = {type = "integer", minimum = 1, default = 5},
+ sequence_bits = {type = "integer", minimum = 1, default = 10},
+ datacenter_id_bits = {type = "integer", minimum = 1, default =
5},
+ worker_number_ttl = {type = "integer", minimum = 1, default =
30},
+ worker_number_interval = {type = "integer", minimum = 1,
default = 10}
+ }
+ }
+ }
+}
local _M = {
version = 0.1,
priority = 11010,
name = plugin_name,
- schema = schema,
+ schema = schema
}
-
function _M.check_schema(conf)
return core.schema.check(schema, conf)
end
+local function gen_worker_number(max_number)
+ if worker_number == nil then
+ local etcd_cli, prefix = core.etcd.new()
+ local res, _ = etcd_cli:grant(attr.snowflake.worker_number_ttl)
+
+ local prefix = prefix .. "/plugins/request-id/snowflake/"
+ local uuid = uuid.generate_v4()
+ local id = 1
+ while (id <= max_number) do
+ ::continue::
+ local _, err1 = etcd_cli:setnx(prefix .. tostring(id), uuid)
+ local res2, err2 = etcd_cli:get(prefix .. tostring(id))
+
+ if err1 or err2 or res2.body.kvs[1].value ~= uuid then
+ core.log.notice("worker_number " .. id .. " is not available")
+ id = id + 1
+ else
+ worker_number = id
+
+ local _, err3 =
+ etcd_cli:set(
+ prefix .. tostring(id),
+ uuid,
+ {
+ prev_kv = true,
+ lease = res.body.ID
+ }
+ )
+
+ if err3 then
+ id = id + 1
+ etcd_cli:delete(prefix .. tostring(id))
+ core.log.error("set worker_number " .. id .. " lease
error: " .. err3)
+ goto continue
+ end
+
+ local handler = function(premature, etcd_cli, lease_id)
+ local _, err4 = etcd_cli:keepalive(lease_id)
+ if err4 then
+ snowflake_init = nil
+ worker_number = nil
+ core.log.error("snowflake worker_number lease faild.")
+ end
+ core.log.info("snowflake worker_number lease success.")
+ end
+ ngx.timer.every(attr.snowflake.worker_number_interval,
Review comment:
> Hi @dickens7 do you mind also add documentations of this feature
ok
--
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]