limiter

package module
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2021 License: MIT Imports: 8 Imported by: 1

README

Limiter

PkgGoDev

A useful gas that used to limit every request for the web applications built using Air.

Installation

Open your terminal and execute

$ go get github.com/air-gases/limiter

done.

The only requirement is the Go, at least v1.13.

Usage

The following application will limit the body size of all requests to within 1 MB.

package main

import (
	"github.com/air-gases/limiter"
	"github.com/aofei/air"
)

func main() {
	a := air.Default
	a.DebugMode = true
	a.Pregases = []air.Gas{
		limiter.BodySizeGas(limiter.BodySizeGasConfig{
			MaxBytes: 1 << 20,
		}),
	}
	a.GET("/", func(req *air.Request, res *air.Response) error {
		return res.WriteString("You are within the limits.")
	})
	a.Serve()
}

Community

If you want to discuss Limiter, or ask questions about it, simply post questions or ideas here.

Contributing

If you want to help build Limiter, simply follow this to send pull requests here.

License

This project is licensed under the MIT License.

License can be found here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BodySizeGas

func BodySizeGas(bsgc BodySizeGasConfig) air.Gas

BodySizeGas returns an `air.Gas` that is used to limit ervery request's body size based on the bsgc. It prevents clients from accidentally or maliciously sending a large request and wasting server resources.

func RateGas added in v0.11.2

func RateGas(rgc RateGasConfig) air.Gas

RateGas returns an `air.Gas` that is used to limit request's rate based on the rgc.

Types

type BodySizeGasConfig

type BodySizeGasConfig struct {
	MaxBytes                 int64
	ErrRequestEntityTooLarge error

	Skippable func(*air.Request, *air.Response) bool
}

BodySizeGasConfig is a set of configurations for the `BodySizeGas`.

type RateGasConfig added in v0.11.2

type RateGasConfig struct {
	MaxRequests        int64
	ResetInterval      time.Duration
	UseClientAddress   bool
	ErrTooManyRequests error

	Skippable func(*air.Request, *air.Response) bool
	// contains filtered or unexported fields
}

RateGasConfig is a set of configurations for the `RateGas`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL