tempo

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

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

Go to latest
Published: May 6, 2018 License: MIT Imports: 1 Imported by: 0

README

tempo Go Report Card Build Status GoDoc Reference

A dispatched batch queue to process items at time intervals or when a batching limit is met.

Features

  • Non-blocking enqueue
    Queue up incoming items without blocking processing.

  • Dispatching by periodic time intervals
    Set a time interval and get batched items after time expires.

  • Dispatching as soon as a batch limit is met
    If a batch is filled before the time interval is up, dispatching is handled immediately.

  • Plain old Go channels
    Implementation relies heavily on channels and is free of mutexes and other bookkeeping techniques.

3s demo

Install

$ dep ensure -add github.com/ef2k/tempo

Documentation

https://godoc.org/github.com/ef2k/tempo

Sample Usage

Dispatch a batch at 10 second intervals or as soon as a batching limit of 50 items is met. See examples/ for working code.

// initialize
d := tempo.NewDispatcher(&tempo.Config{
  Interval:      time.Duration(10) * time.Second,
  MaxBatchItems: 50,
})
defer d.Stop()
go d.Start()

// produce some messages
go func() {
  for i:= 0; i < 100; i++ {
    m := fmt.Sprintf("message #%d", i)
    d.Q<-m
  }
}()

// consume the batch
for {
  select {
    case batch := <-d.Batch:
      for _, b := range batch {
        s := b.(string)
        // do whatever.
        log.Print(s)
      }
  }
}

Contribute

Improvements, fixes, and feedback are welcome.

MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Interval      time.Duration
	MaxBatchItems int
}

Config configure time interval and set a batch limit.

type Dispatcher

type Dispatcher struct {
	Q               chan item
	Batch           chan []item
	Interval        time.Duration
	MaxBatchItems   int
	DispatchedCount int
	// contains filtered or unexported fields
}

Dispatcher coordinates dispatching of queue items by time intervals or immediately after the batching limit is met.

func NewDispatcher

func NewDispatcher(c *Config) *Dispatcher

NewDispatcher returns an initialized instance of Dispatcher.

func (*Dispatcher) Start

func (d *Dispatcher) Start()

Start begins item dispatching.

func (*Dispatcher) Stop

func (d *Dispatcher) Stop()

Stop stops the internal dispatch scheduler.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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