improve ui for rate limiter

This commit is contained in:
Akash Mozumdar 2021-06-30 17:52:52 -06:00
parent ac95d873f1
commit a566818ad4
8 changed files with 31 additions and 27 deletions

View File

@ -193,7 +193,7 @@ extern const std::unordered_map<std::wstring, std::wstring> codes
};
bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true, useFilter = true;
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 1000;
int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 1000;
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp)
{

View File

@ -98,7 +98,7 @@ extern const std::unordered_map<std::wstring, std::wstring> codes
};
bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = true, useCache = true, useFilter = true;
int tokenCount = 10, tokenRestoreDelay = 60000, maxSentenceSize = 1000;
int tokenCount = 10, rateLimitTimespan = 60000, maxSentenceSize = 1000;
enum KeyType { CAT, REST };
int keyType = REST;

View File

@ -98,7 +98,7 @@ extern const std::unordered_map<std::wstring, std::wstring> codes
};
bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true;
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 2500;
int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 2500;
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{

View File

@ -47,7 +47,7 @@ extern const std::unordered_map<std::wstring, std::wstring> codes
};
bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true;
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 2500;
int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 2500;
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{

View File

@ -113,7 +113,7 @@ extern const std::unordered_map<std::wstring, std::wstring> codes
};
bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true;
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 2500;
int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 2500;
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{

View File

@ -233,7 +233,7 @@ extern const std::unordered_map<std::wstring, std::wstring> codes
};
bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true, useFilter = true;
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 1000;
int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 1000;
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp)
{

View File

@ -3,6 +3,7 @@
#include "translatewrapper.h"
#include "blockmarkup.h"
#include "network.h"
#include <concurrent_priority_queue.h>
#include <map>
#include <fstream>
#include <QComboBox>
@ -15,8 +16,8 @@ extern const char* RATE_LIMIT_ALL_THREADS;
extern const char* RATE_LIMIT_SELECTED_THREAD;
extern const char* USE_TRANS_CACHE;
extern const char* FILTER_GARBAGE;
extern const char* RATE_LIMIT_TOKEN_COUNT;
extern const char* RATE_LIMIT_TOKEN_RESTORE_DELAY;
extern const char* MAX_TRANSLATIONS_IN_TIMESPAN;
extern const char* TIMESPAN;
extern const char* MAX_SENTENCE_SIZE;
extern const char* API_KEY;
extern const wchar_t* TOO_MANY_TRANS_REQUESTS;
@ -25,7 +26,7 @@ extern const char* TRANSLATION_PROVIDER;
extern const char* GET_API_KEY_FROM;
extern const QStringList languagesTo, languagesFrom;
extern bool translateSelectedOnly, rateLimitAll, rateLimitSelected, useCache, useFilter;
extern int tokenCount, tokenRestoreDelay, maxSentenceSize;
extern int tokenCount, rateLimitTimespan, maxSentenceSize;
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp);
const std::string TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache.txt", TRANSLATION_PROVIDER);
@ -92,8 +93,8 @@ public:
connect(checkBox, &QCheckBox::clicked, [label, &value](bool checked) { settings.setValue(label, value = checked); });
}
for (auto [value, label] : Array<int&, const char*>{
{ tokenCount, RATE_LIMIT_TOKEN_COUNT },
{ tokenRestoreDelay, RATE_LIMIT_TOKEN_RESTORE_DELAY },
{ tokenCount, MAX_TRANSLATIONS_IN_TIMESPAN },
{ rateLimitTimespan, TIMESPAN },
{ maxSentenceSize, MAX_SENTENCE_SIZE },
})
{
@ -153,15 +154,18 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
public:
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(), [](DWORD token) { return GetTickCount() - token > tokenRestoreDelay; }), tokens->end());
return tokens->size() < tokenCount;
DWORD current = GetTickCount(), token;
while (tokens.try_pop(token)) if (token > current - rateLimitTimespan)
{
tokens.push(token); // popped one too many
break;
}
tokens.push(current);
return tokens.size() < tokenCount;
}
private:
Synchronized<std::vector<DWORD>> tokens;
concurrency::concurrent_priority_queue<DWORD, std::greater<DWORD>> tokens;
} rateLimiter;
auto Trim = [](std::wstring& text)

View File

