From fbe3739da349afbb2e42bc6c1ce5a20b657289ac Mon Sep 17 00:00:00 2001 From: ElzorFox Date: Sun, 19 Mar 2017 15:46:52 +0500 Subject: VKontakte: fix sending API requests (maximum 3 requests to API methods per second) fix sending long and uint parameters as signed int version bump --- protocols/VKontakte/src/vk_queue.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'protocols/VKontakte/src/vk_queue.cpp') diff --git a/protocols/VKontakte/src/vk_queue.cpp b/protocols/VKontakte/src/vk_queue.cpp index d23c68e995..ba844d1436 100644 --- a/protocols/VKontakte/src/vk_queue.cpp +++ b/protocols/VKontakte/src/vk_queue.cpp @@ -151,7 +151,9 @@ void CVkProto::WorkerThread(void*) break; AsyncHttpRequest *pReq; - bool need_sleep = false; + ULONG uTime[3] = { 0, 0, 0 }; + long lWaitingTime = 0; + while (true) { { mir_cslock lck(m_csRequestsQueue); @@ -160,15 +162,30 @@ void CVkProto::WorkerThread(void*) pReq = m_arRequestsQueue[0]; m_arRequestsQueue.remove(0); - need_sleep = (m_arRequestsQueue.getCount() > 1) && (pReq->m_bApiReq); // more than two to not gather + + ULONG utime = GetTickCount(); + lWaitingTime = (utime - uTime[0]) > 1000 ? 0 : 1100 - (utime - uTime[0]); + + if (!(pReq->m_bApiReq) || lWaitingTime < 0) + lWaitingTime = 0; } + if (m_bTerminated) break; - ExecuteRequest(pReq); - if (need_sleep) { // There can be maximum 3 requests to API methods per second from a client - Sleep(800); // (c) https://vk.com/dev/api_requests - debugLogA("CVkProto::WorkerThread: need sleep"); + + if (lWaitingTime) { + debugLogA("CVkProto::WorkerThread: need sleep %d msec", lWaitingTime); + Sleep(lWaitingTime); + } + + if (pReq->m_bApiReq) { + uTime[0] = uTime[1]; + uTime[1] = uTime[2]; + uTime[2] = GetTickCount(); + // There can be maximum 3 requests to API methods per second from a client + // see https://vk.com/dev/api_requests } + ExecuteRequest(pReq); } } @@ -190,7 +207,7 @@ AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const INT_PARAM ¶m) CMStringA &s = pReq->m_szParam; if (!s.IsEmpty()) s.AppendChar('&'); - s.AppendFormat("%s=%i", param.szName, param.iValue); + s.AppendFormat("%s=%ld", param.szName, param.iValue); return pReq; } -- cgit v1.2.3