package
Version:
v0.0.0-...-fd9b66f
Opens a new window with list of versions in this module.
Published: Nov 3, 2019
License: Apache-2.0
Opens a new window with license information.
Imports: 5
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
API Rate / Quota Limiter Middleware
This package provides a simple and minimalist implementation of API rate / quota request limiter within a http middleware.
How To Use
-
Implement APIQuotaInterface
in your package
type APIQuotaInterface interface {
// Parse token from http request.
ParseTokenFromRequest(ctx context.Context, r *http.Request) (string, error)
// Check current token's quota plan.
GetTokenQuotaPlan(ctx context.Context, token string) (int64, error)
}
-
Initiate package (as singleton)
type APIQuotaOption struct {
KeyPrefix string
Cache *redis.Pool
Logger *logrus.Logger
Interface APIQuotaInterface
}
// Call this once.
quota.Init(APIQuotaOption{
KeyPrefix: "foo",
Cache: cachePool,
Logger: logrus.New(),
Interface: customAPIQuotaImplementation{}, // Use struct defined from 1st step.
})
NOTE
- Ideally
GetTokenQuotaPlan
is implemented by selecting into a database.
- Expiration / validity of the API token should be managed yourself (i.e. create custom cron to set / expire tokens).
Example
Example can be found in the test file: quota_test.go
.
Documentation
¶
Get cache key name on given api key.
HTTP Middleware for api quota request limiter.
Initiate package's variables and dependencies.
API Quota middleware interface definition.
API Quota middleware package initiation options.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.