CodeBlanks Logo Icon CodeBlanks

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.

Practice 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.

Starter code

main.go
package main

import "fmt"

func main() {
    // buraya yaz
}

Sample solution

main.go
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)
}

Expected output

5

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 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) Correct answer

Explanation: Goroutines are cheap, but if each job opens a file or makes a request, unbounded concurrency exhausts system resources.

2 Receive-only channel

Question
func isci(id int, isler ____ Is, sonuclar chan<- Sonuc) {
Answer
func isci(id int, isler <-chan Is, sonuclar chan<- Sonuc) {

Explanation: <-chan Is tells the compiler the worker can only receive from the channel.

3 Pool part

Term Meaning
isler Work to do
isci Fixed number of goroutines
sonuclar Outputs
Sonuc.Hata Per-job error

All lessons in Intermediate