[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help request: keeping redis connection alive #11372

Closed
mayeter opened this issue Jun 25, 2024 · 5 comments
Closed

help request: keeping redis connection alive #11372

mayeter opened this issue Jun 25, 2024 · 5 comments

Comments

@mayeter
Copy link
mayeter commented Jun 25, 2024

Description

Hello, I've been working on the limit-req plugin with redis and I realized apisix creates a new connection for every single request, so I think maybe I can alter this behaviour and if I can keep a single connection, it could perform faster after getting rid of three-way handshake.

Maybe you tried it already and it is a bad idea but I really need to get higher numbers 🙏

I've played a little in apisix/plugins/limit-req/limit-req-redis.lua

function _M.new(plugin_name, conf, rate, burst)
    local red, err = redis.new(conf)
    if not red then
        return red, err
    end
    local self = {
        red = red,
        conf = conf,
        plugin_name = plugin_name,
        burst = burst * 1000,
        rate = rate * 1000,
    }

    return setmetatable(self, mt)
end


function _M.incoming(self, key, commit)
    local conf = self.conf
    local red = self.red
    if not red then
        return red, err
    end

    return util.incoming(self, red, key, commit)
end

So it creates a connection once and keeps it under "self". My problem here is that it somehow terminates this connection after first request and the rest throws this error:

2024/06/25 18:29:23 [error] 242482#242482: *3933 [lua] limit-req.lua:171: phase_func(): failed to limit req: closed, client: x.x.x.x, server: _, request: "GET /test1/x HTTP/2.0", host: "xyz.com"

I failed to find the trigger that closes connection. Could you guide me please?

Environment

  • APISIX version (run apisix version): 3.9
  • Operating system (run uname -a): Ubuntu 22.04
  • OpenResty / Nginx version (run openresty -V or nginx -V): openresty/1.21.4.1
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):
@hanqingwu
Copy link
Contributor

image
apisix/utils/redis.lua

@hanqingwu
Copy link
Contributor

need add red:set_keepalive(...)

@mayeter
Copy link
Author
mayeter commented Jun 30, 2024

Hello, thank you for support. I managed to get pooled my connections according to the documentation here however it still closes the connection and puts it in pool, then expect me to recall red:connect() function before using it, so it does tcp handshake anyway. I guess its the way resty library is written, so we could close this issue if you don't have any suggestion for me 🙏

@hanqingwu
Copy link
Contributor

like this test .
openresty/lua-resty-redis#70
I think reuse connect is works, use red:connect after red:set_keepalive

@mayeter
Copy link
Author
mayeter commented Jul 1, 2024

thank you 🙏 I managed to do so, now connection is created with first request and used many times.

@mayeter mayeter closed this as completed Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants