GithubHelp home page GithubHelp logo

t4pusd / go-ratelimiter-demo Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 7 KB

A demo project to demonstrate rate limiting implementation with go and gin server utilizing redis as the rate limiting count holder. Utilized redis server side lua scripting to ensure consistent rate counts.

Go 100.00%

go-ratelimiter-demo's Introduction

Rate Limiter

A web server with go gin module to demonstrate rate limiting implementation with redis

Dependencies

  • Redis

How to run

To run the project a redis server must be installed either in host or in docker environment.
After ensuring that you can use the following command to start the server:

go run ./main.go

Note: If your redis server is not running in default port or host or have a password set in redis then change the following code section in main.go file:

redisClient := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379", // Replace with your Redis server address
		Password: "",               // Replace with your Redis password
		DB:       0,                // Replace with the desired Redis database index
	})

How to test the rate limiter

To test the rate limiter in effect his the following endpoint

curl http://localhost:8080/limited-route

After hitting the endpoint 10 times you will see too many request response code from the server which will get cooled down after 1 minute.

How to change the default max limit and cooldown time window

To change the default max limit and cooldown time window change the following parameters in the main.go file:

  • limit
  • interval
limiter := &RateLimiter{
	limit:       10,          // Maximum number of requests allowed
	interval:    time.Minute, // Time interval for rate limiting
	redisClient: redisClient, // Redis client instance
	luaScriptID: luaScriptID, // Lua script ID
}

go-ratelimiter-demo's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.