From 7f8e7a1a5f1438d8c1ff889538268ba196e916c9 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 27 May 2022 12:04:51 +0200 Subject: limit number of concurrent traewelling requests to five per second --- lib/Travelynx/Command/work.pm | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm index 706eb44..b0458db 100644 --- a/lib/Travelynx/Command/work.pm +++ b/lib/Travelynx/Command/work.pm @@ -4,6 +4,7 @@ package Travelynx::Command::work; # # SPDX-License-Identifier: AGPL-3.0-or-later use Mojo::Base 'Mojolicious::Command'; +use Mojo::Promise; use DateTime; use JSON; @@ -197,15 +198,22 @@ sub run { trip_id => $trip_id ); } + my $request_count = 0; for my $account_data ( $self->app->traewelling->get_pull_accounts ) { # $account_data->{user_id} is the travelynx uid # $account_data->{user_name} is the Träwelling username + $request_count += 1; $self->app->log->debug( - "Pulling Traewelling status for UID $account_data->{user_id}"); - $self->app->traewelling_api->get_status_p( - username => $account_data->{data}{user_name}, - token => $account_data->{token} +"Scheduling Traewelling status pull for UID $account_data->{user_id}" + ); + Mojo::Promise->timer( $request_count * 0.2 )->then( + sub { + return $self->app->traewelling_api->get_status_p( + username => $account_data->{data}{user_name}, + token => $account_data->{token} + ); + } )->then( sub { my ($traewelling) = @_; -- cgit v1.2.3