Skip to content

Instantly share code, notes, and snippets.

@freakhill
Created September 29, 2016 07:44
Show Gist options
  • Save freakhill/673bf510a79478cd58be02f5414fa0cf to your computer and use it in GitHub Desktop.
Save freakhill/673bf510a79478cd58be02f5414fa0cf to your computer and use it in GitHub Desktop.
haproxy lua script for superserver (to use with s6)
global
lua-load '/servers/haproxy/superserver.lua'
frontend tailon-frontend
bind :9090
tcp-request content set-var(txn.service_dir) str("/s6-supervised-servers/tailon")
tcp-request content lua.extend-life
default_backend tailon-backend
backend tailon-backend
server tailon-server localhost:9200
superserver = {}
superserver.version = "0.0.1"
--
-- Configuration
--
superserver.lifetime = 60
superserver.healthcheck_period = 3
--
-- State
--
superserver.expiracy_map = {}
--
-- ~Constant~
--
superserver.ticks_by_lifetime = superserver.lifetime / superserver.healthcheck_period
--
-- Server killing loop
--
superserver.shinigami_task = function()
while true do
for service_dir, ticks_to_live in pairs(superserver.expiracy_map) do
if ticks_to_live == 0 then
-- do nothing
elseif ticks_to_live == 1 then
core.log(core.info, "killing service " .. service_dir)
os.execute('s6-svc -d ' .. service_dir)
superserver.expiracy_map[service_dir] = 0
else
superserver.expiracy_map[service_dir] = ticks_to_live - 1
end
end
core.sleep(superserver.healthcheck_period)
end
end
--
-- Server launching and life extension
--
superserver.extend_life_action = function(txn)
-- core.log(core.info, "extend_life txn " .. txn)
local service_dir = txn.get_var(txn, 'txn.service_dir')
if service_dir then
core.log(core.info, "extending life for service_dir " .. service_dir)
local ticks_to_live = superserver.expiracy_map[service_dir] or 0
if ticks_to_live == 0 then
core.log(core.info, "starting service " .. service_dir)
os.execute('s6-svc -u ' .. service_dir)
end
superserver.expiracy_map[service_dir] = superserver.ticks_by_lifetime
else
core.log(core.warning, "failed to extend life, service_dir var not set.")
end
end
--
-- Register code in haproxy!
--
core.register_task(superserver.shinigami_task)
core.register_action('extend-life', {'tcp-req', 'tcp-res',
'http-req', 'http-res'},
superserver.extend_life_action)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment