Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrGroup ¶
type ErrGroup struct {
// contains filtered or unexported fields
}
ErrGroup implements the errgroup.Group interface, but limits goroutines to a execute at a max throughput.
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/cyrusaf/ratelimit"
)
func main() {
ctx := context.Background()
start := time.Now()
// Create ratelimited errgroup with max 10 executions per second
eg, ctx := ratelimit.WithContext(ctx, 10)
// Kick off 10 goroutines.
for i := 0; i < 10; i++ {
// Shadow i so that it can be used in concurrent goroutines without
// future loop iterations changing its value.
i := i
eg.Go(func() error {
fmt.Printf("%d: %s\n", i, time.Since(start).Round(time.Millisecond*10))
return nil
})
}
err := eg.Wait()
if err != nil {
panic(err)
}
}
Output: 0: 100ms 1: 200ms 2: 300ms 3: 400ms 4: 500ms 5: 600ms 6: 700ms 7: 800ms 8: 900ms 9: 1s
func WithContext ¶
WithContext initializes and returns a new RateLimiter
Click to show internal directories.
Click to hide internal directories.