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

sttp.tapir.server.netty.internal.FutureUtil.scala Maven / Gradle / Ivy

There is a newer version: 1.11.4
Show newest version
package sttp.tapir.server.netty.internal

import io.netty.channel.{Channel, ChannelFuture}

import scala.concurrent.{CancellationException, Future, Promise}
import scala.util.{Failure, Success}

object FutureUtil {
  def nettyChannelFutureToScala(nettyFuture: ChannelFuture): Future[Channel] = {
    val p = Promise[Channel]()
    nettyFuture.addListener((future: ChannelFuture) =>
      p.complete(
        if (future.isSuccess) Success(future.channel())
        else if (future.isCancelled) Failure(new CancellationException)
        else Failure(future.cause())
      )
    )
    p.future
  }

  def nettyFutureToScala[T](f: io.netty.util.concurrent.Future[T]): Future[T] = {
    val p = Promise[T]()
    f.addListener((future: io.netty.util.concurrent.Future[T]) => {
      if (future.isSuccess) p.complete(Success(future.getNow))
      else if (future.isCancelled) p.complete(Failure(new CancellationException))
      else p.complete(Failure(f.cause()))
    })
    p.future
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy