Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math/rand"
- "sync"
- "time"
- )
- func main() {
- count := 50
- ch := make(chan int, count)
- RPCCall := MakeRPCCall()
- wg := sync.WaitGroup{}
- for i := 0; i < count; i++ {
- wg.Add(1)
- go func() {
- defer wg.Done()
- ch <- RPCCall()
- }()
- }
- wg.Wait()
- close(ch)
- for value := range ch {
- fmt.Println(value)
- }
- }
- const rateLimit = 10
- func MakeRPCCall() func() int {
- jobs := make(chan struct{}, rateLimit)
- go func() {
- for {
- time.Sleep(time.Second)
- chanLen := len(jobs)
- if chanLen > rateLimit {
- chanLen = rateLimit
- }
- for i := 0; i < chanLen; i++ {
- <-jobs
- }
- }
- }()
- return func() int {
- jobs <- struct{}{}
- return rand.Int()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment