Created
October 4, 2022 08:07
-
-
Save manzanit0/935972fa462cf8bbb8b2f3d7823bfc74 to your computer and use it in GitHub Desktop.
Go naive worker pool toy
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" | |
"log" | |
"sync" | |
) | |
type Input struct { | |
Id string | |
} | |
func (i Input) ID() string { | |
return i.Id | |
} | |
func main() { | |
input := make(chan Identifiable, 100) | |
for i := 0; i < 100; i++ { | |
input <- Input{Id: fmt.Sprint(i)} | |
} | |
Process(input, 100, 10, func(i Identifiable) error { | |
fmt.Printf("doing work for ID %s\n", i.ID()) | |
return nil | |
}) | |
} | |
type Identifiable interface { | |
ID() string | |
} | |
type Failed struct { | |
Error error | |
ID string | |
} | |
type Success struct { | |
ID string | |
} | |
func Process(input chan Identifiable, inputSize int64, maxConcurrency int, doWork func(Identifiable) error) ([]Success, []Failed) { | |
failuresCh := make(chan Failed, inputSize) | |
successCh := make(chan Success, inputSize) | |
var wg sync.WaitGroup | |
wg.Add(maxConcurrency) | |
for w := 0; w < maxConcurrency; w++ { | |
go func() { | |
defer func() { | |
if r := recover(); r != nil { | |
log.Printf("recover: %+v", r) | |
} | |
}() | |
defer wg.Done() | |
for j := range input { | |
err := doWork(j) | |
if err != nil { | |
failuresCh <- Failed{ID: j.ID(), Error: err} | |
continue | |
} | |
successCh <- Success{ID: j.ID()} | |
} | |
}() | |
} | |
close(input) | |
wg.Wait() | |
close(failuresCh) | |
close(successCh) | |
failures := []Failed{} | |
for f := range failuresCh { | |
failures = append(failures, f) | |
} | |
succesful := []Success{} | |
for f := range successCh { | |
succesful = append(succesful, f) | |
} | |
return succesful, failures | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment