Skip to content

Commit

Permalink
refactor node id to make it unique
Browse files Browse the repository at this point in the history
  • Loading branch information
sumory committed Aug 5, 2017
1 parent 64ca5ff commit e14d833
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### v0.3.3 2017.08.05

- 使用严格的路由节点id策略,避免潜在冲突


### v0.3.2 2017.06.10

- 关于内置session插件的更改
Expand Down
2 changes: 1 addition & 1 deletion dist.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = lor
abstract = A fast and minimalist web framework based on OpenResty.
version = 0.3.2
version = 0.3.3
author = Sumory Wu (@sumory)
is_original = yes
license = mit
Expand Down
16 changes: 15 additions & 1 deletion lib/lor/lib/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ local ActionHolder = require("lor.lib.holder").ActionHolder
local handler_error_tip = "handler must be `function` that matches `function(req, res, next) ... end`"
local middlware_error_tip = "middlware must be `function` that matches `function(req, res, next) ... end`"
local error_middlware_error_tip = "error middlware must be `function` that matches `function(err, req, res, next) ... end`"
local node_count = 0

local function gen_node_id()
local prefix = "node-"
local worker_part = "dw"
if ngx and ngx.worker then
worker_part = ngx.worker.id()
end
node_count = node_count + 1 -- simply count for lua vm level
local unique_part = node_count
local random_part = utils.random()
node_id = prefix .. worker_part .. "-" .. unique_part .. "-" .. random_part
return node_id
end

local function check_method(method)
if not method then return false end
Expand All @@ -33,7 +47,7 @@ function Node:new(root)
end

local instance = {
id = "node-" .. utils.random(),
id = gen_node_id(),
is_root = is_root,
name = "",
allow = "",
Expand Down
2 changes: 1 addition & 1 deletion lib/lor/version.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
return "0.3.2"
return "0.3.3"
38 changes: 38 additions & 0 deletions spec/cases/node_id_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
before_each(function()
Trie = require("lor.lib.trie")
t = Trie:new()
end)

after_each(function()
t = nil
end)

function table_size(t)
local res = 0
if t then
for _ in pairs(t) do
res = res + 1
end
end
return res
end

describe("node is should be unique", function()
it("test case 1", function()
local count = 100
local nodes = {}
for i=1,count,1 do
local node = t:add_node(tostring(i))
table.insert(nodes, node)
end

assert.is.equals(count, #nodes)
assert.is.equals(count, table_size(nodes))

local node_map = {}
for i=1,count,1 do
node_map[nodes[i].id] = true
end
assert.is.equals(count, table_size(node_map))
end)
end)

0 comments on commit e14d833

Please sign in to comment.