bbq

package module
v0.0.0-...-8d61b66 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2019 License: MIT Imports: 2 Imported by: 1

README

bbq - a basic batch queue for Go

GoDoc Build Status codecov

bbq allows you to batch messages by time or count, then flush them to a function of your choice. bbq is thread-safe, utilizing Go's native sync.RWMutex. Flushes are synchronous. If the queue is empty, the flush function will not be called.

Usage

package main

import (
    "fmt"
    "github.com/notduncansmith/bbq"
)

func main() {
    flush := func(ms []interface{}) error {
	for _, m := range ms {
		fmt.Println(m.(string))
	}
	return nil
    }
    q := bbq.NewBatchQueue(flush, BatchQueueOptions{time.Millisecond, 2})
    q.Enqueue("🍖")
    time.Sleep(time.Millisecond)
    // Output:
    // 🍖
}

License

Released under The MIT License (see LICENSE.txt).

Copyright 2019 Duncan Smith

Documentation

Overview

Example
flush := func(ms []interface{}) error {
	str := ""
	for _, m := range ms {
		str += m.(string) + " "
	}
	str += fmt.Sprintf("(%d)", len(str))
	fmt.Println(str)
	return nil
}
q := NewBatchQueue(flush, BatchQueueOptions{time.Second, 2})
q.Enqueue("hello")
q.Enqueue("world")
Output:
hello world (12)
Example (Now)
flush := func(ms []interface{}) error {
	for _, m := range ms {
		fmt.Println(m.(string))
	}
	return nil
}
q := NewBatchQueue(flush, BatchQueueOptions{time.Second, 2})
q.Enqueue("🥑")
q.FlushNow()
Output:
🥑
Example (Time)
flush := func(ms []interface{}) error {
	for _, m := range ms {
		fmt.Println(m.(string))
	}
	return nil
}
q := NewBatchQueue(flush, BatchQueueOptions{1 * time.Millisecond, 2})
q.Enqueue("🍪")
time.Sleep(2 * time.Millisecond)
Output:
🍪

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOptions = BatchQueueOptions{time.Second, 1024}

DefaultOptions will flush at least once per second, including whenever the queue reaches 1024 items

Functions

This section is empty.

Types

type BatchQueue

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

BatchQueue is a thread-safe buffer of items that calls a given `flush` function with its contents when reaching a predefined count or time interval, and then empties itself

func NewBatchQueue

func NewBatchQueue(flush Flush, opts BatchQueueOptions) *BatchQueue

NewBatchQueue returns a queue

func (*BatchQueue) Enqueue

func (q *BatchQueue) Enqueue(item interface{}) Callback

Enqueue puts an item on the batch queue

func (*BatchQueue) FlushNow

func (q *BatchQueue) FlushNow()

FlushNow will immediately flush the batch queue

type BatchQueueOptions

type BatchQueueOptions struct {
	// FlushTime controls the minimum time spent between flushes that do not exeed FlushSize. Setting this to 0 will flush on every Enqueue().
	FlushTime time.Duration

	// FlushCount is the number of items that can accumulate within FlushTime before being flushed immediately. Set this to 0 will flush on every Enqueue().
	FlushCount int
}

BatchQueueOptions define the behavior of the batch queue

type Callback

type Callback = chan error

Callback is a channel returned whenever an item is enqueued, and closed (possibly after sending an error) when an item is processed

type Flush

type Flush = func([]interface{}) error

Flush handles the contents of the batch queue and optionally returns an error

Jump to

Keyboard shortcuts

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