Skip to content

Commit

Permalink
call throttler->resetDelay if only when delay present (#369)
Browse files Browse the repository at this point in the history
This will require **one less network request** for each successful
request from ExApp

Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 authored Aug 22, 2024
1 parent 2fd5fb5 commit 7c0587e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/Service/AppAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private function getUriEncodedParams(array $params): string {
* More info in docs: https://cloud-py-api.github.io/app_api/authentication.html
*/
public function validateExAppRequestToNC(IRequest $request, bool $isDav = false): bool {
$this->throttler->sleepDelayOrThrowOnMax($request->getRemoteAddress(), Application::APP_ID);
$delay = $this->throttler->sleepDelayOrThrowOnMax($request->getRemoteAddress(), Application::APP_ID);

$exAppId = $request->getHeader('EX-APP-ID');
if (!$exAppId) {
Expand Down Expand Up @@ -346,7 +346,7 @@ public function validateExAppRequestToNC(IRequest $request, bool $isDav = false)
}
}

return $this->finalizeRequestToNC($exApp, $userId, $request);
return $this->finalizeRequestToNC($exApp, $userId, $request, $delay);
} else {
$this->logger->error(sprintf('Invalid signature for ExApp: %s and user: %s.', $exApp->getAppid(), $userId !== '' ? $userId : 'null'));
$this->throttler->registerAttempt(Application::APP_ID, $request->getRemoteAddress(), [
Expand All @@ -364,7 +364,7 @@ public function validateExAppRequestToNC(IRequest $request, bool $isDav = false)
* - sets active user (null if not a user context)
* - updates ExApp last response time
*/
private function finalizeRequestToNC(ExApp $exApp, string $userId, IRequest $request): bool {
private function finalizeRequestToNC(ExApp $exApp, string $userId, IRequest $request, int $delay): bool {
if ($userId !== '') {
$activeUser = $this->userManager->get($userId);
if ($activeUser === null) {
Expand All @@ -379,10 +379,12 @@ private function finalizeRequestToNC(ExApp $exApp, string $userId, IRequest $req
$this->session->set('app_api', true);
$this->session->set('app_api_system', true); // TODO: Remove after drop support NC29

$this->throttler->resetDelay($request->getRemoteAddress(), Application::APP_ID, [
'appid' => $request->getHeader('EX-APP-ID'),
'userid' => $userId,
]);
if ($delay) {
$this->throttler->resetDelay($request->getRemoteAddress(), Application::APP_ID, [
'appid' => $request->getHeader('EX-APP-ID'),
'userid' => $userId,
]);
}
return true;
}

Expand Down

0 comments on commit 7c0587e

Please sign in to comment.