Hi Thierry, haproxy-list,

I've created another possibly interesting lua script, and it works :) (mostly). (on my test machine..)

When i visit the 192.168.0.120:9003 website i always see the 'Hello World' page. So in that regard this is usable, it is left to the browser to send the request again, not sure how safe this is in regard to mutations being send twice. It should probably check for POST requests and then just return the error without replacing it with a redirect.. Not sure if that would catch all problem cases..

Ive created a lua service that counts how many requests are made, and returns a error status every 5th request. Second there is a lua response script that checks the status, and replaces it by a redirect if it sees the faulty status 500. This does currently result in the connection being closed and reopened probably due to the txn.res:send().?.

Though i am still struggling with what is and isn't supposed to be possible.
For example the scripts below are running in 'mode http' and mostly just changing 'headers'. I expected to be able to simply read the status by calling txn.f:status() but this always seems to result in 'null'.
Manually parsing the response buffer duplicate works but seems ugly..

txn.f:status()  <  it doesnt result in the actual status.
txn.res:set()  < if used in place of send() causes 30 second delay
txn.done() < dumps core. (im not sure when ever to call it? the script below seems to match the description that this function has.?.)

Am i trying to do it wrong?

p.s. Is 'health checking' using lua possible? The redis example looks like a health 'ping'.. It could possibly be much much more flexible then the tcp-check send / tcp-check expect routines..

I'm currently testing with HA-Proxy version 1.7-dev0-e4c4b7d and the following configuration files:

#### haproxy.cfg ###
global
    maxconn            6000
    lua-load        /var/etc/haproxy/luascript_lua-count5error
defaults
    timeout connect        30000
    timeout server        30000
    timeout client        30000
    mode            http
    log            global
frontend TEST-lua-count
    bind            192.168.0.120:9002
    option            http-keep-alive
    http-request use-service lua.lua-count

frontend TEST-lua-retry-serverror
    bind            192.168.0.120:9003
    option            http-keep-alive
    http-request lua.retrystorerequest
    http-response lua.retryerrors
    default_backend hap_9002_http_ipvANY

backend hap_9002_http_ipvANY
    mode            http
    retries            3
    server            192.168.0.120_9002 192.168.0.120:9002

### luascript_lua-count5error ###
core.register_action("retryerrors" , { "http-res" }, function(txn)
    local clientip = txn.f:src()
    txn:Info("  LUA client " .. clientip)

    local s = txn.f:status() -- doesnt work?
    if s == null then
        core.Info("LUA txn.s:status RETURNED: NULL, fallback needed ??")
        local req = txn.res:dup()
        local statusstr = string.sub(req, 10, 13)
        s = tonumber(statusstr)
    end
    core.Info("LUA status " .. s)

    if s ~= 200 then
        txn:Info("LUA REDIRECT IT ! " .. s)

        local url = txn:get_priv()
        local response = ""
        response = response .. "HTTP/1.1 302 Moved\r\n"
        response = response .. "Location: " .. url .."\r\n"
        response = response .. "\r\n"

        txn.res:send(response)
        --txn.res:set(response) -- causes 30 second delay..
        --txn:done() --dumps core..
    end
end);

core.register_action("retrystorerequest" , { "http-req" }, function(txn)
    local url = txn.f:url()
    txn:set_priv(url);
end);

core.register_service("lua-count", "http", function(applet)
   if test == null then
      test = 0
   end
   test = test + 1
   local response = ""
   if test % 5 == 0 then
      applet:set_status(500)
      response = "Error " .. test
   else
      applet:set_status(200)
      response = "Hello World !" .. test
   end
   applet:add_header("content-length", string.len(response))
   applet:add_header("content-type", "text/plain")
   applet:start_response()
   applet:send(response)
end)


Reply via email to