user1995187
user1995187

Reputation: 403

Do a http request from lua before haproxy routing a request

I have a Lua proxy that needs to route requests. Each request destination is established based on the response from another HTTP request with a header from the initial request. My understanding is that HAProxy is an event-driven software, so blocking system calls are absolutely forbidden and my code is blocking because is doing an HTTP request.

I read about yielding after the request but I think it won't help since the HTTP request is already started. The library for doing the request is https://github.com/JakobGreen/lua-requests#simple-requests

local requests = require('requests')

core.register_fetches('http_backend', function(txn)
  local dest = txn.sf:req_fhdr('X-dest')
  local url = "http://127.0.0.1:8080/service";
 
  local response = requests.get(url.."/"+dest);

  local json = response.json()

  return json.field
end )

How do I convert my code to be non-blocking?

Upvotes: 1

Views: 1676

Answers (2)

user1995187
user1995187

Reputation: 403

I managed to do it using Lua. The thing I was making wrong was using require('requests') this is blocking. Ideally for HA never use a Lua external library. I have to deal with plain sockets and do an HTTP request and very important to use HA core method core.tcp() instead of Lua sockets.

Upvotes: 1

dcorbett
dcorbett

Reputation: 442

You should consider using HAProxy's SPOE which was created exactly for these blocking scenarios.

Upvotes: 1

Related Questions