routinepool

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 4 Imported by: 0

README

routine pool - Golang routine pool for multi asynchronous tasks.

GoDoc 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.NewPool(routinepool.WithMaxWorkers(8))
	p.StartN(8)

	var errCount int32
	client := &http.Client{
		Transport: &http.Transport{
			MaxConnsPerHost: 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

This section is empty.

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 WithIdleTimeout

func WithIdleTimeout(idleTimeout time.Duration) Option

WithIdleTimeout set idle timeout, default is 1s. if idle timeout is less than 1s, it will be set to 1s. worker routine will be closed if no task received in idle timeout. if new task received, new worker will be created.

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 NewPool

func NewPool(opts ...Option) *Pool

NewPool 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 blocking, waiting all tasks be executed and no tasks to execute in idle timeout(default 1s), then stop the pool, if taskChan never empty, it wouldn't return.

func (*Pool) WaitTimeout

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

WaitTimeout waiting with worker 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