How can I increase the connect timeout to the upstream service using the Serverless (Pre-Function) Plugin?
Increasing the connect timeout using the Serverless Functions plugin
How can I increase the connect timeout using the Serverless Functions Plugin?
Override the upstream connect_timeout (in milliseconds) with a Lua snippet — e.g. ngx.ctx.balancer_data.connect_timeout = 100000 — set via config.access on the pre-function plugin (config.functions is not a valid field). This timeout only surfaces visibly against an upstream that’s slow or unresponsive at the TCP handshake stage, not against one that is merely slow to respond.
Steps
Using the Serverless (Pre-function) Plugin in Kong, you can override the connect timeout to the upstream service. Below is an example on how this can be achieved:
-
Create a file called
function.lua. -
Content of
function.lua:ngx.ctx.balancer_data.connect_timeout = 100000Note:
connect_timeoutis in milliseconds, so100000is 100 seconds. Setting it to a plain100(100 milliseconds) would decrease the timeout well below the 60000ms (60s) default, causing Kong to fail faster, not slower — the opposite of what this article is demonstrating. -
Create a Service (upstream service here will not respond for 10 seconds):
curl -i -X POST --header 'kong-admin-token: <TOKEN>' --url http://localhost:8001/services/ \ --data 'name=test' \ --data 'url=http://httpbin.org/delay/10' -
Create a Route:
curl -i -X POST --header 'kong-admin-token: <TOKEN>' --url http://localhost:8001/services/test/routes \ --data 'name=testing-route' \ --data 'paths[]=/without-timeout' -
Apply the Serverless (Pre-Function) Plugin.
config.functionsis not a valid field on the pre-function/post-function plugins. You must use the phase-specific field instead, in this caseconfig.access, which takes an array of Lua code snippets to run in the access phase:curl -i -X POST --header 'kong-admin-token: <TOKEN>' --url http://localhost:8001/routes/testing-route/plugins \ -F "name=pre-function" \ -F "config.access[1]=@function.lua" -
Test.
curl -i GET http://localhost:8000/without-timeoutThis request succeeds after Kong’s default 60-second response wait, since
httpbin.org/delay/10’s 10-second delay happens after the TCP connection is already established — the delay is server-side response latency, governed byread_timeout/write_timeout, not byconnect_timeout. In other words, this particular example does not actually exercise the raisedconnect_timeoutvalue at all, because connecting tohttpbin.orgitself is fast regardless of the configured value.connect_timeoutonly matters (and only fails/succeeds visibly) against an upstream that is slow or unresponsive at the TCP handshake stage — for example, an unroutable/firewalled IP that never completes a TCP handshake. Live-reproduced: pointing a Service at such an address with the default 60000msconnect_timeoutmakes Kong hang for the full default duration before failing; overridingngx.ctx.balancer_data.connect_timeoutto a small value (e.g.100for 100ms) inconfig.accessmakes Kong instead fail fast with a504 Gateway Timeoutin a few hundred milliseconds — confirming the override mechanism itself works correctly, even though thehttpbin.org/delay/10example above doesn’t demonstrate it.