Skip to content

Commit

Permalink
New time method for TimerPair
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Kefurt committed Mar 11, 2022
1 parent b0a8f75 commit 6247207
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ trait TimerPair[F[_]] {
def update(duration: Duration): F[Unit]
def updateFailure(duration: Duration): F[Unit]
def time[T](action: F[T]): F[T]
def time[T](action: F[T])(successCheck: T => Boolean): F[T]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import java.util.concurrent.atomic.{AtomicLong, AtomicReference}

private class GaugeFactoryImpl[F[_]: Sync](monitor: SMonitor) extends GaugeFactory[F] {

override def settableLong(name: String, replaceExisting: Boolean = false): SettableGauge[F, Long] = new SettableGauge[F, Long] {
override def settableLong(gaugeName: String, replaceExisting: Boolean = false): SettableGauge[F, Long] = new SettableGauge[F, Long] {
private[this] val valueRef = new AtomicLong(0L)
private[this] val gauge = monitor.gauge(name, replaceExisting)(valueRef.get)
private[this] val gauge = monitor.gauge(gaugeName, replaceExisting)(valueRef.get)

override def set(value: Long): F[Unit] = Sync[F].delay(valueRef.set(value))

Expand All @@ -25,25 +25,26 @@ private class GaugeFactoryImpl[F[_]: Sync](monitor: SMonitor) extends GaugeFacto
override def dec: F[Long] = Sync[F].delay(valueRef.decrementAndGet())
}

override def settableDouble(name: String, replaceExisting: Boolean = false): SettableGauge[F, Double] = new SettableGauge[F, Double] {
private[this] val valueRef = new AtomicReference(0.0)
private[this] val gauge = monitor.gauge(name, replaceExisting)(valueRef.get)
override def settableDouble(gaugeName: String, replaceExisting: Boolean = false): SettableGauge[F, Double] =
new SettableGauge[F, Double] {
private[this] val valueRef = new AtomicReference(0.0)
private[this] val gauge = monitor.gauge(gaugeName, replaceExisting)(valueRef.get)

override def set(value: Double): F[Unit] = Sync[F].delay(valueRef.set(value))
override def set(value: Double): F[Unit] = Sync[F].delay(valueRef.set(value))

override def name: String = gauge.name
override def name: String = gauge.name

override def value: F[Double] = Sync[F].delay(gauge.value)
override def value: F[Double] = Sync[F].delay(gauge.value)

override def update(f: Double => Double): F[Double] = Sync[F].delay(valueRef.updateAndGet(f(_)))
override def update(f: Double => Double): F[Double] = Sync[F].delay(valueRef.updateAndGet(f(_)))

override def inc: F[Double] = Sync[F].delay(valueRef.accumulateAndGet(1, (a, b) => a + b))
override def inc: F[Double] = Sync[F].delay(valueRef.accumulateAndGet(1, (a, b) => a + b))

override def dec: F[Double] = Sync[F].delay(valueRef.accumulateAndGet(1, (a, b) => a - b))
}
override def dec: F[Double] = Sync[F].delay(valueRef.accumulateAndGet(1, (a, b) => a - b))
}

override def generic[T](name: String, replaceExisting: Boolean = false)(gauge: () => T): Gauge[F, T] = new Gauge[F, T] {
private[this] val sGauge = monitor.gauge(name, replaceExisting)(gauge)
override def generic[T](gaugeName: String, replaceExisting: Boolean = false)(gauge: () => T): Gauge[F, T] = new Gauge[F, T] {
private[this] val sGauge = monitor.gauge(gaugeName, replaceExisting)(gauge)

override def value: F[T] = Sync[F].delay(sGauge.value)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avast.metrics.scalaeffectapi.impl

import cats.effect.{ExitCase, Resource, Sync}
import cats.effect.Sync
import cats.syntax.applicativeError._
import cats.syntax.flatMap._
import cats.syntax.functor._

Expand Down Expand Up @@ -35,10 +36,18 @@ private class TimerPairImpl[F[_]: Sync](success: Timer[F], failure: Timer[F]) ex
override def updateFailure(duration: Duration): F[Unit] = Sync[F].delay(failure.update(duration))

override def time[T](action: F[T]): F[T] = {
Resource
.makeCase(start) {
case (ctx, ExitCase.Completed) => ctx.stop.as(())
case (ctx, _) => ctx.stopFailure.as(())
}
}.use(_ => action)
for {
ctx <- start
result <- action.onError { case _ => ctx.stopFailure.void }
_ <- ctx.stop
} yield result
}

override def time[T](action: F[T])(successCheck: T => Boolean): F[T] = {
for {
ctx <- start
result <- action.onError { case _ => ctx.stopFailure.void }
_ <- if (successCheck(result)) ctx.stop else ctx.stopFailure
} yield result
}
}

0 comments on commit 6247207

Please sign in to comment.