wasps

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2020 License: MIT Imports: 2 Imported by: 0

README

wasps

Build Status Go Report Card Codecov Doc for wasps

English | 中文

Introduction

wasps is a lightweight goroutine pool that implements scheduling management for multiple goroutines.

Features:

  • Automatic scheduling goroutine.
  • Provides commonly-used interfaces: task submission, getting the number of running goroutines, dynamically adjusting the size of the pool, and releasing the pool.
  • Provide callback type goroutine pool, serialization work goroutine pool, custom work goroutine pool.
  • Support custom work goroutine, support panic processing of task goroutine, and custom pass parameter of closure function.
  • Asynchronous mechanism

Docs

https://godoc.org/github.com/yankooo/wasps

Installation

go get github.com/yankooo/wasps

Use

package main

import (
	"context"
	"fmt"
	"github.com/yankooo/wasps"
	"log"
	"sync"
	"time"
)

func main()  {
	pool := wasps.NewCallback(5)
	defer func() {
		pool.Release()
	}()

	var num = 10
	ctx, _ := context.WithTimeout(context.TODO(), 1*time.Second)

	var wg sync.WaitGroup

	wg.Add(3)
	pool.SubmitWithContext(ctx, func(args ...interface{}) {
		defer wg.Done()
		num := args[0].(int)
		log.Printf("first submit %d", num*2)
	}, wasps.WithArgs(num))

	pool.Submit(func(args ...interface{}) {
		defer wg.Done()
		num := args[0].(int)
		log.Printf("second submit %d", num)
	}, wasps.WithArgs(num), wasps.WithRecoverFn(func(r interface{}) { fmt.Printf("catch panic: %+v\n", r) }))

	num = 200
	pool.Submit(func(args ...interface{}) {
		defer wg.Done()
		log.Printf("third submit %d", num)
	})

	wg.Wait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	Ctx       context.Context
	Task      interface{}
	Args      []interface{}
	RecoverFn func(r interface{})
}

Job is the struct that represents the smallest unit of worker tasks

type Pool

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

Pool is a struct that manages a collection of workers, each with their own goroutine. The Pool can initialize, expand, compress and close the workers, as well as asynchronously process submitted tasks.

func New

func New(capacity int, ctor func() Worker) *Pool

New creates a new Pool of workers that starts with a number of workers. You must provide a constructor function that creates new Worker types and when you change the capacity of the pool the constructor will be called to create a new Worker. capacity - how many workers will be created for this pool and size of the pool. ctor - constructor function that creates new Worker types

func NewCallback

func NewCallback(capacity int) *Pool

NewCallback creates a new Pool of workers where workers cast the Job payload into a func() and runs it, or returns ErrNotFunc if the cast failed.

func (*Pool) Capacity

func (p *Pool) Capacity() int

Capacity returns the current capacity of the pool.

func (*Pool) Release

func (p *Pool) Release()

Release will terminate all workers and close the channel of this Pool.

func (*Pool) SetCapacity

func (p *Pool) SetCapacity(size int)

SetCapacity changes the capacity of the pool and the total number of workers in the Pool. This can be called by any goroutine at any time unless the Pool has been stopped, in which case a panic will occur.

func (*Pool) Stats

func (p *Pool) Stats() *Stats

Stats returns information during the running of the goroutine pool.

func (*Pool) Submit

func (p *Pool) Submit(pl payLoad, opts ...TaskOption)

Submit will submit a task to the task queue of the goroutine pool.

func (*Pool) SubmitWithContext

func (p *Pool) SubmitWithContext(ctx context.Context, pl payLoad, opts ...TaskOption)

SubmitWithContext will submit a task to the task queue of the coroutine pool, accompanied by a context. Before the task is executed, if the context is canceled, the task will not be executed.

type PoolOption

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

PoolOption configures how we set up the connection.

type Stats

type Stats struct {
	Cap         int // goroutine pool capacity
	IdleWorker  int // Number of work goroutines in idle state
	WaitingTask int // Number of tasks waiting to be processed
}

Stats contains running pool Infos.

type TaskOption

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

TaskOption configures how we set up the connection.

func WithArgs

func WithArgs(args ...interface{}) TaskOption

WithArgs returns task option for callback func args

func WithRecoverFn

func WithRecoverFn(f func(r interface{})) TaskOption

WithRecoverFn returns task option for recover to catch panic

type Worker

type Worker interface {
	// Do will process a job.
	Do(job *Job)

	// return job channel of worker
	JobChan() chan *Job

	// return stop channel of worker
	StopChan() chan struct{}
}

Worker is an interface representing a wasps working agent. It will be used to process a job of own job channel, and clean up its resources when being removed from the pool.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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