Is it possible to limit how many goroutines run per second?

Pig :

I have a list of URLs that I need to use goroutine to fire off HTTP requests concurrently. Is there anyway to check and limit how many of those HTTP requests are sent per second?

JimB :

A very simple version of this in Go would be an adaptation of a Leaky Bucket algorithm, using a channel and a goroutine. Receiving a token from the rate channel before making a request will check the rate and block if the rate limiter is empty.

// create a buffered channel.
// The capacity of the channel is maximum burst that can be made.
rate := make(chan struct{}, 10)

go func() {
    ticker := time.NewTicker(100 * time.Millisecond)
    for range ticker.C {
        rate <- struct{}{}
    }
}()

Since a series of requests that take longer than the average rate will end up being concurrent, you may need to limit concurrency too. You can add a second channel as a semaphore, adding a token to the semaphore before making a request, and removing it when it's complete.

// limit concurrency to 5
semaphore := make(chan struct{}, 5)

// in request function
semaphore <- struct{}{}
defer func() {
    <-semaphore
}()

A slightly more complete example is here:

https://play.golang.org/p/ZrTPLcdeDF

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

gnu parallel: how to set the limit per second

From Dev

Asyncio - how to limit requests per 1 second?

From Dev

how many is too many Kafka commits per second

From Dev

If I don't know how many times something will run per second, how can I get the same output?

From Dev

How to set message per second limit for users on ejabberd?

From Dev

How to limit API calls per second with angular2

From Dev

php - Antiflood - how to limit 2 requests per second

From Dev

How to Limit request per Second in Async Task C#

From Dev

How to Measure How Many Results App Can Produce Per Second

From Dev

Incapsula Rate Limit Per Second

From Dev

How many attempts per second can a password cracker actually make?

From Java

How many interrupts does my cpu have per second?

From Dev

affdex-sdk. How many frames can processed per second?

From Java

how to run a code 60 times per second in java

From Dev

How to run a command at an average of 5 times per second?

From Dev

How can I run a macro multiple times per second in vba?

From

How do I execute commands many many times per second in Golang?

From Dev

Is there a limit to how many promises can or should run concurrently?

From Dev

Azure function: limit the number of calls per second

From Dev

Limit not bandwith, but packets per second in linux

From Dev

Python Async Limit Concurrent coroutines per second

From Dev

Limit number of connections per second in Apache HttpClient

From Dev

Limit fadeOut() to 3 steps per second

From Dev

Gmail API Global Per Second Rate Limit

From Java

Calling many methods of many objects many times per second

From

How many goroutines are started by default in a Go program?

From

In Golang, how to handle many goroutines with channel

From Dev

Error: too many emails per second on Heroku

From Dev

How to limit an Akka Stream to execute and send down one message only once per second?

Related Related

  1. 1

    gnu parallel: how to set the limit per second

  2. 2

    Asyncio - how to limit requests per 1 second?

  3. 3

    how many is too many Kafka commits per second

  4. 4

    If I don't know how many times something will run per second, how can I get the same output?

  5. 5

    How to set message per second limit for users on ejabberd?

  6. 6

    How to limit API calls per second with angular2

  7. 7

    php - Antiflood - how to limit 2 requests per second

  8. 8

    How to Limit request per Second in Async Task C#

  9. 9

    How to Measure How Many Results App Can Produce Per Second

  10. 10

    Incapsula Rate Limit Per Second

  11. 11

    How many attempts per second can a password cracker actually make?

  12. 12

    How many interrupts does my cpu have per second?

  13. 13

    affdex-sdk. How many frames can processed per second?

  14. 14

    how to run a code 60 times per second in java

  15. 15

    How to run a command at an average of 5 times per second?

  16. 16

    How can I run a macro multiple times per second in vba?

  17. 17

    How do I execute commands many many times per second in Golang?

  18. 18

    Is there a limit to how many promises can or should run concurrently?

  19. 19

    Azure function: limit the number of calls per second

  20. 20

    Limit not bandwith, but packets per second in linux

  21. 21

    Python Async Limit Concurrent coroutines per second

  22. 22

    Limit number of connections per second in Apache HttpClient

  23. 23

    Limit fadeOut() to 3 steps per second

  24. 24

    Gmail API Global Per Second Rate Limit

  25. 25

    Calling many methods of many objects many times per second

  26. 26

    How many goroutines are started by default in a Go program?

  27. 27

    In Golang, how to handle many goroutines with channel

  28. 28

    Error: too many emails per second on Heroku

  29. 29

    How to limit an Akka Stream to execute and send down one message only once per second?

HotTag

Archive