mirror of
https://github.com/go-i2p/go-limit.git
synced 2025-06-07 10:01:46 -04:00
master
go-limit
A simple, thread-safe connection limiter for Go's net.Listener
that manages concurrent connections and rate limiting.
Factored out from sam-forwarder to be used in go-i2ptunnel.
Install
go get github.com/go-i2p/go-limit
Quick Start
package main
import (
"log"
"net"
"github.com/go-i2p/go-limit"
)
func main() {
base, _ := net.Listen("tcp", ":8080")
listener := limitedlistener.NewLimitedListener(base,
limitedlistener.WithMaxConnections(1000), // max concurrent
limitedlistener.WithRateLimit(100)) // per second
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("Accept error: %v", err)
continue
}
go handleConnection(conn)
}
}
Features
- Limit concurrent connections
- Rate limiting (connections per second)
- Connection tracking
- Real-time statistics
Configuration
// Set maximum concurrent connections
WithMaxConnections(1000)
// Set rate limit (connections per second)
WithRateLimit(100.0)
// Get current stats
stats := listener.GetStats()
License
MIT License
Support
Issues and PRs welcome at github.com/go-i2p/go-limit
Description
Languages
Go
98.4%
Makefile
1.6%