Skip to content

Feat/callback on async push messages recieved on socket in the pool#2477

Open
deem0n wants to merge 2 commits intoopenresty:masterfrom
luxms:feat/callback-idle-conn
Open

Feat/callback on async push messages recieved on socket in the pool#2477
deem0n wants to merge 2 commits intoopenresty:masterfrom
luxms:feat/callback-idle-conn

Conversation

@deem0n
Copy link

@deem0n deem0n commented Mar 6, 2026

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.

Current implementation forcefully closes opened connection in the pool in case such connection receives "unexpected" incoming packet from the over side.

There are some use cases when we need to process such messages and keep socket open. For example, NATS (https://nats.io) protocol has health heartbeat (ping/pong) requirement to keep peers connected. If openresty will open connection to the NATS server and will not reply on async PING message, NATS will close unhealthy client.

Also we may want to subscribe to NATS, Redis and other pub/sub servers to receive async incoming messages.

This patch allows such processing with new config option on_push where we need to provide callback function. Here is working example with callback implementation:

    local sock = ngx.socket.tcp()
    sock:settimeout(3000)

    local ok, err = sock:connect("nats", 4222, {
        on_push = function(data)
            if data == "PING\r\n" then
                return "PONG\r\n", true
            end
            return nil, false
        end
    })

This patch is fully backward compatible with old behavior, so if you do not provide on_push handler to the conect() function then connection will be closed on unexpected incoming packets, as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants