暂停一个会话但继续处理其他会话

时间:2017-07-27 11:29:18

标签: mysql lua mysql-proxy

我希望mysql-proxy lua脚本处理对网站的交错访问(例如,两个不同的浏览器窗口/用户),但是能够暂停/延迟两者中的一个而不影响另一个。交错处理会话是可能的在mysql-proyx lua中(所以看起来似乎是后面列出的输出)但是一旦我开始延迟脚本,它就会阻止所有内容而另一个会话也无法前进。

-- the query which indicates the session/connection that shall be delayed at that execution
local qoi = "SELECT loginattempts,uid FROM mybb_users WHERE username='user1' LIMIT 1"

function read_query(packet)
        if string.byte(packet) == proxy.COM_QUERY then
                query = packet:sub(2)
                start_time = os.time()
                if query == qoi then
                        print("busy wait")
                        while os.time() < start_time + 20 do
                                --nothing
                        end
                        print("busy wait end")
                end
                print("Connection id: " .. proxy.connection.server.thread_id)
        end
end

但是这个脚本最终会输出:

Connection id: 36
busy wait
busy wait end
Connection id: 36
Connection id: 36
Connection id: 36
Connection id: 37
Connection id: 37
Connection id: 36
Connection id: 36
Connection id: 36
Connection id: 37

而不是预期的

Connection id: 36
busy wait
connection id: 37
connection id: 37
busy wait end
Connection id: 36

我的意图是否可以实现,如果可以的话?

1 个答案:

答案 0 :(得分:0)

似乎不可能在lua中延迟会话但是如果我将延迟外包给mysql服务器它也可以正常工作,因为这会强制交错。

local DEBUG = true

local qoi = "SELECT loginattempts,uid FROM mybb_users WHERE username='user1' LIMIT 1"


function read_query(packet)
        ret = nil
        comp_query = qoi
        if string.byte(packet) == proxy.COM_QUERY then
                query = packet:sub(2)
                if query == comp_query then
                        if DEBUG then
                                print("found matching query " .. packet:sub(2))
                                print("insert sleep")
                        end
                        inj_query = "SELECT sleep(30);"
                        new_packet = string.char(proxy.COM_QUERY) .. inj_query
                        proxy.queries:append(1, new_packet, { resultset_is_needed = true })
                        proxy.queries:append(2, packet, { resultset_is_needed = true })
                        ret = proxy.PROXY_SEND_QUERY
                end
        end
        return ret
end


function read_query_result(inj)
        if inj.id == 1 then
                if DEBUG then
                        print("sleep query returns")
                end
                return proxy.PROXY_IGNORE_RESULT
        end
        if inj.id == 2 then
                if DEBUG then
                        print("regular query returns")
                end
                return 
        end
        return
end