-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support float ttl #107
Support float ttl #107
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #107 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 1775 1785 +10
=========================================
+ Hits 1775 1785 +10 ☔ View full report in Codecov by Sentry. |
Hey! Thanks for assigning this one to me for review. I have a gut-check question for you though - do we need this feature? Other than running tests faster, I can't think of any possible use requiring If the goal is to speed up the tests (which is a totally valid reason), then I think the easier solution would be to build off the from ._utils import BaseClock
class MockClock(BaseClock):
def __init__(self):
self.time = 0
def now(self) -> int:
return self.time
def advance(self, increment: int) -> None:
self.time += increment And then in testing, just add a call to |
The idea that we can inject clocks into the storage objects sounds really good, although I think the current implementation needs to be added, at least because float timeouts are more user-friendly and seem more natural than integer ones. There are also some complexities in the solution where we should inject clock objects. That is, when we use Here is the code that handles hishel/hishel/_async/_storages.py Lines 287 to 291 in a6b4c65
We simply use the |
Understood. I still think we're going to have a problem with using For SQLite, the For Redis, the For either, the best precision you can get is millisecond precision, so So, I think what we need to do is keep TTL an |
We can use SQLite REAL to work with the Unix times, as I have done in this PR. Yes, Redis represents timeouts as integer values, so I have created a simple float_seconds_to_int_milliseconds function that we can always use for the PX option that Redis provides us. This PR is a huge performance improvement for the tests.
This PR doesn't break the backward compatability. It just adds a float-type support for ttl options, which I think is a far simpler solution than introducing a new argument special for millisecond ttls. It's just not going to work as expected if the user has the sqlite cache because this PR has changed the table schema for sqlite tables, so we can mention it in the release notes. I think it's a good trade-off. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good, I don't have any further comments on the code itself. I still question the wisdom of using float
values for what are int
s, and don't love adding features for testing purposes. But I feel like I've been heard, and I'm happy to go along with this compromise.
This pull request adds float
ttl
support for all kinds of storage.Also, floating
ttl
support means that now we can use tiny sleeps for storage ttl tests, which improves the tests performance.For
SQLite
, I switched thedate_created
field fromdatetime
toreal
in order to compare Unix times.For
Redis
, I have used the PX option, which can set the timeout using miliseconds instead of seconds.For
FileSystem
, no changes were made becausest_mtime
is a floating number.