mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-09-13 20:16:07 -05:00
Add handling for Twitch API throttling
This commit is contained in:
@@ -4,11 +4,13 @@
|
||||
|
||||
#include <log-helper.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace advss {
|
||||
|
||||
static constexpr std::string_view clientID = "ds5tt4ogliifsqc04mz3d3etnck3e5";
|
||||
static const int cacheTimeoutSeconds = 10;
|
||||
static std::atomic_bool apiIsThrottling = {false};
|
||||
|
||||
const char *GetClientID()
|
||||
{
|
||||
@@ -86,6 +88,37 @@ static std::string getRequestBody(const OBSData &data)
|
||||
return json ? json : "";
|
||||
}
|
||||
|
||||
static void handleThrottling(const httplib::Result &response)
|
||||
{
|
||||
const auto &headers = response.value().headers;
|
||||
const auto rateLimitResetTimeStr = headers.find("Ratelimit-Reset");
|
||||
if (rateLimitResetTimeStr == headers.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::time_t rateLimitResetTime =
|
||||
std::stoll(rateLimitResetTimeStr->second);
|
||||
auto resetTimePoint =
|
||||
std::chrono::system_clock::from_time_t(rateLimitResetTime);
|
||||
auto sleepDuration = resetTimePoint - std::chrono::system_clock::now();
|
||||
if (sleepDuration.count() < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
blog(LOG_WARNING, "Twitch API access is throttled for %lld seconds!",
|
||||
static_cast<long long int>(
|
||||
std::chrono::duration_cast<std::chrono::seconds>(
|
||||
sleepDuration)
|
||||
.count()));
|
||||
apiIsThrottling = true;
|
||||
|
||||
auto resetThread = std::thread([sleepDuration]() {
|
||||
std::this_thread::sleep_for(sleepDuration);
|
||||
apiIsThrottling = false;
|
||||
});
|
||||
resetThread.detach();
|
||||
}
|
||||
|
||||
static RequestResult processResult(const httplib::Result &response,
|
||||
const char *funcName)
|
||||
{
|
||||
@@ -97,6 +130,10 @@ static RequestResult processResult(const httplib::Result &response,
|
||||
return {};
|
||||
}
|
||||
|
||||
if (response->status == 429) {
|
||||
handleThrottling(response);
|
||||
}
|
||||
|
||||
RequestResult result;
|
||||
result.status = response->status;
|
||||
|
||||
@@ -116,6 +153,10 @@ static RequestResult sendGetRequest(const TwitchToken &token,
|
||||
const std::string &path,
|
||||
const httplib::Params ¶ms)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
httplib::Client cli(uri);
|
||||
auto tokenStr = token.GetToken();
|
||||
|
||||
@@ -136,6 +177,10 @@ RequestResult SendGetRequest(const TwitchToken &token, const std::string &uri,
|
||||
const std::string &path,
|
||||
const httplib::Params ¶ms, bool useCache)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static std::map<Args, CacheEntry> cache;
|
||||
static std::mutex mtx;
|
||||
[[maybe_unused]] static bool _ = []() {
|
||||
@@ -174,6 +219,10 @@ static RequestResult sendPostRequest(const TwitchToken &token,
|
||||
const httplib::Params ¶ms,
|
||||
const OBSData &data)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
httplib::Client cli(uri);
|
||||
auto tokenStr = token.GetToken();
|
||||
|
||||
@@ -198,6 +247,10 @@ RequestResult SendPostRequest(const TwitchToken &token, const std::string &uri,
|
||||
const httplib::Params ¶ms,
|
||||
const OBSData &data, bool useCache)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static std::map<Args, CacheEntry> cache;
|
||||
static std::mutex mtx;
|
||||
[[maybe_unused]] static bool _ = []() {
|
||||
@@ -237,6 +290,10 @@ static RequestResult sendPutRequest(const TwitchToken &token,
|
||||
const httplib::Params ¶ms,
|
||||
const OBSData &data)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
httplib::Client cli(uri);
|
||||
auto tokenStr = token.GetToken();
|
||||
|
||||
@@ -261,6 +318,10 @@ RequestResult SendPutRequest(const TwitchToken &token, const std::string &uri,
|
||||
const httplib::Params ¶ms, const OBSData &data,
|
||||
bool useCache)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static std::map<Args, CacheEntry> cache;
|
||||
static std::mutex mtx;
|
||||
[[maybe_unused]] static bool _ = []() {
|
||||
@@ -300,6 +361,10 @@ static RequestResult sendPatchRequest(const TwitchToken &token,
|
||||
const httplib::Params ¶ms,
|
||||
const OBSData &data)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
httplib::Client cli(uri);
|
||||
auto tokenStr = token.GetToken();
|
||||
|
||||
@@ -324,6 +389,10 @@ RequestResult SendPatchRequest(const TwitchToken &token, const std::string &uri,
|
||||
const httplib::Params ¶ms,
|
||||
const OBSData &data, bool useCache)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static std::map<Args, CacheEntry> cache;
|
||||
static std::mutex mtx;
|
||||
[[maybe_unused]] static bool _ = []() {
|
||||
@@ -362,6 +431,10 @@ static RequestResult sendDeleteRequest(const TwitchToken &token,
|
||||
const std::string &path,
|
||||
const httplib::Params ¶ms)
|
||||
{
|
||||
if (apiIsThrottling) {
|
||||
return {};
|
||||
}
|
||||
|
||||
httplib::Client cli(uri);
|
||||
auto tokenStr = token.GetToken();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user