-- 讀寫分離 -- -- 發(fā)送所有的非事務性Select到一個從數(shù)據(jù)庫 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] -- 需要選擇一個擁有空閑連接的從數(shù)據(jù)庫 if s.type == proxy.BACKEND_TYPE_RO and s.idling_connections > 0 then if max_conns == -1 or s.connected_clients < max_conns then max_conns = s.connected_clients max_conns_ndx = i end end end -- 至此,我們找到了一個擁有空閑連接的從數(shù)據(jù)庫 if max_conns_ndx > 0 then proxy.connection.backend_ndx = max_conns_ndx end else -- 發(fā)送到主數(shù)據(jù)庫 end return proxy.PROXY_SEND_QUERY