Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rleungx/gin-ratelimiter
The gin-ratelimiter is a middleware for limiting the request rate under Gin framework.
https://github.com/rleungx/gin-ratelimiter
gin middleware rate-limiting
Last synced: 5 days ago
JSON representation
The gin-ratelimiter is a middleware for limiting the request rate under Gin framework.
- Host: GitHub
- URL: https://github.com/rleungx/gin-ratelimiter
- Owner: rleungx
- License: apache-2.0
- Created: 2022-02-14T11:31:29.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-12T10:20:34.000Z (8 days ago)
- Last Synced: 2025-02-12T10:49:18.419Z (8 days ago)
- Topics: gin, middleware, rate-limiting
- Language: Go
- Homepage:
- Size: 31.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gin-ratelimiter
[](https://github.com/rleungx/gin-ratelimiter/actions/workflows/go.yml)
[](https://codecov.io/gh/rleungx/gin-ratelimiter)
[](https://goreportcard.com/report/github.com/rleungx/gin-ratelimiter)
[](https://godoc.org/github.com/rleungx/gin-ratelimiter)The gin-ratelimiter is a middleware for limiting the request rate under [Gin framework](https://github.com/gin-gonic/gin) based on [golang.org/x/time/rate](golang.org/x/time/rate)
## Usage
```go
go get github.com/rleungx/gin-ratelimiter
```And import it in your code:
```go
import "github.com/rleungx/gin-ratelimiter"
```## Example
See the [example](examples/main.go).
```go
package mainimport (
"net/http""github.com/gin-gonic/gin"
ratelimiter "github.com/rleungx/gin-ratelimiter"
)func main() {
r := gin.New()l := ratelimiter.NewLimiter()
// Example ping request.
r.GET("/ping", l.SetLimiter(ratelimiter.WithConcurrencyLimiter(1), ratelimiter.WithQPSLimiter(3, 3)),
func(c *gin.Context) {
c.String(http.StatusOK, "")
})// Listen and Server in 0.0.0.0:8880
r.Run(":8880")
}
```The output with 4 requests:
```
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Wed, 12 Feb 2025 08:34:44 GMT
Content-Length: 0HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Wed, 12 Feb 2025 08:34:44 GMT
Content-Length: 0HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Wed, 12 Feb 2025 08:34:44 GMT
Content-Length: 0HTTP/1.1 429 Too Many Requests
Date: Wed, 12 Feb 2025 08:34:44 GMT
Content-Length: 0
```