Skip to content

Instantly share code, notes, and snippets.

@hackintoshrao
Created July 14, 2015 03:58
Show Gist options
  • Save hackintoshrao/f9a22046a96cc255c8eb to your computer and use it in GitHub Desktop.
Save hackintoshrao/f9a22046a96cc255c8eb to your computer and use it in GitHub Desktop.
worker.go
package main
import (
"fmt"
"io/ioutil"
//"net/http"
//"time"
)
// NewWorker creates, and returns a new Worker object. Its only argument
// is a channel that the worker can add itself to whenever it is done its
// work.
func NewWorker(id int, workerQueue chan chan WorkRequest) Worker {
// Create, and return the worker.
worker := Worker{
ID: id,
Work: make(chan WorkRequest),
WorkerQueue: workerQueue,
QuitChan: make(chan bool)}
return worker
}
type Worker struct {
ID int
Work chan WorkRequest
WorkerQueue chan chan WorkRequest
QuitChan chan bool
}
// This function "starts" the worker by starting a goroutine, that is
// an infinite "for-select" loop.
func (w Worker) Start() {
go func() {
for {
// Add ourselves into the worker queue.
w.WorkerQueue <- w.Work
select {
case work := <-w.Work:
// Receive a work request.
fmt.Printf("worker%d: Received work request, reading file\n", w.ID, work.Name)
dat, err := ioutil.ReadFile(work.Name)
if err != nil {
fmt.Printf("Error reading file" + err.Error())
//http.Error(work.Writer, "Read Error: "+err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(work.Writer, string(dat))
fmt.Printf(string(dat))
case <-w.QuitChan:
// We have been asked to stop.
fmt.Printf("worker%d stopping\n", w.ID)
return
}
}
}()
}
// Stop tells the worker to stop listening for work requests.
//
// Note that the worker will only stop *after* it has finished its work.
func (w Worker) Stop() {
go func() {
w.QuitChan <- true
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment