how to set idle timeout per connection for ws http client

850 views
Skip to first unread message

Marc van Kempen

unread,
Sep 2, 2014, 7:17:34 AM9/2/14
to play-fr...@googlegroups.com
Hi,

I'm opening an http connection to an eventstream that remains open and sends event data from time to time. I'm using Play Framework 2.2.2 and the Async WS http client that comes bundled with Play.

This connection needs to remain open, but times out after 2 minutes of idleness (default timeout for idle connections). I would like to set this per request in the same vein as ws.url().withRequestTimeout().

I have found how to set the idle timeout globally (via ws.timeout.idle in application.conf), however I would like to have the default behaviour (120s) for all other connections I'm making. So, is there a way to do this per connection?

NB I seem to observe that using -1 for the timeout makes it never time out, is this expected behaviour?

Cheers,
Marc.

Will Sargent

unread,
Sep 3, 2014, 1:20:05 PM9/3/14
to play-fr...@googlegroups.com
In 2.2.x, the WS client is global, so changing the configuration for an instance is tough.  The newClient method is package private to play:


I think you should be able to do instance configuration by calling:

val asyncHttpClient = WS.client
val originalConfig = asyncHttpClient.getConfig
val newConfig = new AsyncHttpClientConfig(originalConfig)
// add settings to newConfig
val newClient = new AsyncHttpClient(newConfig)

But there isn't a way to wrap the new client instance in back in a WSClient, because 2.2.x didn't have WSClient.  In 2.3.x, you can create different WS client instances and provide different configuration.

Will Sargent
Consultant, Professional Services
Typesafe, the company behind Play Framework, Akka and Scala


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jsw...@ebay.com

unread,
Aug 29, 2016, 4:08:11 PM8/29/16
to play-framework
Resurrecting this thread for Scala 2.5.x, with interest in how to either set per-request config (beyond withRequestTimeout()) or how to appropriately create multiple instances of WSClient with proper lifecycle management. 
I'm specifically interested in controlling connect timeout, read/idle timeout, and retry count for individual instances. Also, does retry count apply to POSTs?

jeff

Will Sargent

unread,
Aug 29, 2016, 6:32:28 PM8/29/16
to play-fr...@googlegroups.com
There's an example of multiple WS clients with lifecycle management at


--
Will Sargent
Engineer, Lightbend, Inc.


To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/840b9d5a-22aa-4fc0-8ad5-bf2f797838a4%40googlegroups.com.

jsw...@ebay.com

unread,
Aug 29, 2016, 11:06:26 PM8/29/16
to play-framework
Thanks. Where should I go to find information on how retry works? I am guessing this is done at the AsyncHttpClient layer, so possibly out of scope for Play. I'm specifically looking to know things such as what conditions do and don't trigger a retry, e.g. based on connect vs read timeouts, HTTP status codes, GET vs POST, etc. 

jeff
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages