Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zeroboo/gin-request-limitter

Request limitter as middleware for gin-gonic framework
https://github.com/zeroboo/gin-request-limitter

gcp gin gin-gonic go golang google-datastore middleware

Last synced: about 1 month ago
JSON representation

Request limitter as middleware for gin-gonic framework

Awesome Lists containing this project

README

        

# gin-request-limitter
Request limitter as middleware for gin-gonic framework: limit requests can be accepted of an user.

# Features:
- Limit request interval: user can not send request too fast
- Limit request freequently:
- User can not send too many requests in a time window.
- Implement Fixed windows algorithm using Redis or Google Firestore as persistence.
# Usage
* Install
```console
go get github.com/zeroboo/gin-request-limitter
```
* Run

```go

ctx := context.Background()
dsClient, _ := datastore.NewClient(ctx, datastoreProjectId, option.WithCredentialsFile(serviceAccount))

/*
Create a limitter middleware per user:
persists data to kind 'tracker' in datastore
collect userId from gin context by field 'userId'
restricts request only send to 200 milis after the last one
restricts maximum 100 requests per 60 seconds
*/
handler := CreateDatastoreBackedLimitter(dsClient,
"tracker",
GetUserIdFromContextByField("userId"),
200,
60000,
100),

//use handler ...
```

* Test
```console
go test -timeout 60s github.com/zeroboo/gin-request-limitter -v
```