Skip to content

- fixed cannot run require('resty.mongol') under init_by_lua* because ng... #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/resty/mongol/ll.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ local num_to_le_uint = function ( n , bytes )
for i=1 , bytes do
b [ i ] , n = n % 2^8 , floor ( n / 2^8 )
end
assert ( n == 0 )
return strchar ( unpack ( b ) )
end
local num_to_le_int = function ( n , bytes )
Expand All @@ -57,7 +56,6 @@ local num_to_be_uint = function ( n , bytes )
for i=bytes , 1 , -1 do
b [ i ] , n = n % 2^8 , floor ( n / 2^8 )
end
assert ( n == 0 )
return strchar ( unpack ( b ) )
end

Expand Down
23 changes: 15 additions & 8 deletions lib/resty/mongol/object_id.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,27 @@ local object_id_mt = {
}

local machineid
if hasposix then
machineid = posix.uname("%n")
else
machineid = assert(io.popen("uname -n")):read("*l")
local function get_os_machineid()
if hasposix then
machineid = posix.uname("%n")
else
machineid = assert(io.popen("uname -n")):read("*l")
end
machineid = ngx.md5_bin(machineid):sub(1, 3)
return machineid
end
machineid = ngx.md5_bin(machineid):sub(1, 3)

local pid = num_to_le_uint(ngx.var.pid, 2)
local pid
local function get_os_pid()
pid = num_to_le_uint(ngx.var.pid, 2)
return pid
end

local inc = 0
local function generate_id ( )
local function generate_id()
inc = inc + 1
-- "A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON"
return num_to_be_uint ( os.time ( ) , 4 ) .. machineid .. pid .. num_to_be_uint ( inc , 3 )
return num_to_be_uint(os.time(), 4) .. (machineid or get_os_machineid()) .. (pid or get_os_pid()) .. num_to_be_uint(inc, 3)
end

local function new_object_id(str)
Expand Down