Skip to content

Commit

Permalink
Reduce sync interval to 10 secs for first hour of runtime (#11)
Browse files Browse the repository at this point in the history
* Reduce sync interval to 10 secs for first hour of runtime

* Fix mock for sync interval in tests
  • Loading branch information
itssimon authored Feb 12, 2024
1 parent 7878413 commit 3fd00ef
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 336 deletions.
2 changes: 0 additions & 2 deletions apitally/client/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ def __init__(
client_id: str,
env: str,
sync_api_keys: bool = False,
sync_interval: float = 60,
key_cache_class: Optional[Type[ApitallyKeyCacheBase]] = None,
) -> None:
super().__init__(
client_id=client_id,
env=env,
sync_api_keys=sync_api_keys,
sync_interval=sync_interval,
key_cache_class=key_cache_class,
)
self._stop_sync_loop = False
Expand Down
11 changes: 9 additions & 2 deletions apitally/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
HUB_VERSION = "v1"
REQUEST_TIMEOUT = 10
MAX_QUEUE_TIME = 3600
SYNC_INTERVAL = 60
INITIAL_SYNC_INTERVAL = 10
INITIAL_SYNC_INTERVAL_DURATION = 3600

TApitallyClient = TypeVar("TApitallyClient", bound="ApitallyClientBase")

Expand All @@ -43,7 +46,6 @@ def __init__(
client_id: str,
env: str,
sync_api_keys: bool = False,
sync_interval: float = 60,
key_cache_class: Optional[Type[ApitallyKeyCacheBase]] = None,
) -> None:
if hasattr(self, "client_id"):
Expand All @@ -58,7 +60,6 @@ def __init__(
self.client_id = client_id
self.env = env
self.sync_api_keys = sync_api_keys
self.sync_interval = sync_interval
self.instance_uuid = str(uuid4())
self.request_counter = RequestCounter()
self.validation_error_counter = ValidationErrorCounter()
Expand All @@ -82,6 +83,12 @@ def get_instance(cls: Type[TApitallyClient]) -> TApitallyClient:
raise RuntimeError("Apitally client not initialized") # pragma: no cover
return cast(TApitallyClient, cls._instance)

@property
def sync_interval(self) -> float:
return (
SYNC_INTERVAL if time.time() - self._started_at > INITIAL_SYNC_INTERVAL_DURATION else INITIAL_SYNC_INTERVAL
)

@property
def hub_url(self) -> str:
return f"{HUB_BASE_URL}/{HUB_VERSION}/{self.client_id}/{self.env}"
Expand Down
2 changes: 0 additions & 2 deletions apitally/client/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ def __init__(
client_id: str,
env: str,
sync_api_keys: bool = False,
sync_interval: float = 60,
key_cache_class: Optional[Type[ApitallyKeyCacheBase]] = None,
) -> None:
super().__init__(
client_id=client_id,
env=env,
sync_api_keys=sync_api_keys,
sync_interval=sync_interval,
key_cache_class=key_cache_class,
)
self._thread: Optional[Thread] = None
Expand Down
Loading

0 comments on commit 3fd00ef

Please sign in to comment.