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
Click to show internal directories.
Click to hide internal directories.