RateLimitMiddleware
extends AbstractMiddleware
in package
Middleware for handling Canvas API rate limits using a leaky bucket algorithm
Table of Contents
Properties
- $config : array<string, mixed>
- $buckets : array<string, array{remaining: int, cost: int, timestamp: float}>
Methods
- __construct() : mixed
- __invoke() : callable
- Get the Guzzle middleware callable
- configure() : void
- Set configuration for the middleware
- getName() : string
- Get the name of the middleware
- resetBuckets() : void
- Reset rate limit buckets (useful for testing)
- getConfig() : mixed
- Get a configuration value
- getDefaultConfig() : array<string, mixed>
- Get default configuration for the middleware
- calculateDelay() : int
- Calculate delay needed before making a request
- consumeFromBucket() : void
- Consume units from the bucket
- getBucket() : array{remaining: int, cost: int, timestamp: float}
- Get or initialize a bucket
- isRateLimitError() : bool
- Check if an error is a rate limit error
- refundToBucket() : void
- Refund units to the bucket
- updateBucketFromResponse() : void
- Update bucket state from Canvas response headers
Properties
$config
protected
array<string, mixed>
$config
= []
$buckets
private
static array<string, array{remaining: int, cost: int, timestamp: float}>
$buckets
= []
Methods
__construct()
public
__construct([array<string, mixed> $config = [] ]) : mixed
Parameters
- $config : array<string, mixed> = []
__invoke()
Get the Guzzle middleware callable
public
__invoke() : callable
Tags
Return values
callableconfigure()
Set configuration for the middleware
public
configure(array<string|int, mixed> $config) : void
Parameters
- $config : array<string|int, mixed>
Tags
getName()
Get the name of the middleware
public
getName() : string
Tags
Return values
stringresetBuckets()
Reset rate limit buckets (useful for testing)
public
static resetBuckets([string|null $bucketKey = null ]) : void
Parameters
- $bucketKey : string|null = null
-
Specific bucket to reset, or null for all
getConfig()
Get a configuration value
protected
getConfig(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
- $default : mixed = null
getDefaultConfig()
Get default configuration for the middleware
protected
getDefaultConfig() : array<string, mixed>
Tags
Return values
array<string, mixed>calculateDelay()
Calculate delay needed before making a request
private
calculateDelay(string $bucketKey) : int
Parameters
- $bucketKey : string
Return values
int —Delay in seconds
consumeFromBucket()
Consume units from the bucket
private
consumeFromBucket(string $bucketKey, int $cost) : void
Parameters
- $bucketKey : string
- $cost : int
getBucket()
Get or initialize a bucket
private
getBucket(string $bucketKey) : array{remaining: int, cost: int, timestamp: float}
Parameters
- $bucketKey : string
Return values
array{remaining: int, cost: int, timestamp: float}isRateLimitError()
Check if an error is a rate limit error
private
isRateLimitError(mixed $reason) : bool
Parameters
- $reason : mixed
Return values
boolrefundToBucket()
Refund units to the bucket
private
refundToBucket(string $bucketKey, int $cost) : void
Parameters
- $bucketKey : string
- $cost : int
updateBucketFromResponse()
Update bucket state from Canvas response headers
private
updateBucketFromResponse(string $bucketKey, ResponseInterface $response) : void
Parameters
- $bucketKey : string
- $response : ResponseInterface