Go to file
kayos 2e6d1f7496
Merge pull request #12 from yunginnanet/bench
2024-02-06 23:50:24 -08:00
.github Chore[CI]: Remove defunct commit summarizer 2024-01-20 04:59:28 -08:00
speedometer Chore: remove unused atomic.Bool + doc 2024-01-20 05:03:29 -08:00
.gitignore Actions 2022-03-04 13:33:13 -08:00
LICENSE Update README.md 2022-07-06 04:42:37 -07:00
README.md Update README.md 2022-12-21 16:58:16 -08:00
debug.go Fix: only print if we enabled 2022-12-21 16:57:47 -08:00
go.mod Chore[deps]: go mod tidy 2024-01-08 00:35:54 -08:00
go.sum Chore[deps]: go mod tidy 2024-01-08 00:35:54 -08:00
models.go Feat: Stringer Identity + Optimize 2022-12-18 02:38:43 -08:00
ratelimiter.go Fix[performance]: Don't call DeleteExpired unnecessarily often to avoid superfluous mutex locks 2024-01-08 00:25:47 -08:00
ratelimiter_test.go Feat[testing]: Add benchmarks 2024-02-06 23:46:53 -08:00

Rate5

GoDoc Go Report Card Go codecov five

A performant and generic ratelimitter for any golang project.

See the docs and the unit tests as well as the below example for more details.

Basic Example

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
	}
// [...]
}

License

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.