Rate limiting your goroutines

Hey mates!
Just a quick one I like to talk about! Rate limiting goroutines.

This is about controlling the actual amount of concurrent task executions.

Sometimes, we have to process and execute a stream of long-running tasks and we don’t know at run…


This content originally appeared on DEV Community and was authored by Lucas Godoy

Hey mates!
Just a quick one I like to talk about! Rate limiting goroutines.

This is about controlling the actual amount of concurrent task executions.

Sometimes, we have to process and execute a stream of long-running tasks and we don't know at runtime how many of them are coming out from the task channel. So here, the main concern is not firing all the goroutines together, at the time tasks are ingested. Otherwise, firing many of them concurrently uncontrolled could lead to unpredicted behaviors or memory overflow.

Therefore, a limiter (AKA semaphore) empty struct buffered channel has been added into the mix, capped with the count of tasks to run concurrently.

  1. For the first n-count iteration an empty struct will be pushed onto the limiter channel,
  2. A goroutine is fired up to run the incoming task.
  3. At the n-count + 1 iteration, the limiter channel will be full, hence the current main goroutine will be blocked.
  4. Once any of the currently running tasks finish its execution, it will readout of the limiter channel, to make some room for another task to be run. This will unblock the main goroutine.
  5. After the main goroutine takes the control back, it will push an empty struct onto the limiter channel and start over the cycle by running a new goroutine for the incoming task.

And so on until the time out is reached, so the for loop brakes and no more tasks are run.

To sum up, this is how we can limit goroutines to come up all together by controlling how many of them could be up and running concurrently by using a limiter buffered channel for it. Avoiding causing a memory overflow and unpredicted behavior.


This content originally appeared on DEV Community and was authored by Lucas Godoy


Print Share Comment Cite Upload Translate Updates
APA

Lucas Godoy | Sciencx (2021-07-11T14:12:22+00:00) Rate limiting your goroutines. Retrieved from https://www.scien.cx/2021/07/11/rate-limiting-your-goroutines/

MLA
" » Rate limiting your goroutines." Lucas Godoy | Sciencx - Sunday July 11, 2021, https://www.scien.cx/2021/07/11/rate-limiting-your-goroutines/
HARVARD
Lucas Godoy | Sciencx Sunday July 11, 2021 » Rate limiting your goroutines., viewed ,<https://www.scien.cx/2021/07/11/rate-limiting-your-goroutines/>
VANCOUVER
Lucas Godoy | Sciencx - » Rate limiting your goroutines. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/11/rate-limiting-your-goroutines/
CHICAGO
" » Rate limiting your goroutines." Lucas Godoy | Sciencx - Accessed . https://www.scien.cx/2021/07/11/rate-limiting-your-goroutines/
IEEE
" » Rate limiting your goroutines." Lucas Godoy | Sciencx [Online]. Available: https://www.scien.cx/2021/07/11/rate-limiting-your-goroutines/. [Accessed: ]
rf:citation
» Rate limiting your goroutines | Lucas Godoy | Sciencx | https://www.scien.cx/2021/07/11/rate-limiting-your-goroutines/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.