API disabled in the context of log_by_lua, Unable to perform database action as well tcp connection in log(), header_filter(), body_filter() modules in my custom plugin

Hi,
I am building a custom plugin where in I am facing an issue during the database operation like insert, delete, select etc., as well as in tcp connection when it is invoked through :log(), header_filter, body_filter(). Below is the the error message.

[error] 6634#0: 67 failed to run log_by_lua: /usr/local/share/lua/5.1/kong/globalpatches.lua:321: API disabled in the context of log_by_lua*
stack traceback:
[C]: in function 'old_tcp’
/usr/local/share/lua/5.1/kong/globalpatches.lua:321: in function ‘tcp’
/usr/local/share/lua/5.1/kong/plugins/ethercis/access.lua:54: in function ‘login_ethercis’
/usr/local/share/lua/5.1/kong/plugins/ethercis/handler.lua:22: in function ‘log’
/usr/local/share/lua/5.1/kong/init.lua:529: in function ‘log’
log_by_lua(nginx-kong.conf:124):2: in function <log_by_lua(nginx-kong.conf:124):1> while logging request, client: 127.0.0.1, server: kong, req

anyone please give some alternative implementation to perform database action only in log() or header_filter() or body_filter modules in a plugin.

Thank you in advance.

Hi, per https://github.com/openresty/lua-nginx-module/blob/master/README.markdown#cosockets-not-available-everywhere, it’s not possible to do what you want.

The only workaround is to use ngx.timer.at , but you won’t be able to wait for the operation to complete. It’s kinda fire and forget mechanism

2 Likes

Thank you for the information @rucciva

1 Like