Re: LUA: using converters in init phase

2017-03-25 Thread Gabor Lekeny
Hi Holger,

that is exactly what I am looking for. I have to read through the
documentation.
Nevertheless it would be nice to be able to use converters in any phase
without magic tricks.

Thank you for your help!

BR,
Gabor

On Fri, Mar 24, 2017 at 8:59 PM, Holger Just  wrote:

> Hi Gabor,
>
> Gabor Lekeny wrote:
> > 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.
>
> You might be able to use the redir parameter [1] on the server line
> already without having to dive into Lua. Since it follows HAProxy's
> normal server selection algorithms, you wouldn't have to re-implement
> (or even query) them in Lua.
>
> To quote the docs at
> http://cbonte.github.io/haproxy-dconv/1.6/configuration.html#5.2-redir
>
> The "redir" parameter enables the redirection mode for all GET and HEAD
> requests addressing this server. This means that instead of having
> HAProxy forward the request to the server, it will send an "HTTP 302"
> response with the "Location" header composed of this prefix immediately
> followed by the requested URI beginning at the leading '/' of the path
> component. That means that no trailing slash should be used after
> . All invalid requests will be rejected, and all non-GET or HEAD
> requests will be normally served by the server. Note that since the
> response is completely forged, no header mangling nor cookie insertion
> is possible in the response. However, cookies in requests are still
> analysed, making this solution completely usable to direct users to a
> remote location in case of local disaster. Main use consists in
> increasing bandwidth for static servers by having the clients directly
> connect to them. Note: never use a relative location here, it would
> cause a loop between the client and HAProxy!
>
> Example :
>
> server srv1 192.168.1.1:80 redir http://image1.mydomain.com check
>
> Best,
> Holger
>


Re: LUA: using converters in init phase

2017-03-25 Thread Gabor Lekeny
Hi Thierry,

I tried the following code to find the metatable you suggested:
haproxy.cfg:
global
lua-load /etc/haproxy/test.lua

test.lua:
core.register_init(function ()
core.Alert('--- _G ---')
for k in pairs(_G) do
core.Alert(tostring(k))
end
core.Alert('--- core ---')
for k in pairs(core) do
core.Alert(tostring(k))
end
end)

Unfortunately there is no meta_converter or similar I could use to access
converters.
Regarding the second approach: I have never made a Lua C module but I will
give it a try.

Thank you for your help!

BR,
Gabor

On Fri, Mar 24, 2017 at 7:09 PM,  wrote:

> On Fri, 24 Mar 2017 17:32:53 +0100
> Gabor Lekeny  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
> >
> >
> >
>


Re: LUA: using converters in init phase

2017-03-24 Thread Holger Just
Hi Gabor,

Gabor Lekeny wrote:
> 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.

You might be able to use the redir parameter [1] on the server line
already without having to dive into Lua. Since it follows HAProxy's
normal server selection algorithms, you wouldn't have to re-implement
(or even query) them in Lua.

To quote the docs at
http://cbonte.github.io/haproxy-dconv/1.6/configuration.html#5.2-redir

The "redir" parameter enables the redirection mode for all GET and HEAD
requests addressing this server. This means that instead of having
HAProxy forward the request to the server, it will send an "HTTP 302"
response with the "Location" header composed of this prefix immediately
followed by the requested URI beginning at the leading '/' of the path
component. That means that no trailing slash should be used after
. All invalid requests will be rejected, and all non-GET or HEAD
requests will be normally served by the server. Note that since the
response is completely forged, no header mangling nor cookie insertion
is possible in the response. However, cookies in requests are still
analysed, making this solution completely usable to direct users to a
remote location in case of local disaster. Main use consists in
increasing bandwidth for static servers by having the clients directly
connect to them. Note: never use a relative location here, it would
cause a loop between the client and HAProxy!

Example :

server srv1 192.168.1.1:80 redir http://image1.mydomain.com check

Best,
Holger



Re: LUA: using converters in init phase

2017-03-24 Thread thierry . fournier
On Fri, 24 Mar 2017 17:32:53 +0100
Gabor Lekeny  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
> 
> 
> 



LUA: using converters in init phase

2017-03-24 Thread Gabor Lekeny

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?

Thanks in advance.

BR, Gabor