ratelimiter

package
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LIMITER = func(next http.Handler) http.Handler {
	var limiter = rate.NewLimiter(1, LIMITER_TOKENS)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		v, ok := banned.Load(r.RemoteAddr)
		if ok {
			if time.Since(v.(time.Time)) > LIMITER_TIMEOUT {
				banned.Delete(r.RemoteAddr)
			} else {
				w.WriteHeader(http.StatusTooManyRequests)
				w.Write([]byte("<h1>YOU DID TOO MANY REQUEST, YOU HAVE BEEN BANNED FOR 5 MINUTES </h1>"))
				banned.Store(r.RemoteAddr, time.Now())
				return
			}
		}
		if !limiter.Allow() {
			w.WriteHeader(http.StatusTooManyRequests)
			w.Write([]byte("<h1>YOU DID TOO MANY REQUEST, YOU HAVE BEEN BANNED FOR 5 MINUTES </h1>"))
			banned.Store(r.RemoteAddr, time.Now())
			return
		}
		next.ServeHTTP(w, r)
	})
}
View Source
var LIMITER_TIMEOUT = 5 * time.Minute
View Source
var LIMITER_TOKENS = 50

Functions

This section is empty.

Types

type IPRateLimiter

type IPRateLimiter struct {
	// contains filtered or unexported fields
}

func NewIPRateLimiter

func NewIPRateLimiter(r rate.Limit, b int) *IPRateLimiter

func (*IPRateLimiter) AddIP

func (i *IPRateLimiter) AddIP(ip string) *rate.Limiter

AddIP creates a new rate limiter and adds it to the ips map, using the IP address as the key

func (*IPRateLimiter) GetLimiter

func (i *IPRateLimiter) GetLimiter(ip string) *rate.Limiter

GetLimiter returns the rate limiter for the provided IP address if it exists. Otherwise calls AddIP to add IP address to the map

Jump to

Keyboard shortcuts

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