Skip to content

Latest commit

 

History

History
91 lines (68 loc) · 2.76 KB

README.md

File metadata and controls

91 lines (68 loc) · 2.76 KB

wasps

Build Status Go Report Card Codecov Doc for wasps GitHub

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() {
    // init pool
    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)
    
    // submit first task
    var f1 wasps.DefaultWorkerPayLoad = func(ctx context.Context, args ...interface{}) {
        defer wg.Done()
        num := args[0].(int)
        log.Printf("first submit %d", num*2)
    }
    pool.SubmitWithContext(ctx, f1, wasps.WithArgs(num))
    
    // submit secod task
    var f2 wasps.DefaultWorkerPayLoad = func(ctx context.Context, args ...interface{}) {
        defer wg.Done()
        num := args[0].(int)
        log.Printf("second submit %d", num)
    }
    pool.Submit(f2, wasps.WithArgs(num), wasps.WithRecoverFn(func(r interface{}) { fmt.Printf("catch panic: %+v\n", r) }))
    
    // submit thrid task
    num = 200
    var f3 wasps.DefaultWorkerPayLoad = func(ctx context.Context, args ...interface{}) {
        defer wg.Done()
        log.Printf("third submit %d", num)
    }
    pool.Submit(f3)

    wg.Wait()

    time.Sleep(time.Second*3)
    
    // submit single task don't use sync group
    pool.Do(func(ctx context.Context, args ...interface{}) {
        log.Printf("four submit %+v", args)
    })

    time.Sleep(time.Hour)
}