Skip to content

Commit

Permalink
horrific memeory leak in rate limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
Artikash committed Jun 6, 2019
1 parent e107eff commit b18fe3d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion extensions/bingtranslate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ std::wstring Translate(const std::wstring& text, std::wstring translateFrom, std
// Response formatted as JSON: translation starts with :" and ends with "}
else if (std::wsmatch results; std::regex_search(response.value(), results, std::wregex(L":\"(.+)\"\\}"))) translation = results[1];

Escape(translation);
Unescape(translation);
return translation;
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/googletranslate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
for (std::wsmatch results; std::regex_search(response.value(), results, std::wregex(L"\\[\"(.*?)\",[n\"]")); response = results.suffix())
translation += std::wstring(results[1]) + L" ";
Escape(translation);
Unescape(translation);
}
else
{
Expand Down
23 changes: 22 additions & 1 deletion extensions/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ inline std::optional<std::wstring> ReceiveHttpRequest(HINTERNET request)
return StringToWideString(data);
}

inline void Escape(std::wstring& text)
inline void Unescape(std::wstring& text)
{
for (int i = 0; i < text.size(); ++i)
{
Expand All @@ -37,3 +37,24 @@ inline void Escape(std::wstring& text)
}
}
}


class RateLimiter
{
public:
RateLimiter(int tokenCount, int delay) : tokenCount(tokenCount), delay(delay) {}

bool Request()
{
auto tokens = this->tokens.Acquire();
tokens->push_back(GetTickCount());
if (tokens->size() > tokenCount * 5) tokens->erase(tokens->begin(), tokens->begin() + tokenCount * 3);
tokens->erase(std::remove_if(tokens->begin(), tokens->end(), [this](DWORD token) { return GetTickCount() - token > delay; }), tokens->end());
return tokens->size() < tokenCount;
}

const int tokenCount, delay;

private:
Synchronized<std::vector<DWORD>> tokens;
};
12 changes: 0 additions & 12 deletions extensions/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@

#include "common.h"

class RateLimiter
{
public:
RateLimiter(int requests, int delay) : requestsLeft(requests), delay(delay) {}
bool Request() { CreateTimerQueueTimer(DUMMY, timerQueue, [](void* This, BOOLEAN) { ((RateLimiter*)This)->requestsLeft += 1; }, this, delay, 0, 0); return --requestsLeft > 0; }
int delay;

private:
std::atomic<int> requestsLeft;
AutoHandle<Functor<DeleteTimerQueue>> timerQueue = CreateTimerQueue();
};

inline std::wstring StringToWideString(const std::string& text)
{
std::vector<wchar_t> buffer(text.size() + 1);
Expand Down

0 comments on commit b18fe3d

Please sign in to comment.