DistributedDebounceThrottle
is a .NET library designed to facilitate debounce and throttle mechanisms in distributed system environments, leveraging Redis for state management and distributed locking. This ensures that function executions are properly debounced or throttled across multiple instances, preventing excessive or unintended operations.
- Debounce: Ensures a function is executed only once after a specified interval since the last call, useful for minimizing redundant operations over a period.
- Throttle: Limits the execution frequency of a function, ensuring it's not executed more than once within a specified timeframe.
- Distributed Locks: Implements the RedLock algorithm for distributed locking to coordinate debounce and throttle logic across distributed systems.
- Redis Integration: Utilizes Redis for managing timestamps and locks, offering a scalable solution for state synchronization.
Install DistributedDebounceThrottle
via NuGet:
dotnet add package DistributedDebounceThrottle
To integrate DistributedDebounceThrottle
in your application, ensure you have a Redis instance ready for connection. Here's how to get started:
In your application's startup configuration, register DistributedDebounceThrottle:
public void ConfigureServices(IServiceCollection services)
{
// Using an existing IConnectionMultiplexer instance:
services.AddDistributedDebounceThrottle(settings);
// Or, initiating a new IConnectionMultiplexer with a connection string:
services.AddDistributedDebounceThrottle(redisConnectionString, settings);
}
Inject IDebounceThrottle
to access debounce and throttle dispatchers:
public class SampleService
{
private readonly IDispatcher _throttler;
public SampleService(IDebounceThrottle debounceThrottle)
{
_throttler = debounceThrottle.ThrottleDispatcher("uniqueDispatcherId", TimeSpan.FromSeconds(5));
}
public Task ExecuteThrottledOperation()
{
return _throttler.DispatchAsync(async () =>
{
// Operation logic here.
});
}
}
Customize your debounce and throttle settings via DebounceThrottleSettings
:
RedisKeysPrefix
: A prefix for all Redis keys (default "debounce-throttle:").RedLockExpiryTime
: The expiry time for the distributed locks (default TimeSpan.FromSeconds(10)).
Contributions are welcome! Feel free to fork the repository, submit pull requests, or report issues for bugs, features, or documentation improvements.
This project is licensed under the MIT License - see the LICENSE file in the repository for details.