-1

我想要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

我的意图甚至可以实现吗?如果可以,如何实现?

4

1 回答 1

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
于 2017-07-27T14:17:06.393 回答