Coder Social home page Coder Social logo

rate's Introduction

go-rate

Go GitHub tag (latest SemVer) GoDoc Go Report Card Coverage Status

go-rate provides the rate limiters generally.

Features

History

  • v0.5.0

    • BREAK: To decrease unecessary dependants, we removed middleware subpackage. It has been moved into supports/ and taged with ignore.
      • You must copy its codes to use it.
    • the codes reviewed
    • no any third-party deps now, even from mine.
  • v0.1.x

    • as is

Usages

Simple

package main

import (
	"fmt"
	"github.com/hedzr/rate"
	"time"
)

func main() {
	l := rate.New(rate.LeakyBucket, 100, time.Second)
	for i := 0; i < 120; i++ {
		ok := l.Take(1)
		if !ok {
			fmt.Printf("#%d Take() returns not ok, counter: %v\n", i, rate.CountOf(l))
			time.Sleep(50 * time.Millisecond)
		}
	}
}

As a gin middleware

package main

import (
   "github.com/gin-gonic/gin"
   "github.com/hedzr/rate"
   "github.com/hedzr/rate/middleware"
   "time"
)

func webserver() {
	r := engine()
	r.Run(":3000")
}

func engine() *gin.Engine {
	config := &middleware.Config{
		Name:          "...",
		Description:   "...",
		Algorithm:     string(rate.TokenBucket),
		Interval:      time.Second,
		MaxRequests:   1000,
		HeaderKeyName: "X-API-TOKEN",
		ExceptionKeys: nil,
		Routes:        nil,
	}
	r := gin.Default()
	r.Use(middleware.ForGin(config))
	return r
}

Load limit config with cmdr Option Store

While integrated with hedzr/cmdr, the short loading is available:

import "github.com/hedzr/rate/middleware"

func BuildRoutes(rg *gin.Engine) *gin.Engine {
    buildRoutes(rg.Group("/prefix"), rg)
}
func buildRoutes(rg Router, root *gin.Engine) {
    middleware.LoadConfigForGin("server.rate-limits", rg)
    rg.Get("/echo/*action", echoHandler)
}
func echoGinHandler(c *gin.Context) {
    action := c.Param("action")
    if action == "" || action == "/" {
        action = "<no action>"
    }
    _, _ = io.WriteString(c.Writer, fmt.Sprintf("action: %v\n", action))
}

A config file (eg. rate-limit.yml) should be put in cmdr-standard conf.d directory, so it can be loaded automatically:

app:
  your-app: # <- replace it with your app name, the further KB in cmdr docs.
    server:
      rate-limits:
        - name: by-api-key
          interval: 1ms
          max-requests: 30
          header-key-name: X-API-KEY
          exception-keys: [voxr-apps-test-api-key-fndsfjn]

License

MIT

rate's People

Contributors

dependabot[bot] avatar hedzr avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.