All Downloads are FREE. Search and download functionalities are using the official Maven repository.

goodmetrics.pipeline.Batcher.kt Maven / Gradle / Ivy

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)
                }
            }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy