goodmetrics.pipeline.Batcher.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of goodmetrics-kotlin Show documentation
Show all versions of goodmetrics-kotlin Show documentation
A metrics recording library that is good
The newest version!
package goodmetrics.pipeline
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.transform
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeSource
class Batcher(
private val upstream: MetricsPipeline,
private val batchSize: Int = 1000,
private val batchAge: Duration = 10.seconds,
private val timeSource: TimeSource = TimeSource.Monotonic,
) : MetricsPipeline> {
override fun consume(): Flow> {
var currentBatch = ArrayList(batchSize)
var currentBatchDeadline = timeSource.markNow() + batchAge
return upstream.consume()
.transform { item ->
currentBatch.add(item)
if (batchSize <= currentBatch.size || currentBatchDeadline.hasPassedNow()) {
emit(currentBatch)
currentBatchDeadline = timeSource.markNow() + batchAge
currentBatch = ArrayList(batchSize)
}
}
}
}