diff --git a/core/src/main/scala/com/avast/clients/rabbitmq/rabbitmq.scala b/core/src/main/scala/com/avast/clients/rabbitmq/rabbitmq.scala index 3ee31cce..93b2f0d6 100644 --- a/core/src/main/scala/com/avast/clients/rabbitmq/rabbitmq.scala +++ b/core/src/main/scala/com/avast/clients/rabbitmq/rabbitmq.scala @@ -3,6 +3,7 @@ package com.avast.clients import java.util.concurrent.Executors import cats.arrow.FunctionK +import cats.effect.{Effect, IO} import cats.~> import com.avast.bytes.Bytes import com.avast.clients.rabbitmq.api._ @@ -12,6 +13,7 @@ import mainecoon.FunctorK import monix.eval.Task import monix.execution.Scheduler +import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.language.{higherKinds, implicitConversions} @@ -186,4 +188,13 @@ package object rabbitmq { } } + implicit class FAutoCloseableOps[F[_]](val fac: FAutoCloseable[F]) extends AnyVal { + def toAutoCloseable(timeout: Duration = 1.minute)(implicit F: Effect[F]): AutoCloseable = () => { + IO.async[Unit] { cb => + F.runAsync(fac.close())(cb2 => IO(cb(cb2))) + } + .unsafeRunTimed(timeout) + } + } + }