Создание rate limit
Условие задачи
Необходимо реализовать ограничение количества одновременно выполняющихся горутин при отправке большого количества запросов (например, 10 000). Все запросы должны быть выполнены, но в каждый момент времени не должно выполняться более N горутин. Обработка ошибок при этом не требуется
gopackage main
import (
"context"
"fmt"
"strconv"
"sync"
"github.com/sirupsen/logrus"
)
type Request struct {
Payload string
}
type Client interface {
SendRequest(ctx context.Context, request Request) error
}
type client struct{}
func (c client) SendRequest(ctx context.Context, request Request) error {
fmt.Println("send request", request.Payload)
return nil
}
func main() {
ctx := context.Background()
c := client{}
requests := make([]Request, 100)
for i := 0; i < 100; i++ {
requests[i] = Request{Payload: strconv.Itoa(i)}
}
log := logrus.New()
makeBatchApiCalls(ctx, c, log, requests)
}
func makeBatchApiCalls(ctx context.Context, c Client, log *logrus.Logger, requests []Request) {
wg := sync.WaitGroup{}
for _, r := range requests {
r := r
wg.Add(1)
go func() {
defer wg.Done()
err := c.SendRequest(ctx, r)
if err != nil {
log.WithError(err).Error("send request")
}
}()
}
wg.Wait()
}