Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

变量作用范围里关于timer的一处错误 #348

Open
RocFang opened this issue Jun 23, 2021 · 1 comment
Open

变量作用范围里关于timer的一处错误 #348

RocFang opened this issue Jun 23, 2021 · 1 comment

Comments

@RocFang
Copy link

RocFang commented Jun 23, 2021

https://github.com/moonbingbing/openresty-best-practices/blob/master/ngx_lua/lua-variable-scope.md

在这一节里,提到:

init_worker_by_lua_block {
    local delay = 5
    local handler
    handler = function()
        counter = counter or 0
        counter = counter + 1
        ngx.log(ngx.ERR, counter)
        local ok, err = ngx.timer.at(delay, handler)
        if not ok then
            ngx.log(ngx.ERR, "failed to create the timer: ", err)
            return
        end
    end
    local ok, err = ngx.timer.at(delay, handler)
    if not ok then
        ngx.log(ngx.ERR, "failed to create the timer: ", err)
        return
    end
}

每次 counter 的输出都是 1。

然后实际的情况是,确实每次 timer 执行都在累加,而并没有重新定义。应该需要更新一下文档。

@membphis

@membphis
Copy link
Collaborator

欢迎 PR ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants