goodmetrics.pipeline.SynchronizingBuffer.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
package goodmetrics.pipeline
import goodmetrics.Metrics
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.consumeAsFlow
/**
* Threadsafe pipeline starter
*/
class SynchronizingBuffer(
maxQueuedItems: Int = 1024
) : MetricsPipeline, MetricsSink {
private val metricsQueue: Channel = Channel(
capacity = maxQueuedItems,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
onUndeliveredElement = this::failedToDeliver
)
override fun consume(): Flow {
return metricsQueue.consumeAsFlow()
}
override fun emit(metrics: Metrics) {
metricsQueue.trySend(metrics)
}
override fun close() {
metricsQueue.close()
}
private fun failedToDeliver(_metrics: Metrics) {
// TODO: record metrics
}
}