Documentation ¶
Index ¶
- func CacheHandler(fn http.Handler) http.Handler
- func CommonContentType(ext string) (string, bool)
- func GetRequestIP(r *http.Request) (ip string, err error)
- func ServeCustomJSONError(log *zap.Logger, w http.ResponseWriter, status int, err error, msg string)
- func ServeJSONError(log *zap.Logger, w http.ResponseWriter, status int, err error)
- type RateLimiter
- type RateLimiterConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CacheHandler ¶ added in v1.51.1
CacheHandler is a middleware for caching static files for 1 year.
func CommonContentType ¶ added in v1.51.1
CommonContentType returns content-type for common extensions, ignoring OS settings.
func GetRequestIP ¶ added in v1.34.1
GetRequestIP gets the original IP address of the request by handling the request headers.
func ServeCustomJSONError ¶ added in v1.69.1
func ServeCustomJSONError(log *zap.Logger, w http.ResponseWriter, status int, err error, msg string)
ServeCustomJSONError writes a JSON error with a custom message to the response output stream.
func ServeJSONError ¶ added in v1.69.1
ServeJSONError writes a JSON error to the response output stream.
Types ¶
type RateLimiter ¶ added in v1.39.4
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter imposes a rate limit per key.
func NewIPRateLimiter ¶
func NewIPRateLimiter(config RateLimiterConfig, log *zap.Logger) *RateLimiter
NewIPRateLimiter constructs a RateLimiter that limits based on IP address.
func NewRateLimiter ¶ added in v1.39.4
func NewRateLimiter(config RateLimiterConfig, log *zap.Logger, keyFunc func(*http.Request) (string, error)) *RateLimiter
NewRateLimiter constructs a RateLimiter.
func (*RateLimiter) Burst ¶ added in v1.39.4
func (rl *RateLimiter) Burst() int
Burst returns the number of events that happen before the rate limit.
func (*RateLimiter) Duration ¶ added in v1.39.4
func (rl *RateLimiter) Duration() time.Duration
Duration returns the amount of time required between events.
func (*RateLimiter) Limit ¶ added in v1.39.4
func (rl *RateLimiter) Limit(next http.Handler) http.Handler
Limit applies per-key rate limiting as an HTTP Handler.
func (*RateLimiter) Run ¶ added in v1.39.4
func (rl *RateLimiter) Run(ctx context.Context)
Run occasionally cleans old rate-limiting data, until context cancel.
type RateLimiterConfig ¶ added in v1.39.4
type RateLimiterConfig struct { Duration time.Duration `help:"the rate at which request are allowed" default:"5m"` Burst int `help:"number of events before the limit kicks in" default:"5" testDefault:"3"` NumLimits int `help:"number of clients whose rate limits we store" default:"1000" testDefault:"10"` }
RateLimiterConfig configures a RateLimiter.