1
0
Fork 0

Handle queue timers better

No need to the queuedStmts == nil check now.
master
Philip O'Toole 1 year ago
parent b425d0f8e1
commit b923c73838

@ -174,17 +174,11 @@ func (q *Queue) run() {
defer close(q.closed) defer close(q.closed)
queuedStmts := make([]*queuedStatements, 0) queuedStmts := make([]*queuedStatements, 0)
timer := time.NewTimer(q.timeout) // Create an initial timer, in the stopped state.
timer.Stop() timer := time.NewTimer(0)
<-timer.C
writeFn := func() { writeFn := func() {
timer.Stop()
if queuedStmts == nil {
// Batch size was met, but timer expired before it could be
// stopped, so this function was called again. Possibly.
return
}
// mergeQueued returns a new object, ownership will pass // mergeQueued returns a new object, ownership will pass
// implicitly to the other side of sendCh. // implicitly to the other side of sendCh.
req := mergeQueued(queuedStmts) req := mergeQueued(queuedStmts)
@ -198,9 +192,14 @@ func (q *Queue) run() {
case s := <-q.batchCh: case s := <-q.batchCh:
queuedStmts = append(queuedStmts, s) queuedStmts = append(queuedStmts, s)
if len(queuedStmts) == 1 { if len(queuedStmts) == 1 {
// First item in queue, start the timer so that if
// we don't get in a batch, we'll still write.
timer.Reset(q.timeout) timer.Reset(q.timeout)
} }
if len(queuedStmts) == q.batchSize { if len(queuedStmts) == q.batchSize {
if !timer.Stop() {
<-timer.C
}
writeFn() writeFn()
} }
case <-timer.C: case <-timer.C:

Loading…
Cancel
Save