ngx_lua_API 指令详解(一)ngx.timer.at 指令
語法: ok,err = ngx.timer.at(delay,callback,user_arg1,user_arg2 ...)
上下文: init_worker_by_lua *,set_by_lua *,rewrite_by_lua *,access_by_lua *,content_by_lua *,header_filter_by_lua *,body_filter_by_lua *,log_by_lua *,ngx.timer。*,balancer_by_lua *,ssl_certificate_by_lua *,ssl_session_fetch_by_lua *,ssl_session_store_by_lua *
描述:
創建一個帶有用戶回調函數的Nginx定時器以及可選的用戶參數。
第一個參數
delay指定定時器的延遲(以秒為單位)。可以指定小數秒0.001,這意味著1毫秒。0也可以指定延遲,在這種情況下,當當前處理程序產生執行時,定時器將立即過期。
第二個參數
callback可以是任何Lua函數,后面將在后面的“light thread”中調用延遲指定。用戶回調將自動Nginx的芯與參數調用premature, user_arg1,user_arg2,和等等,其中,所述premature 參數采用一個布爾值指示是否它是一個過早的計時器期滿或沒有,和user_arg1,user_arg2和等等,是那些(額外的)用戶調用ngx.timer.at 作為剩余參數時指定的參數。
當Nginx工作進程正在嘗試關閉時,會發生提前定時器到期,如在Nginx配置中由HUP信號觸發的重新加載或Nginx服務器關閉。當Nginx工作器試圖關閉時,不能再調用ngx.timer.at創建具有非零延遲的新定時器,在這種情況下ngx.timer.at將返回nil,還有一個描述錯誤的字符串,即“進程退出”。
從v0.9.3發行版開始,即使Nginx工作進程開始關閉,也允許創建零延遲定時器。
當定時器到期時,定時器回調中的用戶Lua代碼正在從創建定時器的原始請求完全分離的“輕線程”中運行。因此,與創建它們的請求(如子倉)具有相同生命周期的對象不能在原始請求和定時器用戶回調函數之間共享。
?一、只執行一次
nginx.conf
location /ngx_timer_at {default_type 'text/html';lua_code_cache off;content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/get_timer_at.lua;}get_timer_at.lua 文件
local delay = 5 local handler handler = function (premature,param)-- do some routine job in Lua just like a cron jobif premature thenreturnendngx.log(ngx.ERR, "param is : ", param) endlocal ok, err = ngx.timer.at(delay, handler,"Hello Tinywan")執行請求后:curl http://127.0.0.1/ngx_timer_at 執行 5 s 后打印以下內容在日志文件中
tail -f error.log 2017/05/04 23:24:38 [error] 95933#0: *433016 [lua] get_timer_at.lua:9: param is : Hello Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80?
二、不停的循環,和while 一樣的結果
local delay = 5 local handler handler = function (premature,param)-- do some routine job in Lua just like a cron jobif premature thenreturnendngx.log(ngx.ERR, "param is : ", param) ngx.timer.at(delay, handler,"again run... Tinywan") endlocal ok, err = ngx.timer.at(delay, handler,"Hello Tinywan")?
執行請求后:curl http://127.0.0.1/ngx_timer_at 執行 5 s 后打印以下內容在日志文件中
2017/05/04 23:34:53 [error] 96020#0: *437080 [lua] get_timer_at.lua:9: param is : Hello Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80 2017/05/04 23:34:58 [error] 96020#0: *437123 [lua] get_timer_at.lua:9: param is : again run... Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80 2017/05/04 23:35:03 [error] 96020#0: *437152 [lua] get_timer_at.lua:9: param is : again run... Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80 2017/05/04 23:35:08 [error] 96020#0: *437195 [lua] get_timer_at.lua:9: param is : again run... Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80 2017/05/04 23:35:13 [error] 96020#0: *437224 [lua] get_timer_at.lua:9: param is : again run... Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80?
?
<script?src="https://gist.github.com/Tinywan/8cff39afc1b512ca5bfa62a329e10506.js"></script> 通過?Lua?操作?Http?頭syntax:?headers = ngx.req.get_headers(max_headers?, raw?)
context:?set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*
Returns a Lua table holding all the current request headers.
local h = ngx.req.get_headers()for k, v in pairs(h) do ... endjson 和 lua table 轉換的細節
?
總結
以上是生活随笔為你收集整理的ngx_lua_API 指令详解(一)ngx.timer.at 指令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 杭州找Android工作的点点滴滴
- 下一篇: 《ES6标准入门》49~68Page 数