Esempio n. 1
0
func ExampleRateLimiter_WaitMaxDuration() {
	// 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]
}
Esempio n. 2
0
func ExampleRateLimiter_Wait() {
	// 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]
}
Esempio n. 3
0
func TestRateLimiter_Wait(t *testing.T) {

	r := ratelimit.NewRateLimiter(testInterval, 2, 2)
	start := time.Now()
	var end time.Time
	completed := make(chan struct{})
	go func() {
		r.Wait()
		r.Wait()
		r.Wait()
		end = time.Now()
		completed <- struct{}{}
	}()

	assertReceive(t, completed, testTimeout, "Expected rate limiter to complete but timedout")

	expectedEnd := start.Add(testInterval)
	assert.WithinDuration(t, expectedEnd, end, testInterval, "Expected rate limiter waiting interval")
}