How to Rate Limit Go Applications
One of the factors you may want to consider while building your application is the amount of traffic you expect from users. The amount of traffic possibly decides more factors that may include resource allocation, especially if you’re hosting your app on a cloud service provider.
Rate limiting is one of the techniques you can use to control traffic on your application or network.

What Is Rate Limiting?
Rate limiting is a widespread access restriction technique for limiting network traffic, primarily within speculated time frames or when the user has executed several requests.
Rate limiting is popular for reducing cyber attacks like brute force andDDoS (Distributed Denial of Service), limitingweb scraping, API requests, and other irregular user interactions like bot automation and server strain.

Go provides first-class support for rate-limiting applications in theratepackage that provides a rate limiter and interoperates with thetimepackage.
Theratepackage is part of the Go project, but the package is not available in the standard library. You’ll need to install the package with thegetcommand.

Run this command in the terminal of your working directory to add the package to your project’s dependencies.
Import these packages into your Go file for this tutorial.

Thejsonpackage is for encoding a struct as JSON to the client. You’ll use thelogpackage tologerrors to the console and thehttppackage to build the endpoint and the middleware and start a server.
Building a Simple API With One Endpoint
Conventionally, you’ll write a middleware for the handler functions you want to rate limit. Each time the user sends a request, the middleware validates the request status before relaying access to the handler function, depending on the case.
Here’s the struct model with string fields you’ll encode to the client.

The handler function will set the content type to JSON, write a successful status code, and return an encoded struct instance to the client.
TheendpointExamplehandler function takes in anhttppackagewriterandrequestmethod instance and returns a message to the client with thewriterinstance.
Rate Limiting a Simple Go Application
Rate limiting via a user’s number of requests or the available number of requests is similar. You’ll always need to create a limiter before the authorization process.
Here’s how you may create a rate limiter and authorize users based on the number of requests.
TherateLimiterMiddlewarehandler function is a middleware that accepts a handler function as the argument and returns the result of the authorization after creating a new rate limiter with theNewLimitermethod that takes two parameters for the number of requests per second after the specified maximum requests.
TheAllowmethod of the limiter instance returns a boolean based on the status of authorized requests. TherateLimiterMiddlewarereturns the JSON message if the request is authorized or the"rate limit exceeded “message when the client has sent the maximum number of requests.
Themainfunction mounts the/homeendpoint to therateLimiterMiddlewarehandler function that takes in theendpointExamplehandler function.
TheListenAndServemethod starts a server on localhost port8080and returns possible errors.
you could run this command on the terminal of your working directory or witha bash scriptto test the endpoint after running the server.
The code hits the/homeendpoint ten times with a request. Here are the result of the requests.
After the sixth request (maximum), the client is unauthorized and can no longer access the endpoint.
Rate-Limiting Is Important
Rate-limiting is essential, especially if you’re looking towards cutting the cost of hosting your application, want to reduce bot interference, or secure your app from cyber attacks. Similar to Go’sratepackage, npm provides theexpress-rate-limitpackage to rate-limit express applications.
Rate limiting protects a service from becoming overloaded. Discover the express-rate-limit package and how it can improve your apps.
You can’t call this offline, Notion.
Every squeak is your PC’s way of crying for help.
The key is not to spook your friends with over-the-top shenanigans.
Lose your laptop without this feature, and you’ll wish you had turned it on.
These are the best free movies I found on Tubi, but there are heaps more for you to search through.