├── LICENSE.md ├── README.md ├── doc.go └── gotowork.go /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Brenton Morris 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gotowork 2 | Provides a simple and easy to use worker queue for golang. 3 | 4 | ## Example 5 | ```go 6 | package main 7 | 8 | import ( 9 | "fmt" 10 | work "github.com/scrapbird/gotowork" 11 | ) 12 | 13 | func main () { 14 | // create the workerqueue 15 | workerQueue := make(work.WorkerQueue, 5) 16 | 17 | // create and start the workers 18 | workers := make([]work.Worker, 5) 19 | for i := range workers { 20 | workers[i] = work.NewWorker(i, workerQueue) 21 | workers[i].Start() 22 | } 23 | 24 | // add some jobs 25 | for i := 0; i < 5; i++ { 26 | // get a free worker 27 | worker := <-workerQueue 28 | // give it some work 29 | worker <- func () { 30 | fmt.Println("Hello") 31 | } 32 | } 33 | 34 | // tell the workers to stop waiting for work 35 | for i := range workers { 36 | workers[i].Stop() 37 | } 38 | 39 | // wait for the workers to quit 40 | for i := range workers { 41 | workers[i].WaitForFinish() 42 | } 43 | } 44 | ``` 45 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package gotowork implements a simple worker pool. 3 | 4 | First we create a WorkerQueue that can hold up to 5 workers 5 | 6 | workerQueue := make(gotowork.WorkerQueue, 5) 7 | 8 | Next we create 5 workers and start each one 9 | 10 | for i := range workers { 11 | workers[i] = gotowork.NewWorker(i, workerQueue) 12 | workers[i].Start() 13 | } 14 | 15 | Then we add some jobs to the queue 16 | 17 | for i := 0; i < 5; i++ { 18 | // get a free worker 19 | worker := <-workerQueue 20 | // give it some work (this can be any function) 21 | worker <- func () { 22 | fmt.Println("Hello") 23 | } 24 | } 25 | 26 | Once all work has been dispatched we tell the workers to stop 27 | 28 | for i := range workers { 29 | workers[i].Stop() 30 | } 31 | 32 | Then wait for all the workers to finish the current jobs 33 | 34 | for i := range workers { 35 | workers[i].WaitForFinish() 36 | } 37 | */ 38 | package gotowork 39 | -------------------------------------------------------------------------------- /gotowork.go: -------------------------------------------------------------------------------- 1 | // Provides a simple and easy to use worker queue. 2 | package gotowork 3 | 4 | // Define work as a first class function 5 | type Work func() 6 | 7 | // Define the worker queue 8 | type WorkerQueue chan Inbox 9 | 10 | // Define the inbox 11 | type Inbox chan Work 12 | 13 | // This is the worker type, this is used to keep track of which queue the worker is in and the worker id, signaling channels etc. 14 | type Worker struct { 15 | id int 16 | inbox Inbox 17 | workerQueue WorkerQueue 18 | quit chan bool 19 | done chan bool 20 | } 21 | 22 | // Creates a new worker struct and initializes it. 23 | // 24 | // workerQueue := make(WorkerQueue, 5) // Create worker queue that fits up to 5 workers. 25 | // worker := NewWorker(1, workerQueue) // Create a new worker in the queue. 26 | func NewWorker(id int, workerQueue WorkerQueue) Worker { 27 | worker := Worker{ 28 | id: id, 29 | inbox: make(chan Work), 30 | workerQueue: workerQueue, 31 | quit: make(chan bool), 32 | done: make(chan bool), 33 | } 34 | return worker 35 | } 36 | 37 | // Tells the worker to start subscribing to the worker queue and executing jobs. 38 | func (w Worker) Start() { 39 | go func() { 40 | for { 41 | // signal that we are ready for work 42 | w.workerQueue <- w.inbox 43 | select { 44 | case work := <-w.inbox: 45 | work() 46 | case <-w.quit: 47 | w.done <- true 48 | return 49 | } 50 | } 51 | }() 52 | } 53 | 54 | // Tells the worker to stop once it has finished it's current job. 55 | func (w Worker) Stop() { 56 | go func() { 57 | w.quit <- true 58 | }() 59 | } 60 | 61 | // Waits for a worker to finish before returning. 62 | func (w Worker) WaitForFinish() { 63 | <-w.done 64 | } 65 | --------------------------------------------------------------------------------