Skip to content

Instantly share code, notes, and snippets.

@nesv
Last active April 12, 2018 11:59
Show Gist options
  • Select an option

  • Save nesv/9220339 to your computer and use it in GitHub Desktop.

Select an option

Save nesv/9220339 to your computer and use it in GitHub Desktop.

Revisions

  1. nesv revised this gist May 12, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions worker.go
    Original 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() {
    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() {
    func (w *Worker) Stop() {
    go func() {
    w.QuitChan <- true
    }()
  2. nesv revised this gist Feb 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion worker.go
    Original 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 %d seconds", w.ID, work.Delay.Seconds())
    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)
  3. nesv revised this gist Feb 26, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions worker.go
    Original 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
    }

    // Add ourselves into the worker queue.
    w.WorkerQueue <- w.Work
    }
    }()
    }
  4. nesv revised this gist Feb 26, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions worker.go
    Original 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)

  5. nesv revised this gist Feb 26, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion worker.go
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    package main

    import "fmt"
    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
  6. nesv revised this gist Feb 26, 2014. 1 changed file with 2 additions and 9 deletions.
    11 changes: 2 additions & 9 deletions worker.go
    Original file line number Diff line number Diff line change
    @@ -2,20 +2,13 @@ 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

    // 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++

    func NewWorker(id int, workerQueue chan chan WorkRequest) Worker {
    // Create, and return the worker.
    worker := Worker{
    ID: wid,
    ID: id,
    Work: make(chan WorkRequest),
    WorkerQueue: workerQueue,
    QuitChan: make(chan bool)}
  7. nesv revised this gist Feb 26, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions worker.go
    Original 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
  8. nesv revised this gist Feb 26, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions worker.go
    Original file line number Diff line number Diff line change
    @@ -33,9 +33,6 @@ 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.
    @@ -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
    }
    }()
    }
  9. nesv revised this gist Feb 26, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions worker.go
    Original 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
    }

    // Re-add ourselves back into the worker queue.
    w.WorkerQueue <- w.Work
    }
    }()
    }
  10. nesv revised this gist Feb 25, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion worker.go
    Original 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)}
    worker := Worker{
    ID: wid,
    Work: make(chan WorkRequest),
    WorkerQueue: workerQueue,
    QuitChan: make(chan bool)}

    return worker
    }

  11. nesv created this gist Feb 25, 2014.
    56 changes: 56 additions & 0 deletions worker.go
    Original 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
    }()
    }