New instantiates a new queue. flushFunc is is used for periodic index flusing, and closeFunc will be used when closing. flushFunc should add items with values, and delete items without values. Nil items are possible. The goal is to batch index updates on a single thread.
( logger *zap.SugaredLogger, flushFunc func([]*Item) error, closeFunc func() error, opts Options, )
| 44 | // |
| 45 | // The goal is to batch index updates on a single thread. |
| 46 | func New( |
| 47 | logger *zap.SugaredLogger, |
| 48 | flushFunc func([]*Item) error, |
| 49 | closeFunc func() error, |
| 50 | opts Options, |
| 51 | ) *Queue { |
| 52 | if flushFunc == nil { |
| 53 | flushFunc = func([]*Item) error { return nil } |
| 54 | } |
| 55 | if closeFunc == nil { |
| 56 | closeFunc = func() error { return nil } |
| 57 | } |
| 58 | if opts.Rate == 0 { |
| 59 | opts.Rate = 5 * time.Second |
| 60 | } |
| 61 | |
| 62 | return &Queue{ |
| 63 | l: logger, |
| 64 | |
| 65 | closeFunc: closeFunc, |
| 66 | flushFunc: flushFunc, |
| 67 | |
| 68 | pendingC: make(chan *Item, opts.BatchSize), |
| 69 | pending: 0, |
| 70 | pendingItems: make([]*Item, opts.BatchSize), |
| 71 | rate: opts.Rate, |
| 72 | batchSize: opts.BatchSize, |
| 73 | |
| 74 | stopC: make(chan bool, 1), |
| 75 | stopped: true, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Queue indicates that a new item is pending insertion. A nil value indicates |
| 80 | // the item should be deleted. |
no outgoing calls