Go
Worker pool skeleton
Design the worker pool structure and channels.
Project: a worker pool
In this project you will build a worker pool that computes file sizes. A worker pool has three parts: a job channel (the work to do), a fixed number of workers (goroutines) and a result channel.
isler := make(chan Is)
sonuclar := make(chan Sonuc)
for w := 1; w <= 3; w++ {
go isci(w, isler, sonuclar)
}
Keeping the worker count fixed makes resource use predictable even with thousands of jobs.
Define the types
The job and result types are written first; the channels carry these types:
type Is struct {
ID int
Yol string
}
type Sonuc struct {
IsID int
Boyut int64
Hata error
}
Having an error field inside the result means one failing job doesn't stop the whole pool.
Exercise Pool skeleton
Define the Is and Sonuc types. isci(isler <-chan Is, sonuclar chan<- Sonuc) sends Sonuc{IsID: is.ID, Boyut: int64(len(is.Yol))} for every job. Run the job Is{1, "a.txt"} with a single worker and print the result's Boyut.
Show example solution
package main
import "fmt"
type Is struct {
ID int
Yol string
}
type Sonuc struct {
IsID int
Boyut int64
Hata error
}
func isci(isler <-chan Is, sonuclar chan<- Sonuc) {
for is := range isler {
sonuclar <- Sonuc{IsID: is.ID, Boyut: int64(len(is.Yol))}
}
}
func main() {
isler := make(chan Is, 1)
sonuclar := make(chan Sonuc, 1)
go isci(isler, sonuclar)
isler <- Is{1, "a.txt"}
close(isler)
fmt.Println((<-sonuclar).Boyut)
}
Test yourself
-
Why use a pool instead of a goroutine per job?
- Because starting goroutines is forbidden
- Because channels only work in a pool
- To disable the GC
- To limit concurrent work (file handles, connections)
Show answer
To limit concurrent work (file handles, connections) — Goroutines are cheap, but if each job opens a file or makes a request, unbounded concurrency exhausts system resources.
-
Receive-only channel
func isci(id int, isler ____ Is, sonuclar chan<- Sonuc) {
Show answer
func isci(id int, isler <-chan Is, sonuclar chan<- Sonuc) {
-
Pool part
- isler
- Work to do
- isci
- Fixed number of goroutines
- sonuclar
- Outputs
- Sonuc.Hata
- Per-job error
Next lesson: Job queue