Connecting to Postgres in plugin :log() handler is blocked

Hello, we are evaluating Kong API Gateway and we are looking to build a plugin that can limit the number of concurrent requests for specific resources. The plugin works for success cases, but we hit a limitation for the error cases. For example, if a request is unable to reach the upstream endpoint, we noticed that the plugin’s response() handler is not invoked. The plugin’s log() handler is invoked. But inside the log() method, we are quite limited to what functionality we could use. We would like to connect to Postgres (or Redis) and decrement the counter of in-flight requests. The connections are blocked though.

Could anyone suggest a solution to this? What can a plugin implement to be invoked on requests’ completion, successful or not, and be able to connect to a data store at that point? Thank you!

Here’s a log stack trace showing the error we get while trying to connect to Postgres from inside the plugin’s log() method:

2021/04/12 13:32:28 [error] 93173#0: *715 [kong] handler.lua:146 [ia-throttling-byid] ---ThrottleHandlerById:log START while logging request, client: 127.0.0.1, server: kong, request: "GET /v0/hello/world HTTP/1.1", host: "127.0.0.1:8000"
2021/04/12 13:32:28 [error] 93173#0: *715 [kong] handler.lua:151 [ia-throttling-byid] ---ThrottleHandlerById:log identifier: fe16fca1-aadd-4f8c-b977-1b3fb647df92 while logging request, client: 127.0.0.1, server: kong, request: "GET /v0/hello/world HTTP/1.1", host: "127.0.0.1:8000"
2021/04/12 13:32:28 [error] 93173#0: *715 [kong] handler.lua:152 [ia-throttling-byid] ---ThrottleHandlerById:log tenant_id: 0 while logging request, client: 127.0.0.1, server: kong, request: "GET /v0/hello/world HTTP/1.1", host: "127.0.0.1:8000"
2021/04/12 13:32:28 [error] 93173#0: *715 failed to run log_by_lua*: /usr/local/share/lua/5.1/kong/globalpatches.lua:389: API disabled in the context of log_by_lua*
stack traceback:
	[C]: in function 'error'
	....8.2/1.17.8.2/openresty/lualib/resty/core/socket_tcp.lua:177: in function 'old_tcp'
	/usr/local/share/lua/5.1/kong/globalpatches.lua:389: in function 'tcp'
	/usr/local/share/lua/5.1/pgmoon/socket.lua:93: in function 'new'
	/usr/local/share/lua/5.1/pgmoon/init.lua:676: in function '__init'
	/usr/local/share/lua/5.1/pgmoon/init.lua:700: in function 'new'
	.../share/lua/5.1/kong/db/strategies/postgres/connector.lua:203: in function 'connect'
	.../share/lua/5.1/kong/db/strategies/postgres/connector.lua:528: in function 'execute'
	...local/share/lua/5.1/kong/db/strategies/postgres/init.lua:594: in function 'select'
	/usr/local/share/lua/5.1/kong/db/dao/init.lua:989: in function 'find'
	...ua/5.1/kong/plugins/ia-throttling-byid/policies/init.lua:143: in function 'usage'
	...hare/lua/5.1/kong/plugins/ia-throttling-byid/handler.lua:153: in function <...hare/lua/5.1/kong/plugins/ia-throttling-byid/handler.lua:145>
	/usr/local/share/lua/5.1/kong/init.lua:265: in function 'execute_plugins_iterator'
	/usr/local/share/lua/5.1/kong/init.lua:1286: in function 'log'
	log_by_lua(nginx-kong.conf:99):2: in main chunk while logging request, client: 127.0.0.1, server: kong, request: "GET /v0/hello/world HTTP/1.1", host: "127.0.0.1:8000"

© 2019 Kong Inc.    Terms  •  Privacy  •  FAQ