Skip to content

rfyiamcool/gpool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

gpool

goroutine pool

Feature

  • resize worker num
  • worker idle timeout
  • aysnc & sync mode

Usage

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/rfyiamcool/gpool"
)

var (
	wg = sync.WaitGroup{}
)

func main() {
	gp, err := gpool.NewGPool(&gpool.Options{
		MaxWorker:   5,                 // ζœ€ε€§ηš„εη¨‹ζ•°
		MinWorker:   2,                 // ζœ€ε°ηš„εη¨‹ζ•°
		JobBuffer:   1,                 // ηΌ“ε†²ι˜Ÿεˆ—ηš„ε€§ε°
		IdleTimeout: 120 * time.Second, // εη¨‹ηš„η©Ίι—²θΆ…ζ—Άι€€ε‡Ίζ—Άι—΄
	})

	if err != nil {
		panic(err.Error())
	}

	for index := 0; index < 1000; index++ {
		wg.Add(1)
		idx := index
		gp.ProcessAsync(func() {
			fmt.Println(idx, time.Now())
			time.Sleep(1 * time.Second)
			wg.Done()
		})
	}

	wg.Done()
}

About

πŸ˜€ golang goroutine pool

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages