Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Created August 27, 2014 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save travisbrown/aaf452b97192c70a40a1 to your computer and use it in GitHub Desktop.
Save travisbrown/aaf452b97192c70a40a1 to your computer and use it in GitHub Desktop.
Simple example contrasting a client with baked-in timeout functionality and a client composed with a timeout filter.
import com.twitter.conversions.time._
import com.twitter.finagle.client._
import com.twitter.finagle.dispatch.SerialClientDispatcher
import com.twitter.finagle.util.DefaultTimer
import com.twitter.util.{Await, Future}
trait ExampleClient {
def client: Service[String, String]
def sa = new java.net.InetSocketAddress("localhost", 8080)
def sr = com.twitter.finagle.stats.NullStatsReceiver
def bridge: Future[Service[String, String]] =
StringClientTransporter(sa, sr) map { transport =>
new SerialClientDispatcher(transport)
}
def run() {
val result = client("hello")
println(Await.result(result))
}
}
object KludgyClient extends ExampleClient {
implicit val timer = DefaultTimer.twitter
val client = new Service[String, String] {
def apply(req: String) = bridge flatMap { svc =>
svc(req) ensure svc.close()
} within 500.milliseconds
}
}
object ClientWithTimeoutFilter extends ExampleClient {
import com.twitter.finagle.service.TimeoutFilter
val timeout = new TimeoutFilter[String, String](
timeout = 500.milliseconds,
timer = DefaultTimer.twitter
)
val client = timeout andThen new Service[String, String] {
def apply(req: String) = bridge flatMap { svc =>
svc(req) ensure svc.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment