1
2
mirror of https://github.com/yunginnanet/Rate5 synced 2024-06-28 18:11:01 +00:00
Go to file
2021-08-28 09:40:28 -07:00
_examples Fix major ratelimit bugs and expand upon example 2021-08-28 09:40:28 -07:00
go.mod initial commit 2021-08-27 23:22:25 -07:00
go.sum initial commit 2021-08-27 23:22:25 -07:00
LICENSE Add new functions, rename things, add documentation 2021-08-28 07:39:22 -07:00
models.go Fix major ratelimit bugs and expand upon example 2021-08-28 09:40:28 -07:00
ratelimiter.go Fix major ratelimit bugs and expand upon example 2021-08-28 09:40:28 -07:00
README.md Update README.md 2021-08-28 07:45:35 -07:00

Rate5

GoDoc Go Report Card

A generic ratelimitter for any golang project.

Short Example Implementation

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) {
	defer func() {
		c.Conn.Close()
		println("closed: " + c.Conn.RemoteAddr().String())
	}()

	// Returns true if ratelimited
	if Rater.Check(c) {
		c.Conn.Write([]byte("too many connections"))
		return
	}
    // handle connection ... 
}
    

To-Do

More documentation
Test cases