This is the entry of an api-server using rate limiter. It's designed to control the rate of actions that can be performed by a user or a system. The main goal is to prevent any part of the system from being overwhelmed by too many requests.
We use sliding-window algorithm to implement rate limiting. There is also a middleware that imposes a monthly quota on each user.
The code structure is based on project-layout.
first you should have docker installed in your system. you can follow instruction on this link.
after that build and run the project image running this command
docker compose up -d .
first you need to signup:
curl --request POST \
--url https://localhost:8080/signup \
--header 'Content-Type: application/json' \
--data '{
"month_size_limit": 100,
"minute_rate_limit": 4
}
you will see response like this:
{
"ID": "d8fff3b4-388d-48f3-82c3-81924c0de5b4",
"month_size_limit": 100,
"minute_rate_limit": 4
}
you can do it by run bash script here.
after that you can send your data as follows:
curl --request GET \
--url https://localhost:8080/hello \
--header 'Data-ID: id_sampleid' \
--header 'Data-Size: 26' \
--header 'User-ID: d8fff3b4-388d-48f3-82c3-81924c0de5b4' # id from signup respose
by passing the middlewares,
you will access to api which here is HI
.
HI
you can see changes by run redis insight in your browser
https://localhost:8001/
before accessing the API-server we have some middlewares to pass.
HTTP handler are defined in handler
package. Echo
is an awesome HTTP framework that has everything you need.
The main part of each application is its configuration. There are many ways for having configuration in the project from configuration file to environment variables. Koanf has all of them in a one beautiful package. The main points here are:
- Having a defined and typed structure for configuration
- Don't use global configuration. each module has its configuration defined in
config
module and it will pass to it in its initiation. - Print loaded configuration at startup, so everyone can validate the applied configuration.
zap is one the best logger for structure logging.
zap
forces you name loggers with Named
method.
By using the named logger you can easily find you module logs in your log aggregator.
redis, which stands for Remote Dictionary Server, is known for its speed, reliability, and performance, delivering sub-millisecond response times and enabling millions of requests per second for real-time applications. so I decided to use it, reading data types and found the best model to use.
I searched for different algorithms of rate limiter. the Sliding Window algorithm works by maintaining a sliding window of fixed duration which is best fit for our project. find this page and use code from this repo.
custom-rate-limiting-in-golang-using-redis-with-docker
implementing-a-sliding-log-rate-limiter-with-redis-go
rate-limiting-go-application
tollbooth