Skip to content

Files

Latest commit

May 18, 2023
9b3f24d · May 18, 2023

History

History
41 lines (32 loc) · 1012 Bytes

concurrency.rate_limiting.bounded.md

File metadata and controls

41 lines (32 loc) · 1012 Bytes

Overview

  • Example of rate-limiting (Bounded parallelism) pattern

Example

func main() {
    maxParallel := 3

    semaphoreCh := make(chan struct{}, maxParallel)
    defer close(semaphoreCh)

    // -- Spawn tasks
    for taskId := 0; taskId < 50; taskId++ {
        semaphoreCh <- struct{}{} // reserve a slot (blocking)

        go func(taskId int) {
            simulateSlowTask(taskId)
            <-semaphoreCh // free the slot
        }(taskId)
    }

    // -- Wait for all the tasks to complete
    // In practice, use a WaitGroup or done chan
    time.Sleep(3 * time.Minute)
}

func simulateSlowTask(taskId int) {
    // -- Do the slow task
    fmt.Printf("BEGIN: %v\n", taskId)
    time.Sleep(6 * time.Second) // simulate slow task
    fmt.Printf("END: %v\n", taskId)
}

Other Resources

  1. time.Sleep
  2. channels doc
  3. https://gobyexample.com/rate-limiting
  4. https://yourbasic.org/golang/wait-for-goroutines-waitgroup/