On Mon, 30 May 2016 16:35:20 -0400 Louis Munro <[email protected]> wrote:
> Hello, > > I have been playing a little bit with Lua and HAProxy lately and I would like > to know what can be achieved, and what is never going to work before I go too > far down the rabbit hole. > > Specifically, I am attempting to dynamically select a backend based on the > details of the connection and some configuration stored in Redis. > E.g. I would like to be able to send clients to a different backend depending > on whether their IP is part of a set, or perhaps even tarpitting such a > connection. > That’s only an example, as my plans are to add dynamicism to HAProxy so that > changing configuration can be done without restarting, as much as possible. > Are these things possible? Hi, you can do that you describre. First, you can read the general introduction: http://www.arpalert.org/haproxy-lua.html For using redis, you can deal with cosocket, the API documentation provides an example with one redis library: http://www.arpalert.org/src/haproxy-lua-api/1.6/index.html#external-lua-libraries > I have been able to do things like returning a different URL in a redirect > from a fetch (as explained in the documentation) or do more complex request > handling in an applet, but I am not sure the configuration syntax currently > allow what I want considering that fetches cannot yield, and applets are > probably not the way to go to achieve something like: > > use_backend dynamic if lua.is_dynamic > > or > > use_backend lua.dispatcher > > Any hints or pointer to the one true way from anyone? You can use an action (who can yield), store the result in a variable, and use this variable for decisions. http://www.arpalert.org/src/haproxy-lua-api/1.6/index.html#TXN.set_var http://cbonte.github.io/haproxy-dconv/configuration-1.6.html#7.3.2-var core.register_action("select", { "http-req" }, function(txn) -- deal with redis -- ... -- store the result: txn:set_var("req.action", "backend1") end) In the haproxy configuration: frontend name http-request lua.select use_backend backend1 if { var(req.action) -m str backend1 } or: frontend name http-request lua.select use_backend %[var(req.action)] Note that I wrote these example from scratch and without tests, some syntax error can appear. Don't hesitate to share your work (if it is possible). I try to list article about haproxy and lua. http://www.arpalert.org/haproxy-lua-articles.html Thierry > Regards, > -- > Louis Munro > [email protected] :: www.inverse.ca > +1.514.447.4918 x125 :: +1 (866) 353-6153 x125 > Inverse inc. :: Leaders behind SOGo (www.sogo.nu) and PacketFence > (www.packetfence.org) > --

