Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aviddiviner/gin-limit
Gin middleware to limit simultaneous requests.
https://github.com/aviddiviner/gin-limit
Last synced: 2 months ago
JSON representation
Gin middleware to limit simultaneous requests.
- Host: GitHub
- URL: https://github.com/aviddiviner/gin-limit
- Owner: aviddiviner
- License: mit
- Created: 2016-06-18T15:38:25.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-18T01:28:24.000Z (over 7 years ago)
- Last Synced: 2024-02-14T19:32:46.347Z (12 months ago)
- Language: Go
- Homepage:
- Size: 1.95 KB
- Stars: 112
- Watchers: 6
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-gin - aviddiviner/gin-limit
README
# gin-limit
Stay under the limit with this handy Gin middleware.## Purpose
By default, [http.ListenAndServe](https://golang.org/pkg/net/http/#ListenAndServe) (which [gin.Run](https://github.com/gin-gonic/gin/blob/9e930b9bdd5a29bac38fb491ffac4e7ea84b825d/gin.go#L221) wraps) will serve an unbounded number of requests. Limiting the number of simultaneous connections can sometimes greatly speed things up under load. Inspired by [x/net/netutil.LimitListener](https://godoc.org/golang.org/x/net/netutil#LimitListener).## Example
```go
package mainimport (
"github.com/aviddiviner/gin-limit"
"github.com/gin-gonic/gin"
)func main() {
r := gin.Default()
r.Use(limit.MaxAllowed(20))
// ...
r.Run(":8080")
}
```## Lies, damned lies and statistics
Everyone loves synthetic benchmarks, so have some numbers from my 2015 Macbook (on a fast rendering page; single sqlite query, basic templates).% wrk -t12 -c400 -d20s http://localhost:4560/
Running 20s test @ http://localhost:4560/
12 threads and 400 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 848.63ms 525.39ms 1.64s 62.81%
Req/Sec 45.43 61.85 360.00 90.50%
8908 requests in 20.10s, 21.91MB read
Socket errors: connect 0, read 219, write 0, timeout 0
Requests/sec: 443.19
Transfer/sec: 1.09MBNow 10x faster with `limit.MaxAllowed(3)` (although that would be higher in the real world). Hooray!
% wrk -t12 -c400 -d20s http://localhost:4560/
Running 20s test @ http://localhost:4560/
12 threads and 400 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 94.40ms 32.65ms 656.72ms 86.44%
Req/Sec 351.61 84.32 666.00 79.32%
84181 requests in 20.09s, 207.05MB read
Socket errors: connect 0, read 165, write 0, timeout 0
Requests/sec: 4189.75
Transfer/sec: 10.30MB