mysql-proxy-discuss team mailing list archive
-
mysql-proxy-discuss team
-
Mailing list archive
-
Message #00049
Re: Choose which server to connect to
Hi Pierre,
I guess the script may look like this:
--
-- redirect-by-client-address.lua
-- author: Joshua Zhu (zhuzhaoyuan AT gmail.com)
--
local string = require("string")
local is_debug = true
local cli_svr_map
function get_server(client)
if not cli_svr_map then
-- Initializing
-- Note: You should modify the client IPs and the backend addresses
below!
cli_svr_map = {
["192.168.0.189"] = {
address = "192.168.0.192:3306",
backend_ndx = -1 },
["192.168.0.192"] = {
address = "192.168.0.189:3306",
backend_ndx = -1 },
}
for _, v in pairs(cli_svr_map) do
for i = 1, #proxy.global.backends do
local backend = proxy.global.backends[i]
if v.address == backend.address then
v.backend_ndx = i
break
end
end
end
if is_debug then
print("map table:")
for k, v in pairs(cli_svr_map) do
print(" [" .. k .. "] = {" .. v.address .. ", " ..
v.backend_ndx .. "}")
end
end
end
local host = string.match(client, "([^:]+):")
if cli_svr_map[host] then
return cli_svr_map[host].backend_ndx
end
return -1
end
function connect_server()
if is_debug then
print("[connect_server] " .. proxy.connection.client.address)
print("we have " .. #proxy.global.backends .. " backends:")
for i = 1, #proxy.global.backends do
local backend = proxy.global.backends[i]
print(" [" .. i .. "].connected_clients = " ..
backend.connected_clients)
print(" [" .. i .. "].address = " .. backend.address)
print(" [" .. i .. "].state = " .. backend.state)
print(" [" .. i .. "].type = " .. backend.type)
print(" [" .. i .. "].uuid = " .. (backend.uuid or "(nil)"))
end
end
local index = get_server(proxy.connection.client.address)
if index > 0 and index <= #proxy.global.backends then
if proxy.global.backends[index].state ~= proxy.BACKEND_STATE_DOWN
then
if is_debug then
print("redirect the client to backends[" ..
proxy.global.backends[index].address .. "]")
end
proxy.connection.backend_ndx = index
end
end
end
Cheers!
--
Joshua Zhu
http://blog.zhuzhaoyuan.com
Follow ups
References