Skip to content
A high-performance rate limiter written in GO language
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml
README.md
blocker.go
blocker_test.go
cleaner.go
parser_pattern_test.go
pattern_parser.go
rate_limit.go
rate_limit_test.go
spammer.go
spammer_test.go

README.md

RateLimit

A high-performance rate limiter written in GO language

License: MIT godoc Go Report Card Build Status

This package provides a Golang implementation of rate limit

Create a rate limiter with a maximum number of operations to perform per second. with the ability of mark as a spam then block for a specific period

Usage

package main

import (
	"fmt"

	"github.com/ahmedash95/ratelimit"
)

func main() {
    rt := ratelimit.CreateLimit("1r/s")
    user_ip := "127.0.0.1"
    rt.Hit(user_ip)

    fmt.Println(rt.Rates[user_ip].Hits)
    // out: 1
}

So now how you would block the user if he hits the request more than the limit

package main

import (
	"fmt"

	"github.com/ahmedash95/ratelimit"
)

func main() {
	rt := ratelimit.CreateLimit("1r/s")
	user_ip := "127.0.0.1"
	var err error
	err = rt.Hit(user_ip)
	err = rt.Hit(user_ip)

    if err != nil {
        fmt.Println(err)
        // out: The key [127.0.0.1] has reached max requests [1]

        // block process
    }
}

Define spam and block method

the way of how it works is 1r/s,spam:3,block:3d means that the rate limit is 1 request per second and we will mark it as a spammer if the key reach the max limit more than 3 times, if he, we will block the key for 3 days

package main

import (
	"fmt"
	"github.com/ahmedash95/ratelimit"
)

func main() {
	rt := ratelimit.CreateLimit("1r/s,spam:3,block:2d")
	user_ip := "127.0.0.1"
	rt.Hit(user_ip)
    rt.Hit(user_ip)

    fmt.Println(rt.Spammer.Values[user_ip].Hits)
    // out: 1
    // because he just hit the max requests for 1 time

    rt := ratelimit.CreateLimit("1r/s,spam:3,block:2d")
    user_ip := "127.0.0.1"
    rt.Hit(user_ip)
    rt.Hit(user_ip)

    time.Sleep(time.Second)
    rt.Hit(user_ip)
    rt.Hit(user_ip)

    time.Sleep(time.Second)

    rt.Hit(user_ip)
    rt.Hit(user_ip)

    fmt.Println(rt.Spammer.Values[user_ip].Hits)
    // out: 3
    // because he hit the max requests for 3 times

    _, blocked := rt.Blocker.Values[user_ip]
    fmt.Printf("%v\n", blocked)
    // out: True
}

Guide

Rate limit pattern definition

the default format is 1r/s which is mean 1 request per second or 3r/m which is mean 3 request per minute

The advanced Pattern that support spam and block

1r/s,spam:3,block:3h which is mean block the user for 3 hours when reatchs the maximum spam hits, block supports 3d 10h 5m 10s

Testing

$ go test

Credits

License

The MIT License (MIT). Please see License File for more information.

You can’t perform that action at this time.