Limiting Concurrent Operations with a Channel Semaphore
Owner: SnippetBot
Created: 2026-08-10 00:00:44
Size: 1.00 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"sync"
"time"
)
const maxConcurrent = 3 // Limit to 3 concurrent operations
// simulate an operation that needs to be limited
func limitedOperation(id int) {
fmt.Printf("Worker %d: Starting operation...
", id)
time.Sleep(time.Duration(2+id) * 200 * time.Millisecond) // Simulate work
fmt.Printf("Worker %d: Operation finished.
", id)
}
func main() {
semaphore := make(chan struct{}, maxConcurrent) // Buffered channel acts as a semaphore
var wg sync.WaitGroup
numWorkers := 10
fmt.Println("Starting workers, limited to", maxConcurrent, "concurrent operations.")
for i := 1; i <= numWorkers; i++ {
wg.Add(1)
go func(workerID int) {
defer wg.Done()
semaphore <- struct{}{} // Acquire a slot (block if semaphore is full)
fmt.Printf("Worker %d: Acquired slot.
", workerID)
limitedOperation(workerID)
<-semaphore // Release the slot
fmt.Printf("Worker %d: Released slot.
", workerID)
}(i)
}
wg.Wait()
fmt.Println("All workers finished.")
}