limit

package module
Version: v0.0.0-...-2cb72c1 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 11 Imported by: 1

README

bulrush-limit

app.Use(&limit.Limit{
    Frequency: &limit.Frequency{
        Passages: []string{},
        Rules: []limit.Rule{
            limit.Rule{
                Methods: []string{"GET"},
                Match:   "/api/v1/user*",
                Rate:    1,
            },
            limit.Rule{
                Methods: []string{"GET"},
                Match:   "/api/v1/role*",
                Rate:    2,
            },
        },
        // Provides default redis storage, so time >=1s
        Model: &limit.RedisModel{
            Redis: addition.Redis,
        },
    },
})

MIT License

Copyright (c) 2018-2020 Double

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandler

type ErrorHandler func(ctx *gin.Context)

ErrorHandler handler error

var DefaultFailureHandler ErrorHandler = func(ctx *gin.Context) {
	rushLogger.Error("rate limited access, pease check again later")
	ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"message": "rate limited access, pease check again later"})
}

DefaultFailureHandler default error handler

type Frequency

type Frequency struct {
	Passages       []string
	Rules          []Rule
	Model          Model
	FailureHandler ErrorHandler
}

Frequency limit

type Limit

type Limit struct {
	Frequency *Frequency
}

Limit plugin

func New

func New() *Limit

New defined new limit

func (*Limit) AddOptions

func (l *Limit) AddOptions(opts ...Option) *Limit

AddOptions defined add option

func (*Limit) Plugin

func (l *Limit) Plugin(router *gin.RouterGroup)

Plugin for Limit

type Model

type Model interface {
	Save(string, string, string, int)
	Find(string, string, string, int) interface{}
}

Model token store

type Option

type Option func(*Limit) interface{}

Option defined implement of option

type RedisModel

type RedisModel struct {
	Model
	Redis *redisext.Redis
}

RedisModel adapter for redis

func (*RedisModel) Find

func (model *RedisModel) Find(ip string, url string, method string, rate int) interface{}

Find find a token

func (*RedisModel) Save

func (model *RedisModel) Save(ip string, url string, method string, rate int)

Save save a token

type Rule

type Rule struct {
	Methods []string
	Match   string
	Rate    int
}

Rule defined

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL