-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement batcher * try background goroutine and channels * Revert "try background goroutine and channels" (worse performance) This reverts commit 2e46a0b. * improve batcher performance * implement batch write strategy * add log message --------- Co-authored-by: Haris Osmanagić <[email protected]>
- Loading branch information
1 parent
10047a2
commit 4374355
Showing
6 changed files
with
463 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright © 2023 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Batcher[T any] struct { | ||
sizeThreshold int | ||
delayThreshold time.Duration | ||
fn BatchFn[T] | ||
results chan BatchResult | ||
|
||
batch []T | ||
flushTimer *time.Timer | ||
m sync.Mutex | ||
} | ||
|
||
type BatchFn[T any] func([]T) error | ||
|
||
type BatchResult struct { | ||
At time.Time | ||
Size int | ||
Err error | ||
} | ||
|
||
type EnqueueStatus int | ||
|
||
const ( | ||
Scheduled EnqueueStatus = iota + 1 | ||
Flushed | ||
) | ||
|
||
func NewBatcher[T any](sizeThreshold int, delayThreshold time.Duration, fn BatchFn[T]) *Batcher[T] { | ||
return &Batcher[T]{ | ||
sizeThreshold: sizeThreshold, | ||
delayThreshold: delayThreshold, | ||
fn: fn, | ||
results: make(chan BatchResult, 1), | ||
} | ||
} | ||
|
||
func (b *Batcher[T]) Results() <-chan BatchResult { | ||
return b.results | ||
} | ||
|
||
func (b *Batcher[T]) Enqueue(item T) EnqueueStatus { | ||
b.m.Lock() | ||
defer b.m.Unlock() | ||
|
||
b.batch = append(b.batch, item) | ||
|
||
if len(b.batch) == b.sizeThreshold { | ||
// trigger flush synchronously | ||
_ = b.flushNow() | ||
return Flushed | ||
} | ||
if b.flushTimer == nil { | ||
b.flushTimer = time.AfterFunc(b.delayThreshold, func() { b.Flush() }) | ||
} | ||
return Scheduled | ||
} | ||
|
||
func (b *Batcher[T]) Flush() bool { | ||
b.m.Lock() | ||
defer b.m.Unlock() | ||
return b.flushNow() | ||
} | ||
|
||
func (b *Batcher[T]) flushNow() bool { | ||
if b.flushTimer != nil { | ||
b.flushTimer.Stop() | ||
b.flushTimer = nil | ||
} | ||
if len(b.batch) == 0 { | ||
// nothing to flush | ||
return false | ||
} | ||
batchCopy := make([]T, len(b.batch)) | ||
copy(batchCopy, b.batch) | ||
|
||
at := time.Now() | ||
err := b.fn(batchCopy) | ||
r := BatchResult{ | ||
At: at, | ||
Size: len(batchCopy), | ||
Err: err, | ||
} | ||
b.results <- r | ||
b.batch = b.batch[:0] | ||
return true | ||
} |
Oops, something went wrong.