用来分析、监控和变换通信数据MySQL-Proxy(2)_MySQL
程序员文章站
2022-05-24 12:29:54
...
if is_in_transaction == 0 and
packet:byte() == proxy.COM_QUERY and
packet:sub(2, 7) == "SELECT" then
local max_conns = -1
local max_conns_ndx = 0
for i = 1, #proxy.servers do
local s = proxy.servers[i]
packet:byte() == proxy.COM_QUERY and
packet:sub(2, 7) == "SELECT" then
local max_conns = -1
local max_conns_ndx = 0
for i = 1, #proxy.servers do
local s = proxy.servers[i]
-- 需要选择一个拥有空闲连接的从数据库
if s.type == proxy.BACKEND_TYPE_RO and
s.idling_connections > 0 then
if max_conns == -1 or
s.connected_clients max_conns = s.connected_clients
max_conns_ndx = i
end
end
end
-- 至此,我们找到了一个拥有空闲连接的从数据库
if max_conns_ndx > 0 then
proxy.connection.backend_ndx = max_conns_ndx
end
else
-- 发送到主数据库
end
return proxy.PROXY_SEND_QUERY
注释:此技巧还可以用来实现其他的数据分布策略,例如分片(Sharding)。