- Publish task/job to workers
As mentioned earlier, Because workers run concurrently and are
independent of each other, and because goroutines cannot
directly return values to their caller, channels are used
as a coordination mechanism for workers to pull jobs and
publish results safely.
Take a look of this snippet where a caller publishes
jobs to workers.
{
...
numJobs := len(StudentItems)
jobs := make(chan models.StudentItem, numJobs)
for i := 0; i < numJobs; i++ {
jobs <- StudentItems[i]
}
close(jobs)
...
}
func InsertStudentWorker(ctx context.Context, tx *sql.Tx, data <-chan models.StudentItem, errCh chan<- error) {
for d := range data {
err := repository.InsertStudentItemFromWorker(ctx, tx, d)
...
}
}
Worker accepts a read-only channel argument (data <-chan models.StudentItem).
This means as task is availble on channel, whichever available worker
will recieves it. Recall that workers
are running concurrently, this means we don't care which worker
will pull task from caller.
Finally, we close jobs channel to prevent goroutine leak.
This illustration demonstrates second step.
SOCIAL SHARE CARD GENERATOR