英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论