Hi,

i got it working (with "math.random").

I can confirm this is non-blocking with concurrent requests. Thanks Thierry :)


Full working example:


haproxy.cfg:

-------------
global
 lua-load /etc/haproxy/delay.lua


defaults
 mode http
 timeout connect 10s
 timeout client 10s
 timeout server 10s


frontend fe
 bind 127.0.0.1:8001 name fe

 http-request lua delay_request if  { ... your condition ... }

 default_backend be


backend be
 server s 127.0.0.1:80
-------------



delay.lua:

-------------
function delay_request(txn)

    core.msleep(1000 + math.random(1000))

end
-------------


-------------------
Bjoern

2015-06-19 19:37 GMT+02:00 PiBa-NL <piba.nl....@gmail.com>:
> try it with:  math.rand(1000)
>
> bjun...@gmail.com schreef op 19-6-2015 om 14:15:
>
>> Hi,
>>
>>
>> i've tried Thierry's example:
>>
>>
>>
>> function delay_request(txn)
>>        core.msleep(1000 + txn.f.rand(1000))
>> end
>>
>>
>>
>> The request is forwarded to the backend (but isn't delayed and HAProxy
>> throws the following error):
>>
>> [ALERT] 169/140715 (8264) : Lua function 'delay_request': runtime
>> error: /etc/haproxy/case02.lua:4: bad argument #1 to 'rand' ((null)).
>>
>>
>> Problem with the rand() fetch in combination with Lua ?
>>
>>
>> Using latest 1.6 git HEAD.
>>
>>
>> I'm still learning Lua and the integration in HAProxy. Any help will
>> be appreciated.
>>
>>
>> --------------
>> Bjoern
>>
>> 2015-06-18 21:25 GMT+02:00 PiBa-NL <piba.nl....@gmail.com>:
>>>
>>> Ok i didn't realize the msleep to be coming from haproxy itself the
>>> 'core.'
>>> should have made me think twice before sending that mail.
>>> Thanks for clearing it up, of course still actual results from Bjoern
>>> will
>>> be interesting to hear a well :).
>>>
>>> Thierry schreef op 18-6-2015 om 21:12:
>>>
>>>> On Thu, 18 Jun 2015 20:27:07 +0200
>>>> PiBa-NL <piba.nl....@gmail.com> wrote:
>>>>
>>>>> Thing to check, what happens to concurrent connection requests?
>>>>> My guess is with 10 concurrent requests it might take up to 20
>>>>> seconds(worst case for 10 connections) for some requests instead of the
>>>>> expected max 2..
>>>>
>>>> Note that we don't use the sleep from the standard Lua API, but the
>>>> sleep from the HAProxy Lua API.
>>>>
>>>> The Lua sleep is not a real sleep. It have the behavior of a sleep only
>>>> in the Lua code. Is real behavior, is to block the request, set a task
>>>> timeour and give back the hand to the HAProxy scheduler.
>>>>
>>>> So, during the sleep, HAProxy is not blocked an continue to process
>>>> other connections. Same bahavior for the TCP access; it seeme to be
>>>> blocked in the lua code, but HAProxy is not blocked.
>>>>
>>>>> Thierry FOURNIER schreef op 18-6-2015 om 19:35:
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> You can do this with Lua. Its very easy.
>>>>>>
>>>>>> First, you create a lua file containing the following code. The name
>>>>>> of
>>>>>> this Lua file is "file.lua".
>>>>>>
>>>>>>       function delay_request(txn)
>>>>>>          core.msleep(1000 + txn.f.rand(1000))
>>>>>>       end
>>>>>>
>>>>>> Second, you configura haproxy for loading ths file. In the global
>>>>>> section:
>>>>>>
>>>>>>       lua-load file.lua
>>>>>>
>>>>>> In your frontend (or backend)
>>>>>>
>>>>>>       http-request lua delay_request if { ... your condition ... }
>>>>>>
>>>>>> Note that I didn't test this configuration, I'm just giving the main
>>>>>> lines. Please share your results, it's maybe interesting for everyone.
>>>>>>
>>>>>> Thierry
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Thu, 18 Jun 2015 17:55:31 +0200
>>>>>> "bjun...@gmail.com" <bjun...@gmail.com> wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> i want to delay specific requests and i want to have a random delay
>>>>>>> for every request (for example in a range from 1000ms - 2000ms)
>>>>>>>
>>>>>>>
>>>>>>> As an ugly hack, you can use the following (with a static value):
>>>>>>>
>>>>>>>
>>>>>>>     tcp-request inspect-delay 2000ms
>>>>>>>     tcp-request content accept if WAIT_END
>>>>>>>
>>>>>>>
>>>>>>> I think i can achieve random delays with Lua. Does anyone have a
>>>>>>> example how this can be realized with Lua ?
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Thanks in advance !
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> -----------
>>>>>>> Bjoern
>>>>>>>
>>>
>

Reply via email to