Last active
April 12, 2018 11:59
-
-
Save nesv/9220339 to your computer and use it in GitHub Desktop.
Revisions
-
nesv revised this gist
May 12, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ type Worker struct { // 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. @@ -54,7 +54,7 @@ func (w Worker) Start() { // 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 }() -
nesv revised this gist
Feb 26, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ func (w Worker) Start() { select { case work := <-w.Work: // Receive a work request. fmt.Printf("worker%d: Received work request, delaying for %f seconds\n", w.ID, work.Delay.Seconds()) time.Sleep(work.Delay) fmt.Printf("worker%d: Hello, %s!\n", w.ID, work.Name) -
nesv revised this gist
Feb 26, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -31,6 +31,9 @@ type Worker struct { 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. @@ -44,9 +47,6 @@ func (w Worker) Start() { fmt.Printf("worker%d stopping\n", w.ID) return } } }() } -
nesv revised this gist
Feb 26, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,6 +34,8 @@ func (w Worker) Start() { select { case work := <-w.Work: // Receive a work request. fmt.Printf("worker%d: Received work request, delaying for %d seconds", w.ID, work.Delay.Seconds()) time.Sleep(work.Delay) fmt.Printf("worker%d: Hello, %s!\n", w.ID, work.Name) -
nesv revised this gist
Feb 26, 2014 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,9 @@ package main import ( "fmt" "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 -
nesv revised this gist
Feb 26, 2014 . 1 changed file with 2 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,20 +2,13 @@ package main import "fmt" // 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)} -
nesv revised this gist
Feb 26, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ package main import "fmt" // Variable "wid" is nothing more than a counter that we will increment // every time a new worker is created. var wid = 0 -
nesv revised this gist
Feb 26, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -33,9 +33,6 @@ type Worker struct { func (w Worker) Start() { go func() { for { select { case work := <-w.Work: // Receive a work request. @@ -47,6 +44,9 @@ func (w Worker) Start() { fmt.Printf("worker%d stopping\n", w.ID) return } // Add ourselves into the worker queue. w.WorkerQueue <- w.Work } }() } -
nesv revised this gist
Feb 26, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -33,6 +33,9 @@ type Worker struct { 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. @@ -44,9 +47,6 @@ func (w Worker) Start() { fmt.Printf("worker%d stopping\n", w.ID) return } } }() } -
nesv revised this gist
Feb 25, 2014 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,12 @@ func NewWorker(workerQueue chan chan WorkRequest) Worker { wid++ // Create, and return the worker. worker := Worker{ ID: wid, Work: make(chan WorkRequest), WorkerQueue: workerQueue, QuitChan: make(chan bool)} return worker } -
nesv created this gist
Feb 25, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ package main // Variable "wid" is nothing more than a counter that we will increment // every time a new worker is created. var wid = 0 // 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(workerQueue chan chan WorkRequest) Worker { // Increment the worker counter. wid++ // Create, and return the worker. worker := Worker{ID: wid, 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 { select { case work := <-w.Work: // Receive a work request. time.Sleep(work.Delay) fmt.Printf("worker%d: Hello, %s!\n", w.ID, work.Name) case <-w.QuitChan: // We have been asked to stop. fmt.Printf("worker%d stopping\n", w.ID) return } // Re-add ourselves back into the worker queue. w.WorkerQueue <- w.Work } }() } // 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 }() }