https://github.com/karpeleslab/limitlistener
https://github.com/karpeleslab/limitlistener
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/karpeleslab/limitlistener
- Owner: KarpelesLab
- License: mit
- Created: 2024-04-11T04:28:04.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-04-11T06:59:06.000Z (about 2 years ago)
- Last Synced: 2024-06-19T13:38:06.587Z (almost 2 years ago)
- Language: Go
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://godoc.org/github.com/KarpelesLab/limitlistener)
# limitlistener
This library makes it easy to limit the number of live threads for a given listener. This
can be useful in a number of cases to ensure not too many connections will be processed
at the same time rather than letting go create new goroutines like there is no tomorrow.
Any call to `Accept()` when the limit has been reached will wait for any of the currently
running connections to close before accepting any new connection.
## Usage
```go
l, err := net.Listen("tcp", ":12345")
if err != nil {
// ...
}
// limit to 128 concurrent processes
l = limitlistener.New(l, 128)
for {
c, err := l.Accept()
if err != nil {
return err
}
// safe to start a goroutine here, will be limited to 128 routines
go handleClient(c)
}
handleClient(c net.Conn) {
defer c.Close()
// ...
}
```
This can be used for a `http.Server` or anything that takes a `net.Listener` and will
apply the limit as standard as possible. Multiple threads calling Accept() are also
supported.
If using this with `http.Server` be careful to set a `ReadTimeout` or you may end
blocked just with idle connections.