On Fri, 24 Mar 2017 17:32:53 +0100
Gabor Lekeny <gabor.lek...@gmail.com> wrote:

> Hi!
> 
> We have been using haproxy for many years and recently I found that it 
> is possible to use Lua to dynamically configure it. That is great!
> 
> I would like to create a service which balances the HTTP requests on 
> many servers without passing through the traffic on the proxy: actually 
> it would redirect (HTTP 3xx) to the target server. As server status is 
> available in haproxy (core.proxies[backend].servers), only the balance 
> algorithm is needed to select an alive server for redirection. I could 
> not find any solution to get the server id or name after balance 
> algorithm (eg. balance uri) runs without sending the request to the 
> selected server.
> 
> That is why I thought about  to implement the balancing in Lua but 
> converters like crc32 or djb2 are not available in init (or I have not 
> found how to access them). I would like to precalculate server hashes in 
> init (for consistent hash) and not in runtime.
> 
> Is there a way to get the backend id or name without proxying the request?
> Are converter functions available in init phase?


Hi, Maybe I'm wrong, but it seems that the server is choosed after the
Lua executions (action or sample fetch), so it is not possible to known
the chossen serveur during Lua phase.

In other way, the choice of the server is not easy and it is not easiy
predictible.

The converters are static functions, and they can run during the init
phase, but there are not accessible. Maybe it have an ugly solution
that consist to create a fake object Converter and use it. The
following code is just a guideline, it is not tested, and the Lua
syntax is not checked.

   -- Get the metable of converters searching in in the Global object
   -- I assume the variable meta_converter contains this metatable
   meta_converter = ...

   -- Create new object which is an array containing specific content
   -- in the slot "0"
   convs[0] = 0
   set_metatable(convs, meta_converter)

   -- Now conv is a Converter object, and maybe it can execute some
   -- converters.
   convs:crc32("test")
   
I'm afraid that this method doesn't work or in the worst case produce
segfault, but you can try.

In other way, if you are able to procude some line of C, you can export
the hash function from haproxy in a file. These function are autonomous
and doesn't have dependencies. You create your own Lua library
containing these two functions. You will find easyly tutorials.

BR,
Thierry


> Thanks in advance.
> 
> BR, Gabor
> 
> 
> 

Reply via email to