Documentation
¶
Index ¶
- Constants
- Variables
- func CreateTable(ctx context.Context, tableName string, client *dynamodb.Client) error
- type AdditionalConfig
- type AnonymousConfig
- type Config
- type DDBClient
- type Decision
- type LimitPreparer
- type Limiter
- type NegativeCacheConfig
- type Opt
- type Preparer
- type RateLimit
- type RegistrationCacheConfig
- type Revoker
Constants ¶
const DefaultInterval = 60 * time.Second
DefaultInterval is used when Config.Interval is zero.
Variables ¶
var ( // ErrStoreThrottled means DynamoDB throttled the request. It is a server side capacity problem, // not that the caller exceeded its rate limit. The caller's rate limit is reported by Decision.Allowed. ErrStoreThrottled = errors.New("dynamodb throttled the request") // ErrInternal means DynamoDB or the limiter failed unexpectedly. ErrInternal = errors.New("internal error") // ErrInvalidBucketID means the bucket id is empty, too long or not registered. ErrInvalidBucketID = errors.New("invalid bucket id") )
Functions ¶
Types ¶
type AdditionalConfig ¶
type AdditionalConfig struct {
NegativeCache *NegativeCacheConfig `json:"negative_cache,omitempty"`
// RegistrationCache caches registered bucket ids. Revoked ids are allowed until the entry expires.
RegistrationCache *RegistrationCacheConfig `json:"registration_cache,omitempty"`
Anonymous *AnonymousConfig `json:"anonymous,omitempty"`
// FailOpen allows requests when DynamoDB fails. Requests are denied by default (fail-closed).
FailOpen bool `json:"fail_open,omitempty"`
}
type AnonymousConfig ¶
AnonymousConfig allows not registered bucket id. This is used for such as an IP address basis throttling.
type Config ¶
type Config struct {
TableName string `json:"table_name"`
TokenPerInterval int64 `json:"token_per_interval"`
BucketSize int64 `json:"bucket_size"`
Interval time.Duration `json:"interval,omitempty"`
// Shards is the number of shards per bucket. It is computed from the worst requests per second when zero.
// Set it explicitly when the expected traffic per bucket id is known.
// It can be changed at any time; shard items are created on demand.
Shards int64 `json:"shards,omitempty"`
AdditionalConfig *AdditionalConfig `json:"additional,omitempty"`
}
type DDBClient ¶
type DDBClient interface {
GetItem(context.Context, *dynamodb.GetItemInput, ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
UpdateItem(context.Context, *dynamodb.UpdateItemInput, ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
BatchWriteItem(context.Context, *dynamodb.BatchWriteItemInput, ...func(*dynamodb.Options)) (*dynamodb.BatchWriteItemOutput, error)
DeleteItem(context.Context, *dynamodb.DeleteItemInput, ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
}
DDBClient is the subset of *dynamodb.Client used by the limiter.
type Decision ¶
type Decision struct {
// Allowed reports whether the request is within the rate limit.
Allowed bool
// Remaining is the estimated number of requests that can be allowed immediately.
// It is the value of the selected shard.
Remaining int64
// RetryAfter is how long to wait until a request is allowed. Zero when Allowed.
RetryAfter time.Duration
}
Decision is the result of Allow.
type LimitPreparer ¶
type NegativeCacheConfig ¶
NegativeCacheConfig configures negative cache for invalid bucket ids. Size is the maximum number of entries. specifying 0 means to disable cache. TTL is how long an entry is kept. 5 minutes by default.
type Opt ¶
type Opt func(rl *RateLimit)
Opt configures RateLimit.
func WithFailOpen ¶
func WithFailOpen() Opt
WithFailOpen allows requests when DynamoDB fails. The error is still returned.
func WithLogger ¶
WithLogger sets a logger. Logs are discarded by default.
func WithMeterProvider ¶
func WithMeterProvider(mp metric.MeterProvider) Opt
WithMeterProvider sets the OpenTelemetry MeterProvider. The global MeterProvider is used by default, which is a no-op until an OpenTelemetry SDK is configured.
type RateLimit ¶
type RateLimit struct {
// contains filtered or unexported fields
}
func (*RateLimit) Allow ¶
Allow consumes a token of the bucket.
A sharded bucket tries shards in random order. Shards known to be exhausted (the local deny cache) are skipped, and a shard denied by DynamoDB is cached and the next shard is tried, up to maxShardTries writes. Without this fallback a request could be denied while other shards still have tokens.
func (*RateLimit) PrepareTokens ¶
PrepareTokens registers the bucket id with a full bucket. An existing bucket is reset.
func (*RateLimit) RevokeTokens ¶
RevokeTokens deletes the registration of the bucket id. Other instances keep allowing it until their registration cache entries expire (additional.registration_cache.ttl). Shard items expire by TTL. Revoking an unregistered id is not an error.
type RegistrationCacheConfig ¶
type RegistrationCacheConfig struct {
Size int `json:"size"`
TTL time.Duration `json:"ttl,omitempty"`
}
RegistrationCacheConfig configures the cache of registered bucket ids. Size is the maximum number of entries. specifying 0 means to disable cache (a read per request). TTL is how long an entry is kept. 1 minute by default.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package limiterhttp provides net/http middleware for the limiter.
|
Package limiterhttp provides net/http middleware for the limiter. |
|
Package mock_limiter is a generated GoMock package.
|
Package mock_limiter is a generated GoMock package. |