This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from The-B1T-Foundation/added_requests_limiter
Added requests limiter
- Loading branch information
Showing
8 changed files
with
124 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
source/utils/api_requests_limiter/api_requests_limiter.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2024 The B1T Foundation | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#include "api_requests_limiter.hpp" | ||
|
||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
AApi_Requests_Limiter::AApi_Requests_Limiter(std::ptrdiff_t requests_limit, std::int64_t required_duration) : | ||
Requests_Limit{ requests_limit }, Current_Requests_Count{ }, Required_Duration{ required_duration }, Previous_Time{ } | ||
{ } | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
bool AApi_Requests_Limiter::Can_Send_Request() | ||
{ | ||
auto current_time{ static_cast<std::int64_t>(std::chrono::duration_cast<std::chrono::hours>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) }; | ||
if (current_time - Previous_Time >= Required_Duration && Requests_Limit - Current_Requests_Count > 1) // 1 - free requests | ||
{ | ||
++Current_Requests_Count; | ||
return true; | ||
} | ||
|
||
Previous_Time = current_time; | ||
Current_Requests_Count = 0; | ||
|
||
return false; | ||
} | ||
|
44 changes: 44 additions & 0 deletions
44
source/utils/api_requests_limiter/api_requests_limiter.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2024 The B1T Foundation | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#pragma once | ||
|
||
#include <chrono> | ||
#include <cstdint> | ||
#include <cstddef> | ||
|
||
class AApi_Requests_Limiter | ||
{ | ||
public: | ||
explicit AApi_Requests_Limiter(std::ptrdiff_t requests_limit, std::int64_t required_duration); | ||
constexpr ~AApi_Requests_Limiter() = default; | ||
|
||
bool Can_Send_Request(); | ||
|
||
private: | ||
std::ptrdiff_t Requests_Limit; | ||
std::ptrdiff_t Current_Requests_Count; | ||
|
||
std::int64_t Required_Duration; | ||
std::int64_t Previous_Time; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,19 +72,19 @@ std::string AMessage_Reply::Get_Help_Msg() | |
} | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
std::string AMessage_Reply::Get_Info_About_Project() | ||
std::string AMessage_Reply::Get_Info_About_Project_Msg() | ||
{ | ||
return std::format("Інформація про цей проект{}\n\nЦе Open-Source проект ліцензований під MIT ліцензією.\nПосилання на код проекту: https://github.com/The-B1T-Foundation/tg_arbuz-bot\nПровідний розробник проекту: https://github.com/b1tflyyyy\n\nСвої побажання щодо функціоналу надсилайте на пошту [email protected]", BOOKS); | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
std::string AMessage_Reply::Get_Word_Definition(std::string_view primary_word, std::string_view definiton) | ||
std::string AMessage_Reply::Get_Word_Definition_Msg(std::string_view primary_word, std::string_view definiton) | ||
{ | ||
return std::format("Визначення слова {}:\n{} - {}", primary_word, primary_word, definiton); | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
std::string AMessage_Reply::Get_Not_Found_Word_Definition() | ||
std::string AMessage_Reply::Get_Not_Found_Word_Definition_Msg() | ||
{ | ||
return std::format("Скоріш за все ти ввів дивне слово, я не можу знайти визначення для нього)"); | ||
} | ||
|
@@ -99,4 +99,9 @@ std::string AMessage_Reply::Get_Metrics_Count_Msg(std::int64_t metrics_count) | |
std::string AMessage_Reply::Get_Metrics_Msg(const SMetrics& metrics) | ||
{ | ||
return std::format("Метрики Arbuz-Bot\n\nДата: {}\n\nКількість реквестів на команди:\nStart: {}\nProfile: {}\nProgrammer Game: {}\nMath Game: {}\nHelp: {}\nAbout Project: {}\nDefinition: {}", metrics.Get_Current_Date(), metrics.Start_Request_Count, metrics.Profile_Request_Count, metrics.Pr_Game_Request_Count, metrics.Math_Game_Request_Count, metrics.Help_Request_Count, metrics.About_Project_Request_Count, metrics.Definition_Request_Count); | ||
} | ||
|
||
std::string AMessage_Reply::Get_Limit_Api_Requests_Msg() | ||
{ | ||
return std::string{ "Нажаль ліміт запросів перевищено!\nСпробуйте пізніше)" }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters