scheduler

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

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 4 Imported by: 5

README

scheduler

PkgGoDev Build Status codecov Go Report Card LICENSE

Package scheduler implements a task scheduler.

Get started

Install
go get github.com/hslam/scheduler
Import
import "github.com/hslam/scheduler"
Usage
Example
package main

import (
	"github.com/hslam/scheduler"
	"sync"
)

func main() {
	s := scheduler.New(64, nil)
	wg := &sync.WaitGroup{}
	for i := 0; i < 100000; i++ {
		wg.Add(1)
		task := func() {
			wg.Done()
		}
		s.Schedule(task)
	}
	wg.Wait()
	s.Close()
}
License

This package is licensed under a MIT license (Copyright (c) 2021 Meng Huang)

Author

scheduler was written by Meng Huang.

Documentation

Overview

Package scheduler implements a task scheduler.

Index

Constants

View Source
const (

	// Unlimited represents the unlimited number of goroutines.
	Unlimited = 0
)

Variables

This section is empty.

Functions

func Schedule

func Schedule(task func())

Schedule schedules the task to an idle worker.

Types

type Options

type Options struct {
	// Threshold option represents the threshold at which batch task is enabled.
	// If the threshold option is greater than 1, tasks will be scheduled in batches.
	Threshold int
	// IdleTime option represents the max idle time of the worker.
	IdleTime time.Duration
	// Interval option represents the check interval of the worker.
	Interval time.Duration
}

Options represents options

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns default options.

type Scheduler

type Scheduler interface {
	// Schedule schedules the task to an idle worker.
	Schedule(task func())
	// NumWorkers returns the number of workers.
	NumWorkers() int
	// Close closes the task scheduler.
	Close()
}

Scheduler represents a task scheduler.

func New

func New(maxWorkers int, opts *Options) Scheduler

New returns a new task scheduler.

Jump to

Keyboard shortcuts

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