On Tue, Nov 3, 2015 at 5:53 AM, Thrawn <shell_layer-git...@yahoo.com.au> wrote:
> Now that HAProxy has Lua support, I'm looking at the possibility of setting 
> up an echo server, which simply responds with the observed remote address of 
> the client (currently implemented in PHP as <?php echo 
> $_SERVER['REMOTE_ADDRESS']; ?>).
>
>
> Does anyone have a suggestion of the most efficient possible implementation 
> of this? If possible, it should handle millions of clients polling it 
> regularly, so speed is essential.
>
>
> Thanks
>

Hi,

content of echo.lua file:
-- a simple echo server
-- it generates a response whose body contains the client IP address
core.register_action("echo", { "http-req" }, function (txn)
        local buffer = ""
        local response = ""

        buffer = txn.f:src()

        response = response .. "HTTP/1.0 200 OK\r\n"
        response = response .. "Server: haproxy-lua/echo\r\n"
        response = response .. "Content-Type: text/html\r\n"
        response = response .. "Content-Length: " .. buffer:len() .. "\r\n"
        response = response .. "Connection: close\r\n"
        response = response .. "\r\n"
        response = response .. buffer

        txn.res:send(response)
        txn:done()
end)

content of haproxy's configuration:

global
  log 127.0.0.1 local0
  lua-load echo.lua

frontend echo
  bind *:10004
  mode http
  http-request lua.echo


Don't forget to setup timeouts, etc...

Baptiste

Reply via email to