routinepool

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2025 License: MIT Imports: 4 Imported by: 0

README

routine pool - Golang routine pool for multi asynchronous tasks.

GitHub Tag Go Reference Go Report Card Build Status Coverage License

Supports
  • Automatic scaling Number of routines by the number of tasks.
  • Support waiting all tasks finished.
  • Panic in tasks will be recovered.
Usages

Write a http benchmark tool with routinepool, calculate the average time of each request.

package main

import (
	"fmt"
	"io"
	"net/http"
	"sync"
	"sync/atomic"
	"time"

	"github.com/icefed/routinepool"
)

func main() {
	p := routinepool.New(routinepool.WithMaxWorkers(8))
	p.StartN(8)

	var errCount int32
	client := &http.Client{
		Transport: &http.Transport{
			MaxIdleConnsPerHost: 8,
		},
	}
	costs := make([]time.Duration, 0)
	mu := sync.Mutex{}

	f := func() {
		start := time.Now()
		defer func() {
			mu.Lock()
			defer mu.Unlock()
			costs = append(costs, time.Since(start))
		}()
		req, _ := http.NewRequest("GET", "http://localhost:8099/hello", nil)
		resp, err := client.Do(req)
		io.Copy(io.Discard, resp.Body)
		if err != nil {
			atomic.AddInt32(&errCount, 1)
			return
		}
		resp.Body.Close()
	}
	for i := 0; i < 100000; i++ {
		p.AddTask(f)
	}
	p.Wait()

	avg := time.Duration(0)
	total := time.Duration(0)
	for _, cost := range costs {
		total += cost
	}
	avg = total / time.Duration(len(costs))
	fmt.Printf("total requests: %d, avg cost: %s, err count: %d\n", len(costs), avg, errCount)
}

Documentation

Index

Constants

View Source
const WorkerIdleTimeoutToExit = time.Second

worker idle timeout to exit

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option defines pool options

func WithMaxTaskSize

func WithMaxTaskSize(maxTaskSize int) Option

WithMaxTaskSize set max task size, default use runtime.NumCPU().

func WithMaxWorkers

func WithMaxWorkers(maxWorkers int) Option

WithMaxWorkers set max worker routines, default is runtime.NumCPU().

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool worker routine pool

func New added in v0.2.0

func New(opts ...Option) *Pool

New create a routine pool with options.

func (*Pool) AddTask

func (p *Pool) AddTask(task TaskFunc)

AddTask add a task to Pool, the worker routine will execute the task. if task channel is full, it will be blocked.

func (*Pool) Start

func (p *Pool) Start()

Start start routine pool in background. The initial number of workers is determined by the number of tasks in the channel and maxWorkers.

func (*Pool) StartN

func (p *Pool) StartN(workerNum int)

StartN start routine pool in background with workerNum initial workers. if workerNum <= 0 or workerNum > maxWorkers, it will be set to maxWorkers.

func (*Pool) Stop

func (p *Pool) Stop()

Stop stop all routines no matter how many tasks wait for execution.

func (*Pool) Wait

func (p *Pool) Wait()

Wait for blocking, and wait for all tasks to be executed and no tasks to be executed, then stop the pool. if taskChan never empty, it wouldn't return.

func (*Pool) WaitIdleTimeout added in v0.3.0

func (p *Pool) WaitIdleTimeout(idleTimeout time.Duration)

WaitIdleTimeout waiting with idle timeout, then stop the pool. if taskChan never empty, it wouldn't return.

type TaskFunc

type TaskFunc func()

TaskFunc defines a task function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL