Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/zeroboo/gin-request-limitter
- Owner: zeroboo
- License: mit
- Created: 2022-09-29T04:33:46.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-04-07T12:44:48.000Z (over 1 year ago)
- Last Synced: 2024-06-20T11:09:56.166Z (6 months ago)
- Topics: gcp, gin, gin-gonic, go, golang, google-datastore, middleware
- Language: Go
- Homepage:
- Size: 85 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
```