-
Notifications
You must be signed in to change notification settings - Fork 78
feat: expose available needed by chatsdk #3491
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
base: master
Are you sure you want to change the base?
Conversation
|
You can find the image built from this PR at Built from abc3fa8 |
Ivansete-status
left a comment
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.
LGTM! Thanks for it! 💯
NagyZoltanPeter
left a comment
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.
LGTM, just added some comment to consider.
| updateStrict(bucket, currentTime) | ||
|
|
||
| ## Returns the available capacity ratio of the bucket: 0.0 (empty); 1.0 (full) | ||
| proc getAvailableCapacityRatio*(bucket: TokenBucket, currentTime: Moment): float = |
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.
| proc getAvailableCapacityRatio*(bucket: TokenBucket, currentTime: Moment): float = | |
| proc getAvailableCapacityRatio*(bucket: TokenBucket, currentTime = Moment.now()): float = |
Might ease the use with defaulting to now() as most of the time that's one in question. WDYT?
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.
yes
| proc getAvailableCapacityRatio*(bucket: TokenBucket, currentTime: Moment): float = | ||
| if periodElapsed(bucket, currentTime): | ||
| return 1.0 | ||
| return bucket.budget.float / bucket.budgetCap.float |
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.
Mind that this version of TokenBucket introduces two modes.
The simple one called strict which behaves as expected, calculates buget use absolute to time period.
The other one can take consideration to refil the bugdet over the capacity based on previous usage.
My guess this calculation will work in case of strict mode, otherwise you can get > 1.0 results.
It's up to you if you would like to manage this case here or not.
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.
true
|
@plopezlpz what is the intent here? Is the idea to use this as part of the rate limit manager solution? In this case, I would suggest that we fully extract the code you need in its own library. cc @NagyZoltanPeter (doesn't have to be straight away, but end goal should be clear). |
in the other PR @Ivansete-status suggested using a similar approach as this, since we will need nwaku as dependency I thought of using it directly I just needed this method to calculate the remaining balance. About extracting it, it would be a small one file library, I don't know about it |
Ivansete-status
left a comment
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.
This PR is correct and I don't see any harm in exposing the available capacity.
Just adding an additional nitpick comment but I think we can merge that :)
I agree, when was making DOS protection and found TockenBucket living in nim-libp2p, that would have been the good choice is to extract it into a separate high level utility library. @fryorcraken: Is that what you mean? |
Yes, If we are re-using the same piece of code in now 3 places (nim-libp2p, nwaku and chat sdk) then it would be best to have one separate library that can be imported in all 3 projects. |
|
Wouldn't it be better to apply the necessary fixes to nim-chronos first, and then only use it? |
What does "necessary fixes" mean? I assume that But this is FOSS, I see 2 valid paths ahead:
|
I would go with 2., but 2. would be a really small library, I'll create the repo and see, just a copy/paste |
Yes, I came back here for that. What are we talking about here? it's 140 lines of code. I don't see a point in blocking @plopezlpz to try doing some code communism here across 3 different layers of the stack. Up to you @plopezlpz, my recommendation: if you can use chronos' out of the box, go for it. |
a well-argued improvement to chronos would certainly be considered - at the end of the day, a token bucket is a well-described algorithm in literature and if chronos' implementation deviates from that, it's certainly welcome to improve it. On the other hand, if the implementation here deviates from literature, it's certainly a good question to ask whether it's due to a well-understood reason or if it's a local decision that later might come back to haunt us. |
5118fc3 to
08d14fb
Compare
Description
Exposing available capacity
Changes
How to test
Unit tests included, no further testing
closes #3497