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

com.twitter.finagle.kestrel.Server.scala Maven / Gradle / Ivy

package com.twitter.finagle.kestrel

// never name a package "java"
import _root_.java.net.SocketAddress
import _root_.java.util.concurrent.{BlockingDeque, LinkedBlockingDeque}

import com.github.benmanes.caffeine.cache.{Caffeine, CacheLoader}
import com.twitter.finagle.builder.{Server => BuiltServer, ServerBuilder}
import com.twitter.finagle.kestrel.protocol.{Command, Kestrel, Response}
import com.twitter.finagle.{ClientConnection, ServiceFactory}
import com.twitter.io.Buf
import com.twitter.util.{Future, Time}

class Server(address: SocketAddress) {
  private[this] val serviceFactory = new ServiceFactory[Command, Response] {

    private[this] val queues = Caffeine.newBuilder()
      .build(new CacheLoader[Buf, BlockingDeque[Buf]] {
        def load(k: Buf): BlockingDeque[Buf] = new LinkedBlockingDeque[Buf]
      })

    def apply(conn: ClientConnection) = Future.value(new InterpreterService(new Interpreter(queues)))
    def close(deadline: Time) = Future.Done
  }

  private[this] val serverSpec =
    ServerBuilder()
      .name("schmestrel")
      .codec(Kestrel())
      .bindTo(address)

  private[this] var server: Option[BuiltServer] = None

  def start(): BuiltServer = {
    server = Some(serverSpec.build(serviceFactory))
    server.get
  }

  def stop() {
    require(server.isDefined, "Server is not open!")

    server.foreach { server =>
      server.close()
      this.server = None
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy