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

sttp.client4.internal.ws.FutureSimpleQueue.scala Maven / Gradle / Ivy

There is a newer version: 4.0.0-M19
Show newest version
package sttp.client4.internal.ws

import java.util.concurrent.{ArrayBlockingQueue, BlockingQueue, LinkedBlockingQueue}

import sttp.ws.WebSocketBufferFull

import scala.concurrent.{blocking, ExecutionContext, Future}

class FutureSimpleQueue[T](capacity: Option[Int])(implicit ec: ExecutionContext) extends SimpleQueue[Future, T] {

  private val queue: BlockingQueue[T] = capacity match {
    case Some(value) => new ArrayBlockingQueue[T](value)
    case None        => new LinkedBlockingQueue[T]()
  }

  /** Eagerly adds the given item to the queue.
    */
  override def offer(t: T): Unit =
    if (!queue.offer(t)) {
      throw WebSocketBufferFull(capacity.getOrElse(Int.MaxValue))
    }

  /** Takes an element from the queue or suspends, until one is available. May be eager or lazy, depending on `F`.
    */
  override def poll: Future[T] = Future(blocking(queue.take()))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy