Documentation
¶
Overview ¶
Package taskrunner provides a Chromium-inspired task scheduling architecture for Go.
This library implements a threading model where developers post tasks to virtual threads (TaskRunners) rather than managing goroutines directly. The core design is inspired by Chromium's Threading and Tasks system.
Quick Start ¶
Initialize the global thread pool at application startup:
taskrunner.InitGlobalThreadPool(4) // 4 workers defer taskrunner.ShutdownGlobalThreadPool()
Create a SequencedTaskRunner for sequential task execution:
runner := taskrunner.CreateTaskRunner(taskrunner.DefaultTaskTraits())
runner.PostTask(func(ctx context.Context) {
// Your code here - guaranteed sequential execution
})
Key Concepts ¶
TaskRunner: Interface for posting tasks. Tasks posted to a SequencedTaskRunner execute sequentially, eliminating the need for locks on resources owned by that runner.
TaskTraits: Describes task attributes including priority (BestEffort, UserVisible, UserBlocking). Priority determines when the sequence gets scheduled, not the order within a sequence.
GoroutineThreadPool: The execution engine managing worker goroutines that pull and execute tasks from the scheduler.
Thread Safety ¶
SequencedTaskRunner provides strict FIFO execution guarantees with runtime assertions. Tasks within a sequence never run concurrently, allowing lock-free programming for resources owned by that sequence.
Example ¶
import (
"context"
taskrunner "github.com/Swind/go-task-runner"
)
func main() {
taskrunner.InitGlobalThreadPool(4)
defer taskrunner.ShutdownGlobalThreadPool()
runner := taskrunner.CreateTaskRunner(taskrunner.DefaultTaskTraits())
// Tasks execute sequentially
runner.PostTask(func(ctx context.Context) {
println("Task 1")
})
runner.PostTask(func(ctx context.Context) {
println("Task 2")
})
// Delayed task
runner.PostDelayedTask(func(ctx context.Context) {
println("Task 3 - delayed")
}, 1*time.Second)
}
For more details, see https://github.com/Swind/go-task-runner
Index ¶
- Variables
- func InitGlobalThreadPool(workers int)
- func ShutdownGlobalThreadPool()
- type GoroutineThreadPool
- func (tg *GoroutineThreadPool) ActiveTaskCount() int
- func (tg *GoroutineThreadPool) DelayedTaskCount() int
- func (tg *GoroutineThreadPool) ID() string
- func (tg *GoroutineThreadPool) IsRunning() bool
- func (tg *GoroutineThreadPool) Join()
- func (tg *GoroutineThreadPool) PostDelayedInternal(task core.Task, delay time.Duration, traits core.TaskTraits, ...)
- func (tg *GoroutineThreadPool) PostInternal(task core.Task, traits core.TaskTraits)
- func (tg *GoroutineThreadPool) QueuedTaskCount() int
- func (tg *GoroutineThreadPool) Start(ctx context.Context)
- func (tg *GoroutineThreadPool) Stop()
- func (tg *GoroutineThreadPool) StopGraceful(timeout time.Duration) error
- func (tg *GoroutineThreadPool) WorkerCount() int
- type RepeatingTaskHandle
- type ReplyWithResult
- type SequencedTaskRunner
- type SingleThreadTaskRunner
- type Task
- type TaskPriority
- type TaskRunner
- type TaskTraits
- type TaskWithResult
- type ThreadPool
Constants ¶
This section is empty.
Variables ¶
var ( DefaultTaskTraits = core.DefaultTaskTraits TraitsUserBlocking = core.TraitsUserBlocking TraitsBestEffort = core.TraitsBestEffort TraitsUserVisible = core.TraitsUserVisible )
Convenience functions for creating TaskTraits
var GetCurrentTaskRunner = core.GetCurrentTaskRunner
GetCurrentTaskRunner retrieves the current TaskRunner from context
Functions ¶
func InitGlobalThreadPool ¶
func InitGlobalThreadPool(workers int)
InitGlobalThreadPool initializes the global thread pool with specified number of workers. It starts the pool immediately.
func ShutdownGlobalThreadPool ¶
func ShutdownGlobalThreadPool()
ShutdownGlobalThreadPool stops the global thread pool.
Types ¶
type GoroutineThreadPool ¶
type GoroutineThreadPool struct {
// contains filtered or unexported fields
}
GoroutineThreadPool manages a set of worker goroutines Responsible for pulling tasks from WorkSource and executing them
func GetGlobalThreadPool ¶
func GetGlobalThreadPool() *GoroutineThreadPool
GetGlobalThreadPool returns the global thread pool instance. It panics if InitGlobalThreadPool has not been called.
func NewGoroutineThreadPool ¶
func NewGoroutineThreadPool(id string, workers int) *GoroutineThreadPool
NewGoroutineThreadPool creates a new GoroutineThreadPool
func NewPriorityGoroutineThreadPool ¶
func NewPriorityGoroutineThreadPool(id string, workers int) *GoroutineThreadPool
func (*GoroutineThreadPool) ActiveTaskCount ¶
func (tg *GoroutineThreadPool) ActiveTaskCount() int
func (*GoroutineThreadPool) DelayedTaskCount ¶
func (tg *GoroutineThreadPool) DelayedTaskCount() int
func (*GoroutineThreadPool) ID ¶
func (tg *GoroutineThreadPool) ID() string
ID returns the ID of the thread pool
func (*GoroutineThreadPool) IsRunning ¶
func (tg *GoroutineThreadPool) IsRunning() bool
IsRunning returns whether the thread pool is running
func (*GoroutineThreadPool) Join ¶
func (tg *GoroutineThreadPool) Join()
Join waits for all worker goroutines to finish
func (*GoroutineThreadPool) PostDelayedInternal ¶
func (tg *GoroutineThreadPool) PostDelayedInternal(task core.Task, delay time.Duration, traits core.TaskTraits, target core.TaskRunner)
func (*GoroutineThreadPool) PostInternal ¶
func (tg *GoroutineThreadPool) PostInternal(task core.Task, traits core.TaskTraits)
func (*GoroutineThreadPool) QueuedTaskCount ¶
func (tg *GoroutineThreadPool) QueuedTaskCount() int
func (*GoroutineThreadPool) Start ¶
func (tg *GoroutineThreadPool) Start(ctx context.Context)
Start starts all worker goroutines
func (*GoroutineThreadPool) StopGraceful ¶ added in v0.2.0
func (tg *GoroutineThreadPool) StopGraceful(timeout time.Duration) error
StopGraceful stops the thread pool gracefully, waiting for queued tasks to complete Returns error if timeout is exceeded before tasks complete
func (*GoroutineThreadPool) WorkerCount ¶
func (tg *GoroutineThreadPool) WorkerCount() int
WorkerCount returns the number of workers
type RepeatingTaskHandle ¶
type RepeatingTaskHandle = core.RepeatingTaskHandle
RepeatingTaskHandle controls the lifecycle of a repeating task
type ReplyWithResult ¶
type ReplyWithResult[T any] = core.ReplyWithResult[T]
type SequencedTaskRunner ¶
type SequencedTaskRunner = core.SequencedTaskRunner
SequencedTaskRunner ensures sequential execution of tasks
func CreateTaskRunner ¶
func CreateTaskRunner(traits TaskTraits) *SequencedTaskRunner
CreateTaskRunner creates a new SequencedTaskRunner using the global thread pool. This is the recommended way to get a new TaskRunner.
func NewSequencedTaskRunner ¶
func NewSequencedTaskRunner(pool ThreadPool) *SequencedTaskRunner
NewSequencedTaskRunner creates a new SequencedTaskRunner with the given thread pool. This is re-exported for advanced users who want to create runners with custom pools.
type SingleThreadTaskRunner ¶
type SingleThreadTaskRunner = core.SingleThreadTaskRunner
SingleThreadTaskRunner ensures all tasks execute on the same dedicated goroutine
func NewSingleThreadTaskRunner ¶
func NewSingleThreadTaskRunner() *SingleThreadTaskRunner
NewSingleThreadTaskRunner creates a new SingleThreadTaskRunner with a dedicated goroutine. Use this for blocking IO operations, CGO calls with thread-local storage, or UI thread simulation.
type TaskPriority ¶
type TaskPriority = core.TaskPriority
TaskPriority defines the priority levels for tasks
const ( TaskPriorityBestEffort TaskPriority = core.TaskPriorityBestEffort TaskPriorityUserVisible TaskPriority = core.TaskPriorityUserVisible TaskPriorityUserBlocking TaskPriority = core.TaskPriorityUserBlocking )
Priority constants
type TaskTraits ¶
type TaskTraits = core.TaskTraits
TaskTraits defines task attributes (priority, blocking behavior, etc.)
type TaskWithResult ¶
type TaskWithResult[T any] = core.TaskWithResult[T]
TaskWithResult and ReplyWithResult for generic PostTaskAndReply pattern
type ThreadPool ¶
type ThreadPool = core.ThreadPool
ThreadPool is re-exported for type compatibility
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic_sequence
command
|
|
|
delayed_task
command
|
|
|
mixed_priority
command
|
|
|
repeating_task
command
|
|
|
shutdown
command
|
|
|
single_thread
command
|
|
|
task_and_reply
command
|