Skip to content

Commit

Permalink
Handle ratelimiting properly for Account Link request
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Nov 15, 2024
1 parent 611343b commit 5c0eb09
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
7 changes: 6 additions & 1 deletion frontend/src/lib/MessageHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function handleInvalidMessage() {
}

const PayloadTypes = Object.keys(HubToLocalMessagePayload).length / 2;
const PayloadHandlers: MessageHandler[] = new Array<MessageHandler>(PayloadTypes).fill(handleInvalidMessage);
const PayloadHandlers: MessageHandler[] = new Array<MessageHandler>(PayloadTypes).fill(
handleInvalidMessage
);

PayloadHandlers[HubToLocalMessagePayload.ReadyMessage] = (cli, msg) => {
const payload = new ReadyMessage();
Expand Down Expand Up @@ -109,6 +111,9 @@ PayloadHandlers[HubToLocalMessagePayload.AccountLinkCommandResult] = (cli, msg)
case AccountLinkResultCode.InvalidCode:
reason = 'Invalid code';
break;
case AccountLinkResultCode.RateLimited:
reason = 'Too many requests';
break;
case AccountLinkResultCode.InternalError:
reason = 'Internal error';
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export enum AccountLinkResultCode {
InvalidCodeLength = 2,
NoInternetConnection = 3,
InvalidCode = 4,
InternalError = 5
RateLimited = 5,
InternalError = 6
}
9 changes: 6 additions & 3 deletions include/serialization/_fbs/HubToLocalMessage_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,33 @@ enum class AccountLinkResultCode : uint8_t {
InvalidCodeLength = 2,
NoInternetConnection = 3,
InvalidCode = 4,
InternalError = 5,
RateLimited = 5,
InternalError = 6,
MIN = Success,
MAX = InternalError
};

inline const AccountLinkResultCode (&EnumValuesAccountLinkResultCode())[6] {
inline const AccountLinkResultCode (&EnumValuesAccountLinkResultCode())[7] {
static const AccountLinkResultCode values[] = {
AccountLinkResultCode::Success,
AccountLinkResultCode::CodeRequired,
AccountLinkResultCode::InvalidCodeLength,
AccountLinkResultCode::NoInternetConnection,
AccountLinkResultCode::InvalidCode,
AccountLinkResultCode::RateLimited,
AccountLinkResultCode::InternalError
};
return values;
}

inline const char * const *EnumNamesAccountLinkResultCode() {
static const char * const names[7] = {
static const char * const names[8] = {
"Success",
"CodeRequired",
"InvalidCodeLength",
"NoInternetConnection",
"InvalidCode",
"RateLimited",
"InternalError",
nullptr
};
Expand Down
2 changes: 1 addition & 1 deletion schemas
3 changes: 2 additions & 1 deletion src/GatewayConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ AccountLinkResultCode GatewayConnectionManager::Link(std::string_view linkCode)
auto response = HTTP::JsonAPI::LinkAccount(linkCode);

if (response.result == HTTP::RequestResult::RateLimited) {
return AccountLinkResultCode::InternalError; // Just return false, don't spam the console with errors
OS_LOGW(TAG, "Account Link request got ratelimited");
return AccountLinkResultCode::RateLimited;
}
if (response.result != HTTP::RequestResult::Success) {
OS_LOGE(TAG, "Error while getting auth token: %d %d", response.result, response.code);
Expand Down

1 comment on commit 5c0eb09

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format (v18.1.8) reports: 1 file(s) not formatted
  • include/serialization/_fbs/HubToLocalMessage_generated.h

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.