Created
January 1, 2020 20:45
-
-
Save johnpena/615621c54b2e5d291cebba11c27cd33f to your computer and use it in GitHub Desktop.
Very basic example of a non-queueing worker pool + bouncer in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
concurrency := 10 | |
bouncer := make(chan bool, concurrency) | |
for i := 0; i < concurrency; i++ { | |
bouncer <- true | |
} | |
work := 0 | |
bounces := 0 | |
Loop: | |
for { | |
select { | |
case worker := <- bouncer: | |
go func() { | |
time.Sleep(5 * time.Millisecond) | |
work += 1 | |
bouncer <- worker | |
}() | |
default: | |
bounces += 1 | |
if work > 50 { | |
break Loop | |
} | |
} | |
} | |
fmt.Printf("Work: %d\n", work) | |
fmt.Printf("Bounces: %d\n", bounces) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment