A generic ratelimitter for any golang project.
See the docs and the examples below for more details.
import rate5 "github.com/yunginnanet/rate5"
var Rater *rate5.Limiter
[...]
type Client struct {
ID string
Conn net.Conn
loggedin bool
}
// Rate5 doesn't care where you derive the string used for ratelimiting
func (c Client) UniqueKey() string {
if !c.loggedin {
host, _, _ := net.SplitHostPort(c.Conn.RemoteAddr().String())
return host
}
return c.ID
}
func (s *Server) handleTCP(c *Client) {
// Returns true if ratelimited
if Rater.Check(c) {
c.Conn.Write([]byte("too many connections"))
c.Conn.Close()
return
}
[...]
Concurrent TCP Server with Rate5 Ratelimiter
More Documentation
More To-Dos
Test Cases
More test cases.