openresty的ngx.timer.at真是个强大的方法。

  • 例如某些函数不可以在一些NGINX的执行阶段使用时,可以ngx.timer.at API 创建一个零延迟的timer,在timer中去处理。
  • 遇到一些高延迟的函数,因为定时调用是在后台运行,并且他们的执行不会增加任何客户端的响应时长
local function save_message(premature)
ngx.sleep(5)
local file = assert(io.open('/tmp/test.log','a+'))
file:write(string.format("timer=====> %s %s\n", ngx.time(), "in timer"))
file:close()
end local file = assert(io.open('/tmp/test.log','a+'))
file:write(string.format("out tomer 1=====> %s %s\n", ngx.time(), "out timer 1"))
file:close()
ngx.sleep(5)
local file = assert(io.open('/tmp/test.log','a+'))
file:write(string.format("out tomer 2=====> %s %s\n", ngx.time(), "out timer 2"))
file:close()
--ngx.sleep(5)
local ok, err = ngx.timer.at(0, save_message)
if not ok then
ngx.log(ngx.ERR, "[blm-metric] failed to create timer: ", err)
end ngx.say("success")

结果将在5秒后返回,查看日志,每隔5秒分别打印出三次的结果,

openresty的ngx.timer.at的更多相关文章

  1. openresty跑定时任务配置、ngx.timer.every接口使用

    openresty的定时任务是要跟worker绑定的.如果不绑定特定的worker,那么所有启动的woker都会去执行定时任务. 一般情况下默认绑定worker_id=0的,这样在nginx整个进程里 ...

  2. ngx_lua_API 指令详解(一)ngx.timer.at 指令

    语法: ok,err = ngx.timer.at(delay,callback,user_arg1,user_arg2 ...) 上下文: init_worker_by_lua *,set_by_l ...

  3. OpenResty之ngx.var.VARIABLE

    1. ngx.var.VARIABLE syntax: ngx.var.VAR_NAME context: set_by_lua*, rewrite_by_lua*, access_by_lua*, ...

  4. OpenResty之ngx.ssl

    翻译自: ngx.ssl - Lua API for controlling NGINX downstream SSL handshakes 1. 概要 # 注意:如果你使用的是 OpenResty ...

  5. OpenResty之ngx.shared.DICT

    参考链接: resty.core.shdict ngx_shared.DICT 源码正文: dict.lua 部分源码如下: local ffi = require 'ffi' local base ...

  6. Openresty 与 Tengine

    Openresty 与 Tengine Openresty和Tengine基于 Nginx 的两个衍生版本,某种意义上他们都和淘宝有关系,前者是前淘宝工程师agentzh主导开发的,后者是淘宝的一个开 ...

  7. ngx.ctx

    https://github.com/openresty/lua-nginx-module#ngxctx 要点 生命周期和请求一致 每个请求的ngx.ctx是相互独立的,包括ngx.location. ...

  8. openresty + lua 4、openresty kafka

    kafka 官网: https://kafka.apache.org/quickstart zookeeper 官网:https://zookeeper.apache.org/ kafka 运行需要 ...

  9. ngx_lua_API 指令详解(二)ngx.re.match/find/gmatch/sub/gsub指令集合

    1.先来个官方的ngx.re.match location /ngx_re_match { default_type text/html; content_by_lua_block { local m ...

随机推荐

  1. jquery遍历之后代

    向下遍历dom树的jquery方法 children()方法返回被选元素的所有直接子元素,只会对向下一级对dom树进行遍历. 例子 代码: $(document).ready(function(){ ...

  2. 调整文本输入框placeholder的颜色等样式

    input::-webkit-input-placeholder{     color: white !important;}input:-moz-placeholder{    color: whi ...

  3. 修复ios上第三方输入法弹出时输入键盘盖住网页没有进行相应滚动从而盖住表单输入框的问题

    fixIME(); function fixIME(){ scroll_y = 100;  // 如果键盘弹起后 网页window对象的卷起小于此值,说明没有自动卷起 单位:px timer = 50 ...

  4. jquery和ajax,json写法的说明

    一: 在ajax中,如果没有用jquery,则如xmlHttpRequest.open("POST", "AjaxServlet", true); (1)如果用 ...

  5. [BZOJ1984]月下“毛景树”解题报告|树链剖分

    Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里.爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的“毛景树” ...

  6. 培训补坑(day8:树上倍增+树链剖分)

    补坑补坑.. 其实挺不理解孙爷为什么把这两个东西放在一起讲..当时我学这一块数据结构都学了一周左右吧(超虚的) 也许孙爷以为我们是省队集训班... 好吧,虽然如此,我还是会认真写博客(保证初学者不会出 ...

  7. Linux服务器中毒事件(libudev.so)

    今天机房管理人员反馈公司的某台服务器在防火墙上的连接数超限,登陆服务器时发现非常卡顿,远程登录后查看,CPU持续100%,且有一长度为10的随机字符串进程,kill掉,会重新生成另外长度为10的字符串 ...

  8. python3 面向对象补充

    f = People('egon',18,'male') 非函数hasattr # hasattr(f,'name')getattr # getattr(f,'name')setattr # seta ...

  9. JMeter之定时器的作用域

    定时器的作用域 1.定时器是在每个sampler(采样器)之前执行的,而不是之后(无论定时器位置在sampler之前还是下面): 2.当执行一个sampler之前时,所有当前作用域内的定时器都会被执行 ...

  10. string与double的互相转换

    #include <iostream> #include <string> #include <sstream> string DoubleToString(dou ...