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

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

The newest version!
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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy