Skip to content

Easy way to generate a worker pool with customs params

Notifications You must be signed in to change notification settings

phuanca/worker-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Nov 22, 2022
4dcbef6 · Nov 22, 2022

History

2 Commits
Nov 21, 2022
Nov 21, 2022
Nov 21, 2022
Nov 22, 2022
Nov 22, 2022

Repository files navigation

Worker Pool

This is a package that provides an worker pool executor to run any task that implement the interface.

This project is for testing purpose.

    type Task interface{
        Execute()
    }    

Installation

To install worker-pool, use go get:

    go get github.com/phuanca/worker-pool

Example

package your_package

import (
 "log"

 workerpool "github.com/phuanca/worker-pool"
)

// Define struct that implement the executor.Task interface
type sendMessage struct {
 id  int
 msg string
}

func (s sendMessage) Execute() {
 start := time.Now()
 log.Printf("msg_id: %v, doing some work", s.id)
 r := rand.Intn(5000)
 time.Sleep(time.Duration(r) * time.Millisecond)
 log.Printf("msg_id: %v, %v in: %v", s.id, s.msg, time.Since(start))
}

func main() {
 workers := 2 // workers
 maxQueueDepth := 2 // queue size
 
 // create an executor
 wp := workerpool.NewService(workers, maxQueueDepth)
 defer wp.Exit() // you can stop all worker when needed.

 // make executor ready to receive messages (work).
 wp.Run()

 for i := 0; i < 10; i++ {
  number := i
  task := sendMessage{id: number, msg: "snapshot saved"}

  // send task to executor.
  wp.Send(task)
 }
 log.Printf("====== all task sended! ====== ")
 time.Sleep(10 * time.Second)
}

About

Easy way to generate a worker pool with customs params

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages