Created
August 27, 2014 15:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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