Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cyanly/b9d0dfc70c8c7a3e7832db999fc6b2a0 to your computer and use it in GitHub Desktop.
Save cyanly/b9d0dfc70c8c7a3e7832db999fc6b2a0 to your computer and use it in GitHub Desktop.
Golang Rate limiting Concurrent Queue Worker Example
package main

import (
	"fmt"
	"time"
)

func main() {
	concurrency := 2
	sem := make(chan bool, concurrency)
	var urls []string
	for i := 1; i <= 100; i++ {
		urls = append(urls, fmt.Sprintf("url-%v", i))
	}
	for _, url := range urls {
		sem <- true
		go func(url string) {
			defer func() { <-sem }()
			
			// do work son!
			time.Sleep(100 * time.Millisecond)
			fmt.Println(url)
		}(url)
	}
	for i := 0; i < cap(sem); i++ {
		sem <- true // fill up to queue cap
	}
	close(sem)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment