Re: [LUA] Lua advanced documentation

2015-10-28 Thread PiBa-NL

Op 28-10-2015 om 9:28 schreef Thierry FOURNIER:

Hi List,

I wrote a Lua advanced documentation. This explain the Lua integration
in HAProxy, the reason of some choices. Some traps and Lua code with
advanced comments.

This doc is not terminated, but I want to release a first version. I
will fill the missing points later.

unfortunatelly I have some difficulties to write in english, if anyone
wants to correct my doc, it will be welcome.

Thank you,
Thierry

Hi Thierry,

Thanks for the doc !
Ive changed a few words here and there.. Updated doc attached.
Probably there is some more to correct for the more native English 
speaking/writing people.


I haven't tried to check it myself, but didn't see it in either of the 
documents how often is a function from core.register_task called? Or 
should it contain a loop+sleep ? Perhaps a small example could be added?


Regards
PiBa-NL

HAProxy is a powerful load balancer. It embeds many options and many
configuration styles in order to give a solution to many load balancing
problems. However, HAProxy is not universal and some special or specific
problems doesn't have solution with the native software.

This text is not a full explanation of the Lua syntax.

This text is not a replacement of the HAProxy Lua API documentation. The API
documentation can be found at the project root, in the documentation directory. 
The goal of this text is to discover how Lua is implemented in HAProxy and using
it efficiently.

However, this can be read by Lua beginners. Some examples are detailed.

Why a scripting language in HAProxy
===

HAProxy 1.5 makes at possible to do many things using samples, but some people
wants to more combining results of samples fetches, programming conditions and
loops which is not possible. Sometimes people implement these functionalities
in patches which have no meaning outside their network. These people must
maintain these patches, or worse we must integrate them in the HAProxy
mainstream.

Their need is to have an embedded programming language in order to no longer
modify the HAProxy source code, but to write their own control code. Lua is
encountered very often in the software industry, and in some open source
projects. It is easy to understand, efficient, light without external
dependencies, and leaves the resource control to the implementation. Its design
is close to the HAProxy philosophy which uses components for what they do
perfectly.

The HAProxy control block allows one to take a decision based on the comparison
between samples and patterns. The samples are extracted using fetch functions
easily extensible, and are used by actions which are also extensible. It seems
natural to allow Lua to give samples, modify them, and to be an action target.
So, Lua uses the same entities as the configuration language. This is the most
natural and reliable way fir the Lua integration. So, the Lua engine allow one
to add new sample fetch functions, new converter functions and new actions.
These new entities can access the existing samples fetches and converters
allowing to extend them without rewriting them.

The writing of the first Lua functions shows that implementing complex concepts
like protocol analysers is easy and can be extended to full services. It appears
that these services are not easy to implement with the HAProxy configuration
model which is base on four steps: fetch, convert, compare and action. HAProxy
is extended with a notion of services which are a formalisation of the existing
services like stats, cli and peers. The service is an autonomous entity with a
behaviour pattern close to that of an external client or server. The Lua engine
inherits from this new service and offers new possibilities for writing
services.

This scripting language is useful for testing new features as proof of concept.
Later, if there is general interest, the proof of concept could be integrated
with C language in the HAProxy core.

The HAProxy Lua integration also provides also a simple way for distributing Lua
packages. The final user needs only to install the Lua file, load it in HAProxy
and follow the attached documentation.

Design and technical things
===

Lua is integrated in the HAProxy event driven core. We want to preserve the
fast processing of HAProxy. To ensure this, we implement some technical concepts
between HAProxy and the Lua library.

The following paragraph describes also the interactions between Lua and HAProxy
with a technical point of view.

Prerequisite
---

Reading the following documentation links are required to understand the
current paragraph:

   HAProxy doc: http://cbonte.github.io/haproxy-dconv/configuration-1.6.html
   Lua API: http://www.lua.org/manual/5.3/
   HAProxy API: http://www.arpalert.org/src/haproxy-lua-api/1.6/index.html
   Lua guide:   http://www.lua.org/pil/

more about Lua choice
-

Lua language is very easy to extend. It is tri

Re: [LUA] Lua advanced documentation

2015-10-29 Thread Thierry FOURNIER
On Wed, 28 Oct 2015 21:18:42 +0100
PiBa-NL  wrote:

> Op 28-10-2015 om 9:28 schreef Thierry FOURNIER:
> > Hi List,
> >
> > I wrote a Lua advanced documentation. This explain the Lua integration
> > in HAProxy, the reason of some choices. Some traps and Lua code with
> > advanced comments.
> >
> > This doc is not terminated, but I want to release a first version. I
> > will fill the missing points later.
> >
> > unfortunatelly I have some difficulties to write in english, if anyone
> > wants to correct my doc, it will be welcome.
> >
> > Thank you,
> > Thierry
> Hi Thierry,
> 
> Thanks for the doc !


Thanks for read it ;)


> Ive changed a few words here and there.. Updated doc attached.
> Probably there is some more to correct for the more native English 
> speaking/writing people.
> 
> I haven't tried to check it myself, but didn't see it in either of the 
> documents how often is a function from core.register_task called? Or 
> should it contain a loop+sleep ? Perhaps a small example could be added?


Ok, this is a good comment ! My explaination was not clear. I will
improve the explaination. For your information, the function associated
with "core.register_task()" is executed once, if you want that the
function does anything each period of time, you can use a core.sleep().

I will add an example.

Thierry



Re: [LUA] Lua advanced documentation

2015-10-30 Thread Willy Tarreau
Hi guys,

On Thu, Oct 29, 2015 at 01:22:31PM +0100, Thierry FOURNIER wrote:
> Ok, this is a good comment ! My explaination was not clear. I will
> improve the explaination. For your information, the function associated
> with "core.register_task()" is executed once, if you want that the
> function does anything each period of time, you can use a core.sleep().
> 
> I will add an example.

Thierry, your doc is really useful. When you consider it in good
enough shape, please submit it, we'll happily integrate it into
1.6-stable as well. Don't be too perfectionnist, once it starts
to become useful and helpful, it's worth merging it and it will
evolve there.

Willy