Skip to content

Marvin9/ratelimit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ratelimit-go

Prerequisite

  • redis
go get github.com/go-redis/redis/v8
go get github.com/Marvin9/ratelimit
// Initialize

var redisClient = redis.NewClient(&redis.Options{Addr: "localhost:6379"})

// Eg. Consume API maximum 10 times in 15 seconds, reset usage after every 15 seconds
var rt = ratelimit.NewWindow(10, time.Second*15, redisClient)

// use it in middleware
func rateLimitMiddleware(next http.HandlerFunc) http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		ip := getIP(req)

		// use unique identifier, Consume API.
		profiler, canUseAPI := rt.Use(ip)

		log.Prtinf("\nFor %v: \n%v APIs left for usage.\nNext reset: %v", ip, profiler.APIUsageLeft, profiler.NextWindow)

		if !canUseAPI {
		    fmt.Fprintf(w, "API call Limit exceeded. ")
		    return
		}

		next(w, req)
	})
}

func main() {
    http.HandleFunc("/hello", rateLimitMiddleware(hello))

}

About

dead simple ratelimiter in go

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published