Skip to content

Commit

Permalink
Fixing rate limiter (#14)
Browse files Browse the repository at this point in the history
* Fixing rate limiter

* fix
  • Loading branch information
arnasbr authored Sep 25, 2024
1 parent b37a609 commit 589f579
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/traveltime_google_comparison/requests/base_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,20 @@ async def send_request(
@property
def rate_limiter(self) -> AsyncLimiter:
return self._rate_limiter


def create_async_limiter(max_rpm: int) -> AsyncLimiter:
# Convert max_rpm to requests per second
rps = max_rpm / 60

if rps < 1:
# For rates less than 1 per second, we'll use a longer time period
# to allow fractional rates, but keep max_rate low to prevent bursts
time_period = min(60, 1 / rps)
max_rate = rps * time_period
else:
# For rates of 1 per second or higher, use a 1-second time period
time_period = 1
max_rate = rps

return AsyncLimiter(max_rate=max_rate, time_period=time_period)
4 changes: 2 additions & 2 deletions src/traveltime_google_comparison/requests/google_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from datetime import datetime

import aiohttp
from aiolimiter import AsyncLimiter
from traveltimepy import Coordinates

from traveltime_google_comparison.config import Mode
from traveltime_google_comparison.requests.base_handler import (
BaseRequestHandler,
RequestResult,
create_async_limiter,
)

logger = logging.getLogger(__name__)
Expand All @@ -27,7 +27,7 @@ class GoogleRequestHandler(BaseRequestHandler):

def __init__(self, api_key, max_rpm):
self.api_key = api_key
self._rate_limiter = AsyncLimiter(max_rpm // 60, 1)
self._rate_limiter = create_async_limiter(max_rpm)

async def send_request(
self,
Expand Down
4 changes: 2 additions & 2 deletions src/traveltime_google_comparison/requests/here_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from datetime import datetime

import aiohttp
from aiolimiter import AsyncLimiter
from traveltimepy import Coordinates

from traveltime_google_comparison.config import Mode
from traveltime_google_comparison.requests.base_handler import (
BaseRequestHandler,
RequestResult,
create_async_limiter,
)

logger = logging.getLogger(__name__)
Expand All @@ -25,7 +25,7 @@ class HereRequestHandler(BaseRequestHandler):

def __init__(self, api_key, max_rpm):
self.api_key = api_key
self._rate_limiter = AsyncLimiter(max_rpm // 60, 1)
self._rate_limiter = create_async_limiter(max_rpm)

async def send_request(
self,
Expand Down
4 changes: 2 additions & 2 deletions src/traveltime_google_comparison/requests/mapbox_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from datetime import datetime

import aiohttp
from aiolimiter import AsyncLimiter
from traveltimepy import Coordinates

from traveltime_google_comparison.config import Mode
from traveltime_google_comparison.requests.base_handler import (
BaseRequestHandler,
RequestResult,
create_async_limiter,
)

logger = logging.getLogger(__name__)
Expand All @@ -25,7 +25,7 @@ class MapboxRequestHandler(BaseRequestHandler):

def __init__(self, api_key, max_rpm):
self.api_key = api_key
self._rate_limiter = AsyncLimiter(max_rpm // 60, 1)
self._rate_limiter = create_async_limiter(max_rpm)

async def send_request(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from datetime import datetime

import aiohttp
from aiolimiter import AsyncLimiter
from traveltimepy import Coordinates

from traveltime_google_comparison.config import Mode
from traveltime_google_comparison.requests.base_handler import (
BaseRequestHandler,
RequestResult,
create_async_limiter,
)

logger = logging.getLogger(__name__)
Expand All @@ -25,7 +25,7 @@ class OpenRoutesRequestHandler(BaseRequestHandler):

def __init__(self, api_key, max_rpm):
self.api_key = api_key
self._rate_limiter = AsyncLimiter(max_rpm, 60)
self._rate_limiter = create_async_limiter(max_rpm)

async def send_request(
self,
Expand Down
4 changes: 2 additions & 2 deletions src/traveltime_google_comparison/requests/osrm_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from datetime import datetime

import aiohttp
from aiolimiter import AsyncLimiter
from traveltimepy import Coordinates

from traveltime_google_comparison.config import Mode
from traveltime_google_comparison.requests.base_handler import (
BaseRequestHandler,
RequestResult,
create_async_limiter,
)

logger = logging.getLogger(__name__)
Expand All @@ -25,7 +25,7 @@ class OSRMRequestHandler(BaseRequestHandler):

def __init__(self, api_key, max_rpm):
self.api_key = api_key
self._rate_limiter = AsyncLimiter(max_rpm // 60, 1)
self._rate_limiter = create_async_limiter(max_rpm)

async def send_request(
self,
Expand Down
4 changes: 2 additions & 2 deletions src/traveltime_google_comparison/requests/tomtom_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from datetime import datetime

import aiohttp
from aiolimiter import AsyncLimiter
from traveltimepy import Coordinates

from traveltime_google_comparison.config import Mode
from traveltime_google_comparison.requests.base_handler import (
BaseRequestHandler,
RequestResult,
create_async_limiter,
)

logger = logging.getLogger(__name__)
Expand All @@ -25,7 +25,7 @@ class TomTomRequestHandler(BaseRequestHandler):

def __init__(self, api_key, max_rpm):
self.api_key = api_key
self._rate_limiter = AsyncLimiter(max_rpm // 60, 1)
self._rate_limiter = create_async_limiter(max_rpm)

async def send_request(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Union
import logging

from aiolimiter import AsyncLimiter
from traveltimepy import (
Location,
Coordinates,
Expand All @@ -17,6 +16,7 @@
from traveltime_google_comparison.requests.base_handler import (
BaseRequestHandler,
RequestResult,
create_async_limiter,
)

logger = logging.getLogger(__name__)
Expand All @@ -30,7 +30,7 @@ def __init__(self, app_id, api_key, max_rpm):
self.sdk = TravelTimeSdk(
app_id=app_id, api_key=api_key, user_agent="Travel Time Comparison Tool"
)
self._rate_limiter = AsyncLimiter(max_rpm // 60, 1)
self._rate_limiter = create_async_limiter(max_rpm)

async def send_request(
self,
Expand Down

0 comments on commit 589f579

Please sign in to comment.