@@ -32,6 +32,7 @@ <h2>Contents</h2>
3232< li > < a href ="#Status "> Status </ a > </ li >
3333< li > < a href ="#Synopsis "> Synopsis </ a > </ li >
3434< li > < a href ="#Description "> Description </ a > </ li >
35+ < li > < a href ="#Performance_and_optimizations "> Performance and optimizations </ a > </ li >
3536< li > < a href ="#History "> History </ a > </ li >
3637< li > < a href ="#Copyright_and_License "> Copyright and License </ a > </ li >
3738</ ul >
@@ -53,7 +54,7 @@ <h2>Modules</h2>
5354
5455< h1 > lua-resty-timer</ h1 >
5556
56- < p > < a href ="https://travis-ci.org /Kong/lua-resty-timer/branches "> < img src ="https://travis-ci.org /Kong/lua-resty-timer.svg?branch=master " alt ="Build Status "/> </ a > </ p >
57+ < p > < a href ="https://travis-ci.com /Kong/lua-resty-timer/branches "> < img src ="https://travis-ci.com /Kong/lua-resty-timer.svg?branch=master " alt ="Build Status "/> </ a > </ p >
5758
5859< p > Extended timers for OpenResty. Provided recurring, cancellable, node-wide timers,
5960beyond what the basic OpenResty timers do.</ p >
@@ -86,6 +87,7 @@ <h2>Synopsis</h2>
8687 shm_name = < span class ="string "> "timer_shm"</ span > , < span class ="comment "> -- shm to use for node-wide timers
8788</ span > key_name = < span class ="string "> "my_key"</ span > , < span class ="comment "> -- key-name to use for node-wide timers
8889</ span > sub_interval = < span class ="number "> 0.1</ span > , < span class ="comment "> -- max cross worker extra delay
90+ </ span > max_use = < span class ="number "> 1000</ span > , < span class ="comment "> -- maximum re-use of timer context
8991</ span > }
9092
9193 < span class ="keyword "> local</ span > object
@@ -118,7 +120,8 @@ <h2>Description</h2>
118120project.</ p >
119121
120122< ul >
121- < li > < p > recurring timers (supported by OR as well through < code > ngx.timer.every</ code > )</ p > </ li >
123+ < li > < p > recurring timers (supported by OR as well through < code > ngx.timer.every</ code > , but this
124+ implementation will not run overlapping timers)</ p > </ li >
122125 < li > < p > immediate first run for recurring timers</ p > </ li >
123126 < li > < p > cancellable timers</ p > </ li >
124127 < li > < p > cancel callback, called when the timer is cancelled</ p > </ li >
@@ -132,6 +135,37 @@ <h2>Description</h2>
132135< p > See the < a href ="https://kong.github.io/lua-resty-timer/topics/README.md.html "> online LDoc documentation</ a >
133136for the complete API.</ p >
134137
138+ < p > < a name ="Performance_and_optimizations "> </ a > </ p >
139+ < h2 > Performance and optimizations</ h2 >
140+
141+ < p > This timer implementation is based on "sleeping on a timer-context". This means
142+ that a single timer is created, and in between recurring invocations < code > ngx.sleep</ code >
143+ is called as a delay to the next invocation. This as opposed to creating a new
144+ Nginx timer for each invocation. This is configurable however.</ p >
145+
146+ < p > Creating a new context is a rather expensive operation. Hence we keep the context
147+ alive and just sleep without the need to recreate it. The downside is that there
148+ is the possibility of a memory leak. Since a timer is implemented in OR as a
149+ request and requests are short-lived, some memory is not released until after the
150+ context is destroyed.</ p >
151+
152+ < p > The setting < code > max_use</ code > controls the timer behaviour. The default value is < code > 1000</ code > ,
153+ which means that after each < code > 1000</ code > invocations the timer context is destroyed
154+ and a new one is generated (this happens transparent to the user).</ p >
155+
156+ < p > Optimizing this setting (very opinionated/arbitrary!):</ p >
157+
158+ < ul >
159+ < li > < p > if the timer interval is more than < code > 60</ code > seconds, then keeping the context
160+ around in idle state for that period is probably more expensive resource wise
161+ than having to recreate the context. So use < code > max_use == 1</ code > to drop the
162+ context after each invocation.</ p > </ li >
163+ < li > < p > if the timer interval is less than < code > 5</ code > seconds then reusing the context makes
164+ sense. Assume recycling to be done once per minute, or for very high
165+ frequency timers (and hence higher risk of memory leak), more than once per
166+ minute.</ p > </ li >
167+ </ ul >
168+
135169< p > < a name ="History "> </ a > </ p >
136170< h2 > History</ h2 >
137171
@@ -150,6 +184,25 @@ <h3>Releasing new versions:</h3>
150184 < li > upload rock to luarocks: < code > luarocks upload rockspecs/[name] --api-key=abc</ code > </ li >
151185</ ul >
152186
187+ < h3 > unreleased</ h3 >
188+
189+ < ul >
190+ < li > Feat: provide a stacktrace upon errors in the timer callback</ li >
191+ < li > Feat: add a < code > max_use</ code > option. This ensures timer-contexts are recycled to
192+
193+ < pre >
194+ prevent memory leaks.
195+ </ pre >
196+ </ li >
197+ < li > Feat: adds a new function < a href ="../index.html#sleep "> sleep</ a > similar to < code > ngx.sleep</ code > except that it is
198+
199+ < pre >
200+ interrupted on worker exit.
201+ </ pre >
202+ </ li >
203+ < li > Fix: now accounts for execution time of the handler, when rescheduling.</ li >
204+ </ ul >
205+
153206< h3 > 1.1.0 (6-Nov-2020)</ h3 >
154207
155208< ul >
@@ -213,7 +266,7 @@ <h2>Copyright and License</h2>
213266</ div > <!-- id="main" -->
214267< div id ="about ">
215268< i > generated by < a href ="http://github.com/stevedonovan/LDoc "> LDoc 1.4.6</ a > </ i >
216- < i style ="float:right; "> Last updated 2020-11-07 00:01:15 </ i >
269+ < i style ="float:right; "> Last updated 2021-08-12 14:42:50 </ i >
217270</ div > <!-- id="about" -->
218271</ div > <!-- id="container" -->
219272</ body >
0 commit comments