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]
-- 需要选择一个拥有空闲连接的从数据库
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)。
   
 
   