worker_processes 2;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
resolver 8.8.4.4; # use Google's open DNS server
set $target '';
access_by_lua '
local key = ngx.var.http_user_agent
if not key then
ngx.log(ngx.ERR, "no user-agent found")
return ngx.exit(400)
end
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 second
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.log(ngx.ERR, "failed to connect to redis: ", err)
return ngx.exit(500)
end
local host, err = red:get(key)
if not host then
ngx.log(ngx.ERR, "failed to get redis key: ", err)
return ngx.exit(500)
end
if host == ngx.null then
ngx.log(ngx.ERR, "no host found for key ", key)
return ngx.exit(400)
end
ngx.var.target = host
';
proxy_pass http://$target;
}
}
}local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 second
local ok, err = red:connect("127.0.0.1", 6379)
-- do things
local host, err = red:get(key)
-- do things
-- Put the underlying socket in the worker's connection pool.
local ok, err = red:set_keepalive()
-- handle error
Thanks for the further clarification.Can I assume that the same principle applied to pintsized/lua-resty-http, a HTTP client driver for OpenResty / ngx_lua ?
--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
> HTTP/1.1 is keep alive by default - no need for a connection header.> set_keepalive() is for setting the connection pool status, not http.Which is why you want to avoid putting a socket in the connection pool when the connection is actually closed, which can be checked with the Connection header.
On 20 January 2016 at 00:44, Thibault Charbonnier <thib...@mashape.com> wrote:> HTTP/1.1 is keep alive by default - no need for a connection header.> set_keepalive() is for setting the connection pool status, not http.Which is why you want to avoid putting a socket in the connection pool when the connection is actually closed, which can be checked with the Connection header.Yep exactly, and lua-resty-http keeps track of this for you. So you can safely call set_keepalive(), which will either close or put the connection in the pool, and optionally you can check the return code if you need to know what happened: