task

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

README

████████╗░█████╗░░██████╗██╗░░██╗
╚══██╔══╝██╔══██╗██╔════╝██║░██╔╝
░░░██║░░░███████║╚█████╗░█████═╝░
░░░██║░░░██╔══██║░╚═══██╗██╔═██╗░
░░░██║░░░██║░░██║██████╔╝██║░╚██╗
░░░╚═╝░░░╚═╝░░╚═╝╚═════╝░╚═╝░░╚═╝

Introduction

A lightweight and efficient task runner designed to manage and execute tasks concurrently. This library is ideal for building worker pools and scheduling asynchronous jobs with ease.

Features

  • Worker Pool: Efficiently manage multiple concurrent tasks with configurable worker limits.
  • Task Scheduling: Supports scheduling tasks to run in the background.
  • Context Management: Handle context propagation for better control over task lifecycle.
  • Easy Integration: Seamlessly integrate into any Go project.
  • Can yeild the function to future, similar to runtime.Gosched()

Installation

To install the package, use the following command:

go get ella.to/task

Usage

Below is a basic example of how to use the Task Runner:

package main

import (
    "context"
    "sync/atomic"

    "ella.to/task"
)


func main() {
    runner := task.NewRunner(
		task.WithBufferSize(10),
		task.WithWorkerSize(10),
	)

	var count atomic.Int64
	for i := 0; i < 100; i++ {
		runner.Submit(context.TODO(), func(ctx context.Context) error {
			count.Add(1)
			return nil
		})
	}

	runner.Close(context.TODO())

    if count.Load() != 100 {
        panic("expect count to be 100")
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRunnerClosed        = errors.New("runner is closed")
	ErrRunnerAlreadyClosed = errors.New("runner is already closed")
)

Functions

func NewMergedContext added in v0.0.2

func NewMergedContext(ctx1, ctx2 context.Context) context.Context

NewMergedContext returns a new context that merges two contexts.

func WithBufferSize

func WithBufferSize(bufferSize int) runnerOpt

WithBufferSize sets the buffer size of the tasks channel. if the buffer size is less than 0, it will be set to 1.

func WithDelay added in v0.0.4

func WithDelay(delay time.Duration) yeildOpt

func WithWorkerSize

func WithWorkerSize(worker int) runnerOpt

WithWorkerSize sets the number of workers that will be used to execute the submitted tasks. if the worker size is less than or equal to 0, it will be set to 1.

func Yeild added in v0.0.3

func Yeild(ctx context.Context, opts ...yeildOpt) *yeild

Types

type Future

type Future interface {
	Await(ctx context.Context) error
}

Future represents a task that will be executed in the future. The Await function will block until the task is completed. If the context passed to the Await function is canceled, the Await function will return but the task will still be executed. if the task needs to be canceled, the context passed to the Submit function should be canceled.

type Runner

type Runner interface {
	// Submit a task to be executed by the runner's workers. the context passed to the Submit function will be merged with the runner's context.
	// and the merged context will be passed to the submitted function. This ensures that if the runner is closed, the submitted function will
	// be canceled.
	Submit(ctx context.Context, fn func(context.Context) error) Future
	// Close the runner and wait for all the submitted tasks to be completed.
	// Calling this function again after closure will return ErrRunnerAlreadyClosed.
	Close(ctx context.Context) error
}

func NewRunner

func NewRunner(opts ...runnerOpt) Runner

NewRunner creates a new runner with the given options. The default worker size is 10 and the default buffer size is 100.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