1

I am trying to request a persistent HTTP Connection using Scala Spray-client library such that the client has to establish the connection once and then it can make multiple calls on the same connection.

I am not able to find any information for this on the Spray documentation page

emotionull
  • 595
  • 2
  • 8
  • 24

1 Answers1

1

I think by default spray client will reuse connections where possible.

By default, connections that are unused for 60 seconds will be terminated by the client.

From the reference config of spray:

# The time after which an idle connection will be automatically closed.
# Set to `infinite` to completely disable idle timeouts.
spray.client.idle-timeout = infinite

If we disable idle timeouts, our connections will stay active forever unless the remote server is set to kill idle connections after some timeout (it probably is, but spray client should make sure we can always get a new connection, obeying the limit bellow).

We can limit the size of the Host Connector pool to 1 to get the desired behaviour (one connection per host):

# The maximum number of parallel connections that an `HttpHostConnector`
# is allowed to establish to a host. Must be greater than zero.
spray.can.host-connector.max-connections = 1

This will mean that if we send a second request before our first request has received a response the second will not be sent until the first is completed. To send multiple requests on a single connection without waiting for the first to complete we can enable pipelining.

# If this setting is enabled, the `HttpHostConnector` pipelines requests
# across connections, otherwise only one single request can be "open"
# on a particular HTTP connection.
spray.can.host-connector.pipelining = off
rahilb
  • 726
  • 3
  • 15