Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/time/rate: Limit support limit per minute or custom duration #30367

Closed
acynothia opened this issue Feb 23, 2019 · 1 comment
Closed

x/time/rate: Limit support limit per minute or custom duration #30367

acynothia opened this issue Feb 23, 2019 · 1 comment

Comments

@acynothia
Copy link

How about Limit support custom duration. Sometimes it's hard to limit user's behaviors in per second. but maybe we can limit frequency in 300 times per minute or 150 times per 30 seconds.

@gopherbot gopherbot added this to the Unreleased milestone Feb 23, 2019
@acynothia
Copy link
Author

sorry, I made a mistake. I think I understand what mean about Limit;

package main

import (
    "fmt"
    "time"

    "golang.org/x/time/rate"
)

func main() {
    // every 1 action per second
    {   
        limiter := rate.NewLimiter(1, 1)
        for i := 0; i < 5; i++ {
            if limiter.Allow() {
                fmt.Printf("act1 ")
            }   
            fmt.Println("wait")
            time.Sleep(time.Second)
        }   
    }   
    // every 3 action per 5 second
    limiter := rate.NewLimiter(rate.Every(5*time.Second), 3)
    for i := 0; i < 10; i++ {
        if limiter.Allow() {
            fmt.Printf("act2 ")
        }   
        fmt.Println("wait")
        time.Sleep(time.Second)
    }   

    // return a token back to limiter
    time.Sleep(5 * time.Second)

    for i := 0; i < 10; i++ {
        if limiter.Allow() {
            fmt.Printf("act2 ")
        }
        fmt.Println("wait")
        time.Sleep(time.Second)
    }
}   

@golang golang locked and limited conversation to collaborators Feb 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants