GithubHelp home page GithubHelp logo

gasparian / worker-pool-go Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 28 KB

Implementation of worker pool with a variable balancing strategy

License: MIT License

Go 99.07% Makefile 0.93%
worker-pool worker-pool-go go-workerpool go-workers-pool golang go go-concurrency

worker-pool-go's Introduction

tests

worker-pool-go

Implementation of worker pool with extendable functionality.
By default, I've implemented "round-robin" balancing to evenly distribute jobs across workers.
You need to implement the BalancingStrategy interface in order to use other balancing strategies:

type BalancingStrategy interface {
	NextWorkerId(workersStats []Stats) int
}

API:

// worker pool struct private api
(wp *WorkerPool) getCurrentJobsNumber() int64
(wp *WorkerPool) getWorkerStats(workerId int) (Stats, error)
(wp *WorkerPool) terminateWorker(workerId int) error
(wp *WorkerPool) reloadWorker(workerId int) error

// worker pool public methods
// New creates new instance of workers pool
// params defining it's size and max jobs per worker (without blocking)
NewWorkerPool(nWorkers, maxJobs uint, balancer BalancingStrategy) *WorkerPool
// ScheduleJob puts new job in some worker queue
(wp *WorkerPool) ScheduleJob(f JobFunc) (chan Result, error)

// NewRoundRobin creates new instance of the "load balancer"
NewRoundRobin() *RoundRobin
// NextWorkerId returns worker id selected by some
// predefined policy
(rr *RoundRobin) NextWorkerId(workerStats []Stats) int

Install:

go get -u github.com/gasparian/worker-pool-go

Usage example:

package main

import (
    "fmt"
    wp "github.com/gasparian/worker-pool-go"
)

// Use closure to create job function
func exampleJob(inp int) wp.JobFunc {
    return func() wp.Result {
        res := inp * inp
        return wp.Result{
            Data: res,
            Err:  nil,
        }
    }
}

func main() {
    pool := NewWorkerPool(
        3,
        10,
        NewRoundRobin(),
    )

    // NOTE: if number of jobs per woker will be > MaxJobs, 
    //       ScheduleJob func will block
    nJobs := 50
    results := make([]chan wp.Result, 0)
    for i := 0; i < nJobs; i++ {
        ch := pool.ScheduleJob(exampleJob(i))
        results = append(results, ch)
    }
    
    for _, r := range results {
        res := <-r
        fmt.Println(res)
    }
}

Run tests:

make test

worker-pool-go's People

Contributors

gasparian avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

sparklive

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.