From 55da829019dbc3bb7d3f93d8f0316075512ab83c Mon Sep 17 00:00:00 2001
From: Pavel Berezhnoy
Date: Tue, 29 Mar 2016 13:22:41 +0300
Subject: [PATCH 1/2] Deffers implemented
---
deferrs.lua | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
create mode 100644 deferrs.lua
diff --git a/deferrs.lua b/deferrs.lua
new file mode 100644
index 0000000..b3303cd
--- /dev/null
+++ b/deferrs.lua
@@ -0,0 +1,66 @@
+local queue = require 'queue'
+local fiber = require 'fiber'
+
+local function show_error(str)
+ box.error(box.error.PROC_LUA, str)
+end
+
+if not queue then
+ show_error("Failed to load queue module")
+end
+
+if not queue.tube.deferrs then
+ queue.start()
+end
+
+box.once('deferrs_init', function ()
+ queue.create_tube('deferrs', 'fifottl')
+end)
+
+local function get_task_id(task)
+ return task[1]
+end
+
+local function get_task_data(task)
+ return task[3]
+end
+
+function deferr_put(uid, release_time, data)
+ local ok, ret = pcall(function(uid, release_time, data)
+ local delay = release_time - math.floor(fiber.time())
+ if delay < 0 then
+ show_error("Invalid release_time found for user " .. uid .. ", data == " .. data)
+ end
+ return queue.tube.deferrs:put({ uid, release_time, data }, { delay = delay })
+ end, uid, release_time, data)
+
+ if not ok then
+ show_error(str) -- unexpected error. Pass it to capron
+ end
+
+ return get_task_id(ret)
+end
+
+function deferr_delete(id)
+ local ok, ret = pcall(function (task_id)
+ return queue.tube.deferrs:delete(task_id)
+ end, id)
+
+ if not ok then
+ return nil
+ end
+
+ return get_task_data(ret) -- a tuple: { uid, release_time, data }
+end
+
+function deferr_peek(id)
+ local ok, ret = pcall(function (task_id)
+ return queue.tube.deferrs:peek(task_id) -- task state will not be changed
+ end, id)
+
+ if not ok then
+ return nil
+ end
+
+ return get_task_data(ret) -- a tuple: { uid, release_time, data }
+end
From 5ddd3346d352716aed499fe2006f1877f7874abe Mon Sep 17 00:00:00 2001
From: Pavel Berezhnoy
Date: Fri, 13 May 2016 15:48:35 +0300
Subject: [PATCH 2/2] deferrs: bug fix
---
deferrs.lua | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/deferrs.lua b/deferrs.lua
index b3303cd..a6bde8b 100644
--- a/deferrs.lua
+++ b/deferrs.lua
@@ -29,13 +29,13 @@ function deferr_put(uid, release_time, data)
local ok, ret = pcall(function(uid, release_time, data)
local delay = release_time - math.floor(fiber.time())
if delay < 0 then
- show_error("Invalid release_time found for user " .. uid .. ", data == " .. data)
+ show_error("Invalid release_time (" .. release_time .. ") found for user (delay < 0), uid == " .. uid .. ", data == " .. data)
end
return queue.tube.deferrs:put({ uid, release_time, data }, { delay = delay })
end, uid, release_time, data)
if not ok then
- show_error(str) -- unexpected error. Pass it to capron
+ show_error(ret) -- unexpected error. Pass it to capron
end
return get_task_id(ret)