GithubHelp home page GithubHelp logo

pool's Introduction

pool

English | δΈ­ζ–‡

A goroutine pool in golang.

Feature

  • goroutine count limit
  • goroutine reuse
  • set goroutine idle timeout
  • all goroutine shutdown gracefully

Install

go get:

$ go get -u github.com/sulybot/pool

go module:(with GO111MODULE=on)

$ ls
main.go
$ go mod init main
go: creating new go.mod: module main
$ go mod tidy
go: finding module for package github.com/sulybot/pool
go: found github.com/sulybot/pool in github.com/sulybot/pool v0.1.0
$ go run main.go

Usage

run anonymous func in pool:

package main
import (
	"fmt"
	"github.com/sulybot/pool"
)

func main() {
	// create a routine pool, max routine count 10, task cache queue 100
	p := pool.New(10, 100)
	// set routine idle timeout in second, default 10, set to 0 to disable timeout
	p.setIdleTimeout(60)

	for i := 0, i < 100; i++ {
		n := i

		// call Start to start a goroutine and execute the anonymous func
		p.Start(func() {
			fmt.Println(n)
		})
	}

	// call Shutdown to stop the pool and wait for all the goroutines shutdown
	<-p.Shutdown()
}

implements of pool.Runnable:

package main

import (
	"fmt"
	"github.com/sulybot/pool"
)

// define a struct
type Task struct {
	id int
}

// implement Run method for the struct
func (r *Task) Run() {
	fmt.Println(r.id)
}

func main() {
	p := pool.New(10, 100)
	p.SetIdleTimeout(60)

	for i := 0; i < 100; i++ {
		// declare a pool.Runnable variable
		var r pool.Runnable
		r = &Task{
			id: i,
		}

		// call start and pass the pool.Runnable
		p.Start(r)
	}

	<-p.Shutdown()
}

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.