Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
func NewRateLimiter ¶
func NewRateLimiter(resetInterval time.Duration, itemsPerInterval, limit int64) *RateLimiter
func (*RateLimiter) Update ¶
func (r *RateLimiter) Update(limit, remaining int64, resetAt time.Time)
func (*RateLimiter) Wait ¶
func (r *RateLimiter) Wait()
Example ¶
package main import ( "fmt" "github.com/dougnukem/ratelimit" "time" ) func main() { // 2 requests per second refreshing 2 capacity every second r := ratelimit.NewRateLimiter(time.Second, 2, 2) start := time.Now() r.Wait() fmt.Printf("r.Wait() elapsed less than 500 ms [%t]\n", time.Now().Sub(start) < 500*time.Millisecond) r.Wait() fmt.Printf("r.Wait() elapsed less than 500 ms [%t]\n", time.Now().Sub(start) < 500*time.Millisecond) r.Wait() fmt.Printf("r.Wait() elapsed greater than 500 ms [%t]\n", time.Now().Sub(start) > 500*time.Millisecond) }
Output: r.Wait() elapsed less than 500 ms [true] r.Wait() elapsed less than 500 ms [true] r.Wait() elapsed greater than 500 ms [true]
func (*RateLimiter) WaitMaxDuration ¶
func (r *RateLimiter) WaitMaxDuration(duration time.Duration) bool
Example ¶
package main import ( "fmt" "github.com/dougnukem/ratelimit" "time" ) func main() { // 2 requests per second refreshing 2 capacity every second r := ratelimit.NewRateLimiter(time.Second, 2, 2) maxDuration := 500 * time.Millisecond s := r.WaitMaxDuration(maxDuration) fmt.Printf("r.Wait() success[%t]\n", s) s = r.WaitMaxDuration(maxDuration) fmt.Printf("r.Wait() success[%t]\n", s) s = r.WaitMaxDuration(maxDuration) fmt.Printf("r.Wait() success[%t]\n", s) }
Output: r.Wait() success[true] r.Wait() success[true] r.Wait() success[false]
Click to show internal directories.
Click to hide internal directories.