Skip to content

T5WEB/ratelimiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How works:

  • Initialy are declare two globals variables ctx and persistence.

  • func init(), is used for load variables .env and fill variable persistence with client of redis.

  • I created a interface Persistence to apply strategy pattern.

  • RedisPersistence is a struct for conector redis, the methods GetLimit, Incr and Expire inplements the logic for use of the Redis.

  • In the func main init gin.Default(), then set up a variable config to receive the configs of cors, so r.use inject the configs of cors in gin.

  • The func handleRequest is used only take a message when status is ok.

  • The func rateLimiterMiddleware make role of intermediate the quantity of the requests. In your param pass the persistence. It logic implemented is first create three vars in local scope key, limit and blockTime. In the first if else is to split when the request is by ip of token. So is filled the variable in each case. After persistence.GetLimit return the limit or error

  • In the func getEnvInt is used for get values in file .env

How to test:

  • set up in file .env the variables IP_LIMIT, TOKEN_LIMIT to configure maximum values for requests. When the values aren't pass, by default IP_LIMIT=10 and TOKEN_LIMIT=100.

  • Run the commands: go run main.go and docker compose up -d redis

  • Open in navigator the address: http://localhost:8080/api/resource

    • When you use for navigator the the rate limit is used for ip
  • For test with token open the client request as Postman and set up in header request API_TOKEN. We put a public workspace for tests in Postman, but you to use ever other. For Postman:

    Run In Postman

    Obs: To use Postman is necessary download software Desktop Agent
    • Alternatively you can use for website https://resttesttest.com/
      • Click in + Add header button
      • Filling empty fields, Header Name with API_KEY and Header Value with ever value
      • Press f12 in navigator chrome for open developer tools
      • Walk until network and click fetch/xhr
      • Click button Ajax Request

Automatic tests

  • Run go test

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages