rbatching

package module
v0.0.0-...-97f5ebb Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2018 License: MIT Imports: 3 Imported by: 0

README

Godoc

rbatching

One pain point of doing batching in local memory is when the program crash all the batch will be gone. Rbatching provide persistent batching mechanism backed by redis, the persistence is as far as redis definition of persistence. The main idea is putting the queue to a redis list, pop and push it to other list before usage then remove when the usage is finished. All the redis operation is atomic.

This package is not thread safe, create different rbatching instance for each go routine

Usage

    var redisPool = &redis.Pool{
		MaxActive: 5,
		MaxIdle:   5,
		Wait:      true,
		Dial: func() (redis.Conn, error) {
			return redis.Dial("tcp", "localhost:6380")
		},
    }
    // Create new batcher
    // the uniqueness of the id is important to avoid racing condition
	batch := rbatching.NewRBatcher("batch1", 2, redisPool)

    // Enqueue data
    batch.Enqueue("1")
	batch.Enqueue("2")
	batch.Enqueue("3")
    batch.Enqueue("4")
    
    // get the batch batch
    val, err := batch.GetBatch()
    // Close the batch session when the operation is complete
    // the data will be available on the next GetBatch() call
    // when the app crash before CloseBatch get called
    defer batch.CloseBatch()

    if err != nil {
		log.Println(err)
	}
	for _, val := range val {
		string := string(val.([]byte))
		log.Println(string)
    }
    
    // output:
    // 2
    // 1
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisBatcher

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

RedisBatcher provide persistence batching mechanism backed by redis, the persistence is as far as redis definition of persistance

func NewRBatcher

func NewRBatcher(uniqueID string, maxBatch int, redispool *redis.Pool) (batcher *RedisBatcher)

NewRBatcher create redis batcher instance the uniqueness of the id is important to avoid racing condition with other batcher

func (*RedisBatcher) CloseBatch

func (r *RedisBatcher) CloseBatch()

CloseBatch close a batch session

func (*RedisBatcher) Enqueue

func (r *RedisBatcher) Enqueue(element interface{}) (err error)

Enqueue add element to the queue

func (*RedisBatcher) GetBatch

func (r *RedisBatcher) GetBatch() (elements []interface{}, err error)

GetBatch returns elements from the queue as many as maxBatch. This will open a batch session which need to be closed before getting another batch. When the app crash before calling CloseBatch, the elements will be included on the next GetBatch call

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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