Skip to content

🦙 An implementation of worker pool pattern for concurrency control in golang.

License

Notifications You must be signed in to change notification settings

sinomoe/goworker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goworker

MIT

An implementation of worker pool pattern for concurrency control in golang.

Get started

import (
    "fmt"
    "time"
    
    "github.com/sinomoe/goworker/pool"
    "github.com/sinomoe/goworker/work"
)

// task A
func myWorkA(workerID int, work *work.DefaultWork) {
    fmt.Printf("Woker[%d] run work[%s]\n", workerID, work.Hash())
    time.Sleep(time.Second / 4)
}

// task B
func myWorkB(workerID int, work *work.DefaultWork) {
    fmt.Printf("Woker[%d] run work[%s]\n", workerID, work.Hash())
    time.Sleep(time.Second / 2)
}

func main() {
    // start pool 
    c := pool.StartDispatcher(4)
    
    // send task to collector
    c.Send(work.HandleFunc(myWorkA))
    c.Send(work.HandleFunc(myWorkB))
    
    // wait end
    c.End()
}

Further

  1. define your work type

    type Work struct {
        ID  int
        // ...
    }
  2. implement its Workable interface

    func (w *Work) Do(workerId int) {
        // ...
    }
  3. start a dispatcher and then returns the collector

    c := pool.StartDispatcher(4)
  4. send works to collector

    c.Send(&Work{
        ID: id,
        // ...
    })
  5. stop collector

    c.End()

for more, see example.go

About

🦙 An implementation of worker pool pattern for concurrency control in golang.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages