GithubHelp home page GithubHelp logo

truonglvx / worker-pools Goto Github PK

View Code? Open in Web Editor NEW

This project forked from appboy/worker-pools

0.0 1.0 0.0 13 KB

Go package for managing a set of lazily constructed, self-expiring, concurrency-limited worker pools and their data

License: MIT License

Makefile 9.60% Go 90.40%

worker-pools's Introduction

worker-pools

Go Report Card

Go package for managing a set of lazily constructed, self-expiring, concurrency-limited worker pools.

maxConcurrentWorkloads := 500
stalePoolExpiration := 10*time.Minute
maxPoolLifetime := 4*time.Hour
poolManager := pool.NewWorkerPoolManager(
  maxConcurrentWorkloads, stalePoolExpiration, maxPoolLifetime,
)

pool, doneUsing := poolManager.Get("pool 1")
pool.Submit(func() {
  // Do anything here. Only maxConcurrentWorkloads will be allowed to execute concurrently per pool.
  // This is useful for limiting concurrent usage of external resources.
})
close(doneUsing)

Each pool instance is constructed when it is required and cached for stalePoolExpiration each time it is used, up to a maximum of maxPoolLifetime if the pool is receiving constant usage. Multiple goroutines may safely reserve and use pools concurrently. The pool will spin up worker routines lazily as they're required, allowing for large levels of concurrency and a high cardinality of pools in the manager.

If you want to attach shared data or behavior to each pool instance:

type myPooledData struct {
	pool.WorkerPool

	// You can put shared data of any type here
	myData string
}
func (p *myPooledData) Dispose() {
	p.WorkerPool.Dispose()
	// Release shared data here.
}

poolManager := pool.NewWorkerPoolManager(500, 10*time.Minute, 4*time.Hour)

var poolFactory pool.Factory = func(maxSize int) (pool.WorkerPool, error) {
	workerPool, _ := pool.NewWorkerPool(maxSize)

	// Build shared resources here, return errors, etc.

	return &myPooledData{
		WorkerPool: workerPool,
		myData: "my shared data"
	}, nil
}

pool, doneUsing, err := s.ClientBundleManager.GetPoolWithFactory("pool 1", sendSize, bundleFactory)
fmt.Println(pool.(*myPooledData).myData)
// Any error returned in the Factory function will bubble up here
if err != nil {
  // Handle
}
pool.Submit(func() {
  // Do anything here. Only maxConcurrentWorkloads will be allowed to execute concurrently per pool.
})
close(doneUsing)

See GoDoc for more details.

worker-pools's People

Contributors

froodian avatar

Watchers

 avatar

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.