为什么我不能在init_by_lua阶段使用ngx.socket.tcp()?

huangapple go评论64阅读模式
英文:

Why can't i use ngx.socket.tcp() in init_by_lua phase?

问题

我需要从Unix套接字(postgresql)中读取API定义,并将其设置为共享字典中的变量。是否有禁止这样做的原因?

local pg = pgmoon.new({
    socket_type = "nginx",
    host = table.concat({"unix:", config.socket}),
    database = config.database,
    user = config.user
})

assert(pg:connect())
local res = assert(pg:query("select info from testy limit 1"))
return res

出现以下错误:

nginx: [error] init_by_lua_file error: /usr/local/share/lua/5.1/pgmoon/socket.lua:99: no request found
stack traceback:
        [C]: in function 'tcp'
        /usr/local/share/lua/5.1/pgmoon/socket.lua:99: in function 'new'
        /usr/local/share/lua/5.1/pgmoon/init.lua:1093: in function '__init'
        /usr/local/share/lua/5.1/pgmoon/init.lua:1101: in function 'new'
        ./lua/store.lua:7: in function 'read'
        .../peter/Documents/resty-engine/src/lua/lifecycle/init.lua:8: in function 'reloadAPI'
        .../peter/Documents/resty-engine/src/lua/lifecycle/init.lua:16: in main chunk

如果只插入以下内容,将出现相同的问题:

local socket = ngx.socket.tcp()
英文:

I need to read api definition from unix socket(postgresql) and set it as a variable in shared dict. Is there a reason why this is forbidden?

    local pg = pgmoon.new({
        socket_type = "nginx",
        host = table.concat({"unix:", config.socket}),
        database = config.database,
        user = config.user
    })

    assert(pg:connect())
    local res = assert(pg:query("select info from testy limit 1"))
    return res

crashes with

nginx: [error] init_by_lua_file error: /usr/local/share/lua/5.1/pgmoon/socket.lua:99: no request found
stack traceback:
        [C]: in function 'tcp'
        /usr/local/share/lua/5.1/pgmoon/socket.lua:99: in function 'new'
        /usr/local/share/lua/5.1/pgmoon/init.lua:1093: in function '__init'
        /usr/local/share/lua/5.1/pgmoon/init.lua:1101: in function 'new'
        ./lua/store.lua:7: in function 'read'
        .../peter/Documents/resty-engine/src/lua/lifecycle/init.lua:8: in function 'reloadAPI'
        .../peter/Documents/resty-engine/src/lua/lifecycle/init.lua:16: in main chunk

same problem will occur if you just insert

local socket = ngx.socket.tcp()

into init_by_lua_block or file

答案1

得分: 3

根据文档
> 目前,在init_by_lua*init_worker_by_lua*指令上下文中,cosocket已被禁用。

英文:

According to the documentation,

> The cosockets are currently [...] disabled in the init_by_lua* and init_worker_by_lua* directive contexts

huangapple
  • 本文由 发表于 2023年5月24日 20:11:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定