Skip to content

Closed socket error #28

@subnetmarco

Description

@subnetmarco

I'm trying to understand what is the best practice for handling Redis connections.

The doc suggests to avoid having a global connection to the Redis server in order to avoid any concurrency problems. This means opening a new connection to Redis for every incoming connection to Nginx, isn't this very inefficient?

Also, I'm trying to reuse the same connection in both access_by_lua and log_by_lua, but I get a attempt to send data on a closed socket error in the log_by_lua timer callback.

init_by_lua:

redis = require "resty.redis"

access_by_lua:

ngx.ctx.redis = redis:new()
local ok, err = ngx.ctx.redis:connect("127.0.0.1", 6379)
if not ok then
  ngx.log(ngx.ERR, "Cant connect to Redis")
end

ngx.ctx.redis:incr("incoming_requests")

log_by_lua:

local function log(premature, redis)
  redis:incr("requests_completed")
end

local ok, err = ngx.timer.at(0, log, ngx.ctx.redis)
if not ok then
    ngx.log(ngx.ERR, "failed to create timer: ", err)
    return
end

And I'm getting the following error in log_by_lua:

[error] 16644#0: attempt to send data on a closed socket: u:00007FCA22600B78, c:0000000000000000, ft:0 eof:0, context: ngx.timer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions