scheduler

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

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

Go to latest
Published: Apr 17, 2020 License: BSD-3-Clause Imports: 3 Imported by: 3

README

scheduler

scheduler is a simple load-balancer across n-workers. Work-units are values of type jobs.Interface that is a type alias for func().

Installation

go get github.com/caelifer/scheduler

Usage

package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"

	"github.com/caelifer/scheduler"
)

const (
	NWORKERS = 10
	NSAMPS   = 100
)

func main() {
	var wg sync.WaitGroup
	sch := scheduler.New(NWORKERS)
	t0 := time.Now()
	rand.Seed(t0.UnixNano())
	for i := 0; i < NSAMPS; i++ {
		wg.Add(1)
		sch.Schedule(func() {
			defer wg.Done()
			time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
		})
	}
	wg.Wait()
	fmt.Printf("Ran %d samples in %s\n", NSAMPS, time.Since(t0))
}

This code should produce output similar to

$ go run test.go
Ran 100 samples in 546.21049ms

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scheduler

type Scheduler interface {
	Schedule(job.Interface)
	Shutdown()
}

Scheduler is an interface type to provide an abstraction around load-balancing operation scheduling.

func New

func New(nworkers int) Scheduler

New builds a new Scheduler object. It starts its internal scheduling process in the background. This scheduling process makes sure that all available workers are always running in the background waiting for the work units to come in. New takes a single paramter: nworkers - a number of background workers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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