Skip to content
/ wasps Public

wasps is a lightweight goroutine pool for golang, use limited goroutines to achieve multi-task concurrent execution.

License

Notifications You must be signed in to change notification settings

yankooo/wasps

Repository files navigation

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)
}

About

wasps is a lightweight goroutine pool for golang, use limited goroutines to achieve multi-task concurrent execution.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages