Skip to content
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

Jonathan Beck #249

Closed
Closed

Conversation

C0D3Name
Copy link

@C0D3Name C0D3Name commented Nov 7, 2024

Rate-limiting Solution - Updated

Problem Statement:

  • Design a resource rate limiting configuration framework with the following goals and non-goals:

Goals:

  • Configurability:
    • The framework should allow for the configuration of rate limiting rules on multiple resources.
  • Composability:
    • The framework should be capable of applying multiple rate limiting rules to a single resource.
  • Extensibility:
    • The framework should support the creation of new rate limiting rules in the future.

Non-Goals:

  • Do not create a sophisticated rate limiting algorithm, the focus is on managing rate limiting rules on specific resources.

Implementation Details:

Core Functionality

The solution implements Limiters that provide Leases to requesting Resources. Limiters are configured through a common LimiterConfig file that contains all the configuration properties that can be applied for all limiters. It is the responsibilty of each limiter to ensure the proper configuration is set in this class for the given limiter type.

Leases are produced when a resource attempts to utilize a limiter. The lease may be acquired or denied based on the current state of the limiter. Leases are configured using the LeaseConfig class. For example, the TokenLimiter class uses LeaseConfig to enable requesting a lease with a variable amount of tokens.

Relinquishing a lease releases the limiter according to the limiter implementation.

Linked Limiters

It is possible to compose limiter rules using the LinkedLimiter class.

  • Any combination of limiters can be created, including chaining multiple limiters of the same type together.
    • Example: Rule A + Rule B + Rule C + Rule A is a supported use case for a linked limiter.
  • When acquiring a lease, all linked limiters must succeed, or the lease acquisition fails as a unit.
  • Nesting of linked limiters is not currently supported, but could be with an enhancement to the linked limiter.
    • Example: Rule A + (Rule B + Rule C) + Rule D is not currently supported

Example Limiters

Two example limiters have been created: TokenLimiter and FixedWindowLimiter.

TokenLimiter

  • Sets a maximum number of tokens that can be utilized at one time.
  • Provids the ability to configure the number of tokens each resource acquires for a given lease.
  • As leases are acquired, the number of tokens utilized is incremented.
  • As leases are released, the number of tokens utilized is decremented.

FixedWindowLimiter

  • A simple limiter which takes the window duration in seconds and the maximum number of resources that can utilize the limiter for the given window
  • The number of uses increases for each successful lease and is only reset when the window's elapsed time causes a token refresh.

JSON-style resource configuration

ResourceRateLimiter supports (but does not require) JSON-style resource configuration to enable dynamic configuration of rules and resources through JSON hosted in an external source (API Project AppSettings.json file, JSON-based configuration data store, etc.)

A functional sample configuration file has also been provided in SampleResourceRateLimiterConfig.json

  • Note - A unit test was created in ResourceRateLimiter.Tests that utilizes this json file for demonstration purposes.

Potential Future Enhancements:

  1. Support nested linked limiters to support additional combinations of limiters.
    Example: Rule A + Rule B + (Rules A + B) + ((Rules A + B) + (Rules B + C)).
  2. Support for asynchronous calls.
  3. Support for resource queuing.

…ateLimiter and related classes.

Updated README.md with detailed rate-limiting solution explanation and instructions. Added System.Threading.RateLimiting NuGet package references in RateLimiter.Tests.csproj and RateLimiter.csproj. Removed obsolete RateLimiterTest.cs. Updated RateLimiter.sln for Visual Studio 17 and added SampleResourceRateLimiterConfig.json. Introduced new test class RandomRateLimiterTests.cs.

Added ResourceRateLimiterTests.cs to test ResourceRateLimiter functionality. Introduced RateLimiterConfig and ResourceLimitConfig classes for JSON-based rate limiter configurations. Enhanced ResourceRateLimiter to manage resource-specific rate limiters with error handling.

Implemented custom RandomRateLimiter in RateLimiter.CustomLimiters namespace, including RandomRateLimiterLease class. Added methods for acquiring permits and handling rate limiter statistics.
…with custom implementation that does not rely on any existing framework resource.

The new implementation supports rate limiting for resources including limiter composition via the LinkedLimiter class.

Two sample limiters have been created to demonstrate the functionality: a token based limiter and a fixed window limiter.

All related tests, documentation, and sample data files have been updated to reflect the changes.
@C0D3Name C0D3Name closed this by deleting the head repository Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant