Created
July 7, 2020 19:12
-
-
Save onurkagan/d4654584c42194a174660a3ea5bf52a0 to your computer and use it in GitHub Desktop.
This file contains 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" | |
"math/rand" | |
"strconv" | |
"sync" | |
"time" | |
) | |
// Worker is the work horse | |
type Worker interface { | |
executeTask(string, *sync.WaitGroup) error | |
} | |
// VizWorker is just a type of worker | |
type VizWorker struct { | |
id string | |
} | |
func (w *VizWorker) executeTask(task string, wg *sync.WaitGroup) error { | |
defer wg.Done() | |
fmt.Printf("Worker's id %s , executing task, message is %s \n", w.id, task) | |
return nil | |
} | |
// WorkerPool is the combination of available workers up for processing a task. | |
type WorkerPool struct { | |
pool []Worker | |
wg *sync.WaitGroup | |
} | |
func (wp *WorkerPool) job(task string) error { | |
// look into the pool | |
// take out one worker goroutine | |
worker := wp.pool[rand.Intn(len(wp.pool))] | |
// increase waitgroup | |
wp.wg.Add(1) | |
// execute job | |
go worker.executeTask(task, wp.wg) | |
return nil | |
} | |
func main() { | |
var wg sync.WaitGroup | |
rand.Seed(time.Now().Unix()) // initialize global pseudo random generator | |
// Initiate worker pool | |
numberOfWorkers := 3 | |
vizWorkerArray := make([]Worker, 0) | |
for i := 0; i < numberOfWorkers; i++ { | |
w := &VizWorker{id: strconv.FormatInt(int64(i), 10)} | |
vizWorkerArray = append(vizWorkerArray, w) | |
} | |
workerPool := &WorkerPool{wg: &wg, pool: vizWorkerArray} | |
messages := []string{"alpha", "beta", "gamma", "aa", "bb", "cc", "dd"} | |
for _, v := range messages { | |
workerPool.job(v) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment