Skip to content

Commit

Permalink
New time method for TimerPair (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
nb-ceffa authored Mar 14, 2022
1 parent 64c0533 commit 3bed972
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 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 @@ -9,9 +9,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(0)
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 @@ -26,32 +26,33 @@ private class GaugeFactoryImpl[F[_]: Sync](monitor: SMonitor) extends GaugeFacto
override def value: F[Long] = Sync[F].delay(gauge.value)
}

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 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 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 generic[T](name: String, replaceExisting: Boolean = false)(retrieveValue: () => T): Gauge[F, T] = new Gauge[F, T] {
private[this] val gauge = monitor.gauge(name, replaceExisting)(retrieveValue)
override def generic[T](gaugeName: String, replaceExisting: Boolean = false)(retrieveValue: () => T): Gauge[F, T] = new Gauge[F, T] {
private[this] val gauge = monitor.gauge(gaugeName, replaceExisting)(retrieveValue)

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

override def name: String = gauge.name
}

override def genericWithUnsafeRun[T](name: String, replaceExisting: Boolean)(retrieveValue: F[T])(implicit
override def genericWithUnsafeRun[T](gaugeName: String, replaceExisting: Boolean)(retrieveValue: F[T])(implicit
dispatcher: Dispatcher[F]
): Gauge[F, T] = generic(name, replaceExisting)(() => dispatcher.unsafeRunSync(retrieveValue))
): Gauge[F, T] = generic(gaugeName, replaceExisting)(() => dispatcher.unsafeRunSync(retrieveValue))
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avast.metrics.scalaeffectapi.impl

import cats.effect.{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, Resource.ExitCase.Succeeded) => 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 3bed972

Please sign in to comment.