As far as I know, when you use the automagic Thrift.newIface[Iface]
method to create your service, you can't close it, because the only thing that your code knows about the resulting value is that it conforms to Iface
. If you need to close it, you can instantiate your client in two steps, creating the Thrift service in one and adapting it to your interface in the other.
Here's how it looks if you're using Scrooge to generate your Thrift interface:
val serviceFactory: ServiceFactory[ThriftClientRequest,Array[Byte]] =
Thrift.newClient(s"$host:$port")
val client: MyPingService[Future] =
new MyPingService.FinagledClient(serviceFactory.toService)
doStuff(client).ensure(serviceFactory.close())
I tried this in the repl, and it worked for me. Here's a lightly-edited transcript:
scala> val serviceFactory = Thrift.newClient(...)
serviceFactory: ServiceFactory[ThriftClientRequest,Array[Byte]] = <function1>
scala> val tweetService = new TweetService.FinagledClient(serviceFactory.toService)
tweetService: TweetService.FinagledClient = TweetService$FinagledClient@20ef6b76
scala> Await.result(tweetService.getTweets(GetTweetsRequest(Seq(20))))
res7: Seq[GetTweetResult] = ... "just setting up my twttr" ...
scala> serviceFactory.close
res8: Future[Unit] = ConstFuture(Return(()))
scala> Await.result(tweetService.getTweets(GetTweetsRequest(Seq(20))))
com.twitter.finagle.ServiceClosedException
This is not too bad, but I hope there's a better way that I don't know yet.