dispatcher

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

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

Go to latest
Published: Feb 8, 2019 License: MIT Imports: 1 Imported by: 0

README

go-dispatcher

Build Status

Introduction

This library is a simple implementation of a worker pool in go, building on the concepts from this article.

Features

Variable concurrency

Instead of spawning an unbounded number of goroutines, the number of concurrent jobs to be processed can be set. This is useful for managing system resources, as well as cases where the job processing involves interacting with another rate sensitive service.

Variable buffer

If the rate at which jobs are processed is slower than the rate at which they are added, those waiting are added into a queue - the size of which can be set as well.

Graceful shutdown

Calling Stop() waits for all added jobs to finish.

Installing

go get github.com/danielpoonwj/go-dispatcher

Usage

package main

import (
	"fmt"

	"github.com/danielpoonwj/go-dispatcher"
)

type Job struct {
	msg string
}

func (j *Job) Process() {
	fmt.Println(j.msg)
}

func NewJob(msg string) *Job {
	return &Job{msg: msg}
}

func main() {
	d := dispatcher.NewDispatcher(1, 3)
	d.Start()

	j1 := NewJob("Job 1")
	d.AddJob(j1)

	j2 := NewJob("Job 2")
	d.AddJob(j2)

	d.Stop()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

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

Dispatcher : Manage dispatching Jobs to Workers

func NewDispatcher

func NewDispatcher(maxWorkers int, queueSize int) *Dispatcher

NewDispatcher : Initialize new Dispatcher

func (*Dispatcher) AddJob

func (d *Dispatcher) AddJob(job Job)

AddJob : Add Job to job queue

func (*Dispatcher) QueuedJobCount

func (d *Dispatcher) QueuedJobCount() int

QueuedJobCount : Get number of jobs in queue. Note: some may already dispatched to the worker pool

func (*Dispatcher) Start

func (d *Dispatcher) Start()

Start : Register and start workers

func (*Dispatcher) Stop

func (d *Dispatcher) Stop()

Stop : Ensure all jobs registered are processed

type Job

type Job interface {
	Process()
}

Job : Common interface that processes raw data

Jump to

Keyboard shortcuts

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