worker

package module
v0.0.0-...-ba2f7a6 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 7 Imported by: 0

README

worker

worker is a Go package that implements a task-based worker system inspired by the internal task-based operations of the Chromium browser. This package provides functionality to add tasks to a queue and process them with multiple worker threads.

Features

  • Callback Binding
  • Task Runner

Usage

go get github.com/ISSuh/worker
Callback & Bind
package main

import (
    "context"
    "fmt"

    worker "github.com/ISSuh/worker"
)

func add(a, b int) int {
    return a + b
}

func main() {
    // Bind function and return callback
    callback1, err := worker.Bind[func(a, b int) int](add)
    if err != nil {
        panic(err)
    }

    // Run callback with parameter
    res1 := callback1.Run(1, 2)
    fmt.Printf("callback1 result: %d\n", res1)

    // Bind function with partial parameter and return callback with captured partial parameters
    callback2, err := worker.Bind[func(a int) int](add, 10)
    if err != nil {
        panic(err)
    }

    // Run callback with partial parameter
    res2 := callback2.Run(20)
    fmt.Printf("callback2 result: %d\n", res2)

    // Bind function with all parameter and return callback with captured all parameters
    callback3, err := worker.Bind[func() int](add, 100, 200)
    if err != nil {
        panic(err)
    }

    // Run callback without all parameter
    res3 := callback3.Run()
    fmt.Printf("callback3 result: %d\n", res3)
}
callback1 result: 3
callback2 result: 30
callback3 result: 300
basic Task
package main

import (
    "context"
    "fmt"
    "time"

    worker "github.com/ISSuh/worker"
)

func taskFunc(index int) {
    fmt.Printf("[taskFunc] index : %d\n", index)
}

func main() {
    // Create task runner with number of worker
    runner := worker.NewTaskRunner(5)

    // run task runner
    // task runner will be stopped when cancel context
    c, cancel := context.WithCancel(context.Background())
    go runner.RunLoop(c)

    for i := 0; i < 50; i++ {
        // Bind task function
        // Task function signature is only can use TaskSigniture. it is func() type
        cb, err := worker.Bind[worker.TaskSigniture](taskFunc, i)
        if err != nil {
            panic(err)
        }

        // Create task
        task := worker.NewTask(cb)

        // Post task to task runner
        runner.PostTask(task)
    }

    time.Sleep(3 * time.Second)

    // when cancel context, task runner will be stopped
    cancel()
}
[taskFunc] index : 2
[taskFunc] index : 4
[taskFunc] index : 5
[taskFunc] index : 6
...
delay Task
package main

import (
    "context"
    "fmt"
    "time"

    worker "github.com/ISSuh/worker"
)

func taskFunc(index int, duration time.Duration) {
    fmt.Printf("[taskFunc] index : %d, duration : %d\n", index, duration)
}

func main() {
    // Create task runner with number of worker
    runner := worker.NewTaskRunner(5)

    // run task runner
    // task runner will be stopped when cancel context
    c, cancel := context.WithCancel(context.Background())
    go runner.RunLoop(c)

    for i := 0; i < 50; i += 10 {
        duration := time.Duration(i) * time.Millisecond

        // Bind task function
        // Task function signature is only can use TaskSigniture. it is func() type
        cb, err := worker.Bind[worker.TaskSigniture](taskFunc, i, duration)
        if err != nil {
            panic(err)
        }

        // Create delay task with duration
        delayTask := worker.NewDelayTask(duration, cb)

        // Post task to task runner
        runner.PostTask(delayTask)
    }

    time.Sleep(3 * time.Second)

    // when cancel context, task runner will be stopped
    cancel()
}

[taskFunc] index : 0, duration : 0
[taskFunc] index : 10, duration : 10000000
[taskFunc] index : 20, duration : 20000000
[taskFunc] index : 30, duration : 30000000
[taskFunc] index : 40, duration : 40000000

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind[FuncDefinition any](f interface{}, fixedArgs ...interface{}) (*callback[FuncDefinition], error)

func BindOnce

func BindOnce[FuncDefinition any](f interface{}, fixedArgs ...interface{}) (*onceCallback[FuncDefinition], error)

Types

type Task

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

func NewDelayTask

func NewDelayTask(delay time.Duration, callback *callback[TaskSigniture]) *Task

func NewTask

func NewTask(callback *callback[TaskSigniture]) *Task

func (*Task) Run

func (t *Task) Run()

func (*Task) TimeStamp

func (t *Task) TimeStamp() time.Time

type TaskRunner

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

func NewTaskRunner

func NewTaskRunner(workerNum int) *TaskRunner

func (*TaskRunner) PostTask

func (r *TaskRunner) PostTask(task *Task)

func (*TaskRunner) RunLoop

func (r *TaskRunner) RunLoop(c context.Context)

RunLoop run task runner loop

type TaskSigniture

type TaskSigniture func()

Directories

Path Synopsis
cmd
http command

Jump to

Keyboard shortcuts

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