Giter Club home page Giter Club logo

grpool's Introduction

grpool

Introduction

grpool is a groutine pool which can provide a fixed size of capacity, recycle the stale workers.

Installation

go get -u github.com/POABOB/grpool

How to use

If you want to process a massive number of jobs, you don't need to use the same number of goroutines. It's wasteful to use excessive memory, leading to high consumption.

A Simple Example with Default Pool

package main

import (
	"fmt"
	"strconv"
	"sync"
	"sync/atomic"

	"github.com/POABOB/grpool"
)

var wg sync.WaitGroup
var count int32 = 0

func main() {
	// Use default Pool Will use the math.MaxInt32 of capacity.
	pool := grpool.NewDefaultPool()
	// Release worker resource
	defer pool.Release()

	for i := 0; i < 10000; i++ {
		wg.Add(1)
		_ = pool.Schedule(func() {
			printFunc(i)
		})
	}

	wg.Wait()
    
	fmt.Printf("running goroutines: %d\n", pool.Running())
}

func printFunc(i int) {
	atomic.AddInt32(&count, 1)
	fmt.Println("Hello World! I am worker" + strconv.Itoa(i) + " from goroutine pool.")
	wg.Done()
}

Limit the goroutines and pre-alloc it

package main

import (
	"fmt"
	"strconv"
	"sync"
	"sync/atomic"

	"github.com/POABOB/grpool"
)

var count int32 = 0
var wg sync.WaitGroup

func main() {
	// init a capacity of 18 goroutine pool with preallocing the space.
	pool, _ := grpool.NewPool(18, grpool.WithPreAlloc(true))
	// Release worker resource
	defer pool.Release()

	for i := 0; i < 10; i++ {
		wg.Add(1)
		_ = pool.Schedule(func() {
			printFunc(i)
		})
	}

	wg.Wait()

	fmt.Printf("running goroutines: %d\n", pool.Running())
}

func printFunc(i int) {
	atomic.AddInt32(&count, 1)
	fmt.Println("Hello World! I am worker" + strconv.Itoa(i) + " from goroutine pool.")
	wg.Done()
}

Use non-blocking pool

pool, err := grpool.NewPool(1000, grpool.WithNonblocking(true))

Customize panic handler

func ph(v interface{}) {
    // dosomething...
	fmt.Printf("[panic occurred]: %v\n", v)
}
pool, err := grpool.NewPool(1000, grpool.WithPanicHandler(ph))

Customize the time interval of clear stale worker

pool, err := grpool.NewPool(1000, grpool.WithExpiryDuration(time.Second * 5))

License

MIT License

grpool's People

Contributors

poabob avatar

Stargazers

 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.