github.com/arielsrv/taskpool

Task builder inspired in Java Thread Pool


Keywords
golang, pool, taskbuilder, threading, worker-pool, workers
License
MIT
Install
go get github.com/arielsrv/taskpool

Documentation

taskpool

Coverage

CI Coverage Update release version.

Developer tools

For macOs

$ brew install go-task/tap/go-task
$ brew install golangci-lint
$ go install github.com/oligot/go-mod-upgrade@latest
$ brew install jq

Table of contents

Installation

go get -u github.com/arielsrv/taskpool

⚡️ Quickstart

package main

import (
	"github.com/arielsrv/taskpool"
	"log"
	"math/rand"
	"runtime"
	"time"
)

func main() {
	// generics task, return error is mandatory, @todo: fire and forget
	var task1, task2, task3 *task.Task[int]

	tb := &task.Builder{
		MaxWorkers: runtime.NumCPU() - 1,
	}

	start := time.Now()
	tb.ForkJoin(func(c *task.Awaitable) {
		task1 = task.Await[int](c, GetNumber)
		task2 = task.Await[int](c, GetNumber)
		task3 = task.Await[int](c, GetNumber)
	})

	log.Println(task1.Result)
	log.Println(task2.Result)
	log.Println(task3.Result)

	end := time.Since(start)
	log.Println(end)
}

func GetNumber() (int, error) {
	value := rand.Int()
	time.Sleep(time.Millisecond * 1000)
	return value, nil
}