@ -141,8 +141,8 @@ const char* TRANSLATE_SELECTED_THREAD_ONLY = u8"Translate selected text thread o
const char* RATE_LIMIT_ALL_THREADS = u8"Rate limit all text threads";
const char* RATE_LIMIT_SELECTED_THREAD = u8"Rate limit selected text thread";
const char* USE_TRANS_CACHE = u8"Use translation cache";
const char* RATE_LIMIT_TOKEN_COUNT = u8"Rate limiter token count";
const char* RATE_LIMIT_TOKEN_RESTORE_DELAY = u8"Rate limiter token restore delay (ms)";
const char* MAX_TRANSLATIONS_IN_TIMESPAN = u8"Max translation requests in timespan";
const char* TIMESPAN = u8"Timespan (ms)";
const wchar_t* TOO_MANY_TRANS_REQUESTS = L"Rate limit exceeded: refuse to make more translation requests";
const wchar_t* TRANSLATION_ERROR = L"Error while translating";
const char* USE_PREV_SENTENCE_CONTEXT = u8"Use previous sentence as context";
@ -418,8 +418,8 @@ Clic y arrastra los bordes de la ventana para moverla, o en la esquina inferior
RATE_LIMIT_ALL_THREADS = u8"限制所有文本线程的请求频率";
RATE_LIMIT_SELECTED_THREAD = u8"限制当前选择线程的请求频率";
USE_TRANS_CACHE = u8"使用缓存的翻译结果";
RATE_LIMIT_TOKEN_COUNT = u8"限流器令牌数";
RATE_LIMIT_TOKEN_RESTORE_DELAY = u8"限流器令牌重置时间 (ms)";
MAX_TRANSLATIONS_IN_TIMESPAN = u8"限流器令牌数";
TIMESPAN = u8"限流器令牌重置时间 (ms)";
TOO_MANY_TRANS_REQUESTS = L"太多翻译请求: 拒绝生成更多";
TRANSLATION_ERROR = L"翻译时出错";
USE_PREV_SENTENCE_CONTEXT = u8"使用之前的句子作为上下文";
@ -593,8 +593,8 @@ padding: длина добавочных данных перед строкой
RATE_LIMIT_ALL_THREADS = u8"Ограничение скорости для всех текстовых потоков";
RATE_LIMIT_SELECTED_THREAD = u8"Ограничение скорости выделенного текстового потока";
USE_TRANS_CACHE = u8"Использовать кеш перевода";
RATE_LIMIT_TOKEN_COUNT = u8"Ограничение частоты подсчёта токенов";
RATE_LIMIT_TOKEN_RESTORE_DELAY = u8"Ограничение частоты задержки восстановления токенов (мс)";
MAX_TRANSLATIONS_IN_TIMESPAN = u8"Ограничение частоты подсчёта токенов";
TIMESPAN = u8"Ограничение частоты задержки восстановления токенов (мс)";
TOO_MANY_TRANS_REQUESTS = L"Слишком много запросов для перевода: отклонено";
TRANSLATION_ERROR = L"Ошибка при переводе";
USE_PREV_SENTENCE_CONTEXT = u8"Использовать предыдущее предложение как контекст";
@ -852,8 +852,8 @@ esempio: Textractor -p4466 -p"My Game.exe" sta tentando di inniettare i processi
RATE_LIMIT_ALL_THREADS = u8"Rate limit tutti i thread del testo";
RATE_LIMIT_SELECTED_THREAD = u8"Rate limit thread del testo selezionato";
USE_TRANS_CACHE = u8"Utilizza la cache di traduzione";
RATE_LIMIT_TOKEN_COUNT = u8"Numero di token del Rate Limit";
RATE_LIMIT_TOKEN_RESTORE_DELAY = u8"Token del rate limit ripristina il ritardo (ms)";
MAX_TRANSLATIONS_IN_TIMESPAN = u8"Numero di token del Rate Limit";
TIMESPAN = u8"Token del rate limit ripristina il ritardo (ms)";
TOO_MANY_TRANS_REQUESTS = L"Rate limit superato: rifiuta per fare altre richieste di traduzione";
TRANSLATION_ERROR = L"Errore durante la traduzione";
USE_PREV_SENTENCE_CONTEXT = u8"Utilizza la precedente sentenza come contesto";
@ -1272,8 +1272,8 @@ example: Textractor -p4466 -p"My Game.exe" tries to inject processes with id 446
RATE_LIMIT_ALL_THREADS = u8"Taux limite tout les threads de texte";
RATE_LIMIT_SELECTED_THREAD = u8"Limite de débit du thread de texte sélectionné";
USE_TRANS_CACHE = u8"Utiliser le cache de traduction";
RATE_LIMIT_TOKEN_COUNT = u8"Nombre de tokens du limiteur de débit";
RATE_LIMIT_TOKEN_RESTORE_DELAY = u8"Délai de restauration du token du limiteur de débit (ms)";
MAX_TRANSLATIONS_IN_TIMESPAN = u8"Nombre de tokens du limiteur de débit";
TIMESPAN = u8"Délai de restauration du token du limiteur de débit (ms)";
TOO_MANY_TRANS_REQUESTS = L"Limite de taux dépassée: refus de faire plus de demande de traduction";
TRANSLATION_ERROR = L"Une erreur est survenue pendant la traduction";
USE_PREV_SENTENCE_CONTEXT = u8"Utiliser la phrase précédente comme contexte";