Skip to content

atuowgo/ratelimiter

Repository files navigation

Overview

ratelimiter is a library to limit request rate

Installing

    
    go get -u github.com/atuowgo/ratelimiter
    

Next, include in your application:

    
    import "github.com/atuowgo/ratelimiter"
    

Using

    
    r := ratelimiter.Rule{
    		Name:         "rule1",
    		IntervalInMs: 1000,
    		CellNum:      2,
    		LimitQps:     200,
    }
    ratelimiter.Load(r)
    
    //before call the resource,call the Entry()
    succ,ctx := Entry()
    if succ {
        resource()
    }
    //finally call the Exit()
    Exit(ctx)
    
    
    
    func resource() {
        time.Sleep(time.Millisecond * time.Duration(rand.Intn(50)))
        resCnt++
    }
    
    

About

ratelimiter is a library to limit request rate

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages