Annotate

util/timer.lua @ 7567:495de404a8ae

ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck] Functions roster(), roster_pending(), roster_group(), private_storage() and offline_msg() have argument named "host", which used to shadow upvalue of this variable before this change. Instead of renaming this argument, let's rename the variable to match what the script says in usage: Usage: ejabberdsql2prosody.lua filename.txt hostname
author Anton Shestakov <av6@dwimlabs.net>
date Fri, 12 Aug 2016 13:44:47 +0800
parent 6777:5de6b93d0190
child 6791:e813e8cf6046
child 7988:dc758422d896
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4871
diff changeset
4 --
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
4808
07d0a3a75c8a net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
Matthew Wild <mwild1@gmail.com>
parents: 4751
diff changeset
9 local server = require "net.server";
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
10 local math_min = math.min
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
11 local math_huge = math.huge
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
12 local get_time = require "socket".gettime;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local t_insert = table.insert;
4751
0c7ae4bfc835 util.timer: Remove unused function imports
Matthew Wild <mwild1@gmail.com>
parents: 4413
diff changeset
14 local pairs = pairs;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local type = type;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local new_data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
20 local _ENV = nil;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
22 local _add_task;
4808
07d0a3a75c8a net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
Matthew Wild <mwild1@gmail.com>
parents: 4751
diff changeset
23 if not server.event then
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
24 function _add_task(delay, callback)
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
25 local current_time = get_time();
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
26 delay = delay + current_time;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
27 if delay >= current_time then
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
28 t_insert(new_data, {delay, callback});
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
29 else
4871
b2d177f2febc util.timer: Always pass the current time to timer callbacks.
Waqas Hussain <waqas20@gmail.com>
parents: 4812
diff changeset
30 local r = callback(current_time);
4385
c94167139f27 util.timer: Fix corner case of timer not repeating if it returns <= 0
Matthew Wild <mwild1@gmail.com>
parents: 3683
diff changeset
31 if r and type(r) == "number" then
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
32 return _add_task(r, callback);
4385
c94167139f27 util.timer: Fix corner case of timer not repeating if it returns <= 0
Matthew Wild <mwild1@gmail.com>
parents: 3683
diff changeset
33 end
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
34 end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
35 end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
36
4808
07d0a3a75c8a net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
Matthew Wild <mwild1@gmail.com>
parents: 4751
diff changeset
37 server._addtimer(function()
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
38 local current_time = get_time();
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
39 if #new_data > 0 then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
40 for _, d in pairs(new_data) do
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
41 t_insert(data, d);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
42 end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
43 new_data = {};
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
44 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4871
diff changeset
45
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
46 local next_time = math_huge;
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
47 for i, d in pairs(data) do
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
48 local t, callback = d[1], d[2];
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
49 if t <= current_time then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
50 data[i] = nil;
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
51 local r = callback(current_time);
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
52 if type(r) == "number" then
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
53 _add_task(r, callback);
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
54 next_time = math_min(next_time, r);
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
55 end
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
56 else
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
57 next_time = math_min(next_time, t - current_time);
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
58 end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
59 end
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
60 return next_time;
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
61 end);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
62 else
4812
5bcdc384e485 util.timer: Remove unnecessary require calls, fixes a traceback (thanks nulani)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
63 local event = server.event;
5bcdc384e485 util.timer: Remove unnecessary require calls, fixes a traceback (thanks nulani)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
64 local event_base = server.event_base;
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
65 local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
4808
07d0a3a75c8a net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
Matthew Wild <mwild1@gmail.com>
parents: 4751
diff changeset
66
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
67 function _add_task(delay, callback)
2964
49b5c87d2fa0 util.timer: When using libevent hold onto the event handle to stop it being collected (and the timer stopping). Fixes BOSH ghosts, thanks Flo, niekie, waqas.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
68 local event_handle;
49b5c87d2fa0 util.timer: When using libevent hold onto the event handle to stop it being collected (and the timer stopping). Fixes BOSH ghosts, thanks Flo, niekie, waqas.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
69 event_handle = event_base:addevent(nil, 0, function ()
4871
b2d177f2febc util.timer: Always pass the current time to timer callbacks.
Waqas Hussain <waqas20@gmail.com>
parents: 4812
diff changeset
70 local ret = callback(get_time());
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
71 if ret then
2366
c3a364342cb4 util.timer: Use luaevent's built-in method of repeating an event (fixes a weird crash)
Matthew Wild <mwild1@gmail.com>
parents: 2098
diff changeset
72 return 0, ret;
2964
49b5c87d2fa0 util.timer: When using libevent hold onto the event handle to stop it being collected (and the timer stopping). Fixes BOSH ghosts, thanks Flo, niekie, waqas.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
73 elseif event_handle then
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
74 return EVENT_LEAVE;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
75 end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
76 end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
77 , delay);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
78 end
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
81 return {
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
82 add_task = _add_task;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
83 };