-
Notifications
You must be signed in to change notification settings - Fork 200
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
Cannot create a RedisDistributedSemaphore with extensionCadence set to Timeout.InfiniteTimeSpan #130
Comments
@LinkForce thanks for reporting. This does seem like a bug. Out of curiosity why do you want to disable auto-extension? |
I want to have a Semaphore to limit some operation to the maximum of 25 operations per second, but the thing is it does not matter if the operation finishes or not, I just need to limit that no more than 25 operations starts in the same second window. |
I see thanks for the context. The way the library works it will auto-release the semaphore if the handle gets garbage collected. So the way I’d recommend doing this is:
You should still set the overall timeout like you’re doing, but you can skip disabling extension. Just to be 100% clear:
|
@madelson Continuing from the other issue: |
@Skier23 ok. Just want to confirm that you're aware that if you let the handle be GC'd it will be released before the expiry. E.g. you can't do something like this:
I'm curious what coding pattern you are planning on using and how you expect it to behave. For example I could imagine doing something like this:
|
yes. I have teams that are doing something like I know this isn't good practice and I'm trying to get them away from doing it like this but that's how some teams are doing it now. |
@Skier23 is the code you show using the |
Its currently using another implementation. |
I finally had some time to look into this in more depth. I'm coming around to the opinion that the thing I want to change here is the misleading error message that implies that one should be allowed to set ExtensionCadence to infinite. This error message appears to have been copied from
In order to support disabling extension, we'd need to actually do quite a bit more than just change the one check. We'd have to:
This wouldn't be that big a deal if the use-case was really strong, but I still don't feel like I see it. For @LinkForce's use-case, the error actually stopped them from accidentally writing broken code. If this change were implemented, the code would have failed to do the right thing in a silent and subtle way rather than failing fast. For @Skier23's use-case it feels like we're just trying to support a known-to-be-bad pattern, when the default behavior of this library makes it trivial to support a very robust pattern instead. Furthermore, even as-written the library does indirectly support the pattern of using the semaphore as a time-based rate-limiter rather than a true semaphore. This can be done simply with:
This can easily be wrapped up in a utility method. If this use-case of "distributed rate-limiter" is a popular one, I wonder if it merits consideration for a dedicated API in the library.? For example, we could have something like this:
Thoughts? I really appreciate all the engagement here! |
Yes, I agree that it would be better to fix the error message instead of allowing Timeout.InfiniteTimeSpan. |
I am trying to instantiate a RedisDistributedSemaphore that doesn't auto extends the locks, and according to the source code, I should set extensionCadence set Timeout.InfiniteTimeSpan. But when the RedisDistributedSyncronizationOptions is built, I get an ArgumentOutOfRangeException stating that "extensionCadence must be less than expiry". I believe this is not right, as Timeout.InfiniteTimeSpan is less than the default expiry.
The text was updated successfully, but these errors were encountered: