-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache certain GitHub requests (#381)
* Cache certain GitHub requests To avoid GH rate limit issues with big big clients we can cache some of our more used requests that are unlikely to change in a short period of time. This work was done initially on PR #379 by @trent-codecov. The present changes are an attempt to integrate the changes better into our existing code. In oarticular we wanted to use `OurOwnCache`, but share it for torngit instances. I thought it was better to have a shared `TorngitCache` among all the torngit instances from a process. This is not a problem, as the configuration of the cache doesn't change within a process, and so `TorngitCache` was born. It is configured (for real) by the first instance, but every instance calls the function (that then does nothing). The more troublesome portion was that the original PR had logging for cache hits. `OurOwnCache` is crystal clear fully transparent, and it's very versatile as well, so it was a bit tricky to get info from it while making sure we don't log dangerous things. The solution I landed on is to let each cached function specfity if the cache should log hits, and in this case what arguments from the function should be called. This is as good a logging as we can get, I believe. Notice from the tests `shared/tests/unit/helpers/test_cache.py::TestCache::test_simple_caching_fake_backend_with_params` that there is a difference if you use args or kwargs. And the cache understands this difference as 2 separate func calls (so it becomes 2 different keys). There's a difference in logging as well. In any case this work was a bit rushed, and that was the best balance I could get to. If you think this is a bit overkill, well I thought so to, but it's not in a bad shape. And it's very easy to use. No Tickets associated * Make `init_torngit_cache` a method of the singleton. Moving logic from `init_torngit_cache` to the `TorngitCache` so it's a method of the singleton. The new method is `initialize`.
- Loading branch information
1 parent
20f2646
commit 3565648
Showing
9 changed files
with
283 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,5 +36,7 @@ | |
"httpx>=0.23.0", | ||
"oauthlib", | ||
"redis", | ||
"typing", | ||
"typing_extensions", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import Union | ||
|
||
from redis import Redis | ||
from typing_extensions import Literal | ||
|
||
from shared.config import get_config | ||
from shared.helpers.cache import OurOwnCache, RedisBackend | ||
|
||
|
||
def get_redis_url() -> str: | ||
url = get_config("services", "redis_url") | ||
if url is not None: | ||
return url | ||
hostname = "redis" | ||
port = 6379 | ||
return f"redis://{hostname}:{port}" | ||
|
||
|
||
CachedEndpoint = Union[Literal["check"], Literal["compare"], Literal["status"]] | ||
|
||
|
||
class TorngitCache(OurOwnCache): | ||
def __init__(self): | ||
super().__init__() | ||
self.ttls = {} | ||
self._initialized = False | ||
self._enabled = False | ||
|
||
def initialize(self) -> None: | ||
"""Initializes and configures the TorngitCache according to config.""" | ||
if self.is_initialized: | ||
return | ||
use_cache = get_config("services", "vcs_cache", "enabled", default=False) | ||
if use_cache: | ||
redis = Redis.from_url(get_redis_url()) | ||
backend = RedisBackend(redis_connection=redis) | ||
app = get_config("services", "vcs_cache", "metrics_app", default=None) | ||
self.configure(backend=backend, app=app) | ||
self._enabled = True | ||
ttls = { | ||
"check": get_config("services", "vcs_cache", "check_duration", default=120), | ||
"compare": get_config( | ||
"services", "vcs_cache", "compare_duration", default=120 | ||
), | ||
"status": get_config( | ||
"services", "vcs_cache", "status_duration", default=120 | ||
), | ||
} | ||
self.ttls = ttls | ||
self._initialized = True | ||
|
||
@property | ||
def is_initialized(self) -> bool: | ||
return self._initialized | ||
|
||
@property | ||
def is_enabled(self) -> bool: | ||
return self._enabled | ||
|
||
def get_ttl(self, endpoint: CachedEndpoint) -> dict: | ||
return self.ttls.get(endpoint, 120) | ||
|
||
|
||
# This instance is shared among all the Torngit instances created on the same process. | ||
# It doesn't matter because the cache is distributed and configuration can't change | ||
# between these instances, so it's ok to share the same cache. | ||
torngit_cache = TorngitCache() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.