2017-07-27 44 views
-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 

我打算甚至達到如果又如何?

+0

@ i486我會感激更長的評論/回答。如果我的思想嚴重缺陷,並且我的意圖無法實現,那麼解釋這個問題的答案將會與任何問題一樣好。在這個(潛在的)用例中沒有任何內容可以覆蓋,一個明確的答案會挽救下一個可憐的靈魂,試圖達到這一點。 – Sim

+0

你的程序沒有睡覺,它實際上非常忙,運行那個空的while循環, – Piglet

+0

@Piglet我意識到這一點,雖然考慮到lua不支持睡眠,「忙等待」就是我所知道的。總體意圖是充分延遲會話/執行以使其他並行執行交錯。我編輯了我的問題的違規部分。 – Sim

回答

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