[12.x] Adds callback debounce to Rate Limiter. #58237
Closed
+81
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
This introduces the
attemptOnceto the Rate Limiter helper, which is just syntax sugar forattempt()with 1 retry while being atomic without the need of atomic locks. It uses the same syntax style thatattempt(), with a 60 second default.The idea is to tun a callback only once inside a given window of time, sharing it execution state across multiple app instances and checkable across the whole app. This also allows to clear the debounce data manually or on failure.
When the debounced callback is executed, its original result will be returned, otherwise
falseis returned.Since it uses the Rate Limiter, the status of the key can be checked elsewhere.
Caveats
This is made to avoid serializing a result, or making the developer mistakenly not returning anything (
nullofvoid. I assume the developer will also want the fresh result of the callback instead of stale data, otherwise he would useCache::remember()or have more control with an Atomic Lock.If the callback fails, the attempt is cleared. If the developer requires to not do so, catching the exception inside the function is required, like using with
rescue().