org.allenai.common.webapp

SprayClientHelpers

Related Doc: package webapp

object SprayClientHelpers

Utility methods for sending HTTP requests through spray without being tripped-up by the nastiness of spray's API / underlying implementation. The two methods in this object are intended for use together.

Example: Making quick GET requests with little internal buffering. format: OFF

import SprayClientHelpers._

// Define parameters for sending requests to the foo service.
// This can be done once per web client instance as part of initialization, if all of the
// fields are constant.
val quickConnectorSetup = getConnectionSetup(
  host = "foo.com",
  port = 1234,
  connectionTimeout = 500.millis,
  requestTimeout = 1.second,
  maxConnections = 100
)

// Define a function that requests a Foo object from the remote service and parses the
// response JSON.
def getFoo: Future[Foo] = sendRequest(Get("/foo"), quickConnectorSetup) { response =>
  response ~> unmarshal[Foo]
}

// Completes in ~ 1 second, either with a Seq of 100 Foos or spray's RequestTimeoutException.
Future.sequence(Seq.fill(100) { getFoo })

format: ON

Example: Making slow, CPU-intensive POST requests with internal rate-limiting. format: OFF

import SprayClientHelpers._

val slowConnectorSetup = getConnectorSetup(
  host = "bar.com",
  port = 9876,
  connectionTimeout = 1.second,
  requestTimeout = 10.seconds,
  // Limit the number of in-flight requests at any one time to 4, to avoid overloading the
  // remote service.
  maxConnections = 4
)

def fooToBar(foo: Foo): Future[Bar] = {
  sendRequest(Post("/bar", foo), slowConnectorSetup) { response =>
    response ~> unmarshal[Bar]
  }
}

// Takes longer than 10 seconds! Only 4 requests will be sent over the wire at a time, and
// the 10-second request timeout doesn't apply until a request gets sent out.
Future.sequence(Seq.fill(16) { fooToBar(someFoo) })

format: ON

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. SprayClientHelpers
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  5. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  10. def getConnectionSetup(host: String, port: Int, connectionTimeout: FiniteDuration, requestTimeout: FiniteDuration, maxConnections: Int, connectorId: Option[String] = None)(implicit system: ActorSystem): HostConnectorSetup

    Override spray's default settings for sending requests with the given timeouts.

    Override spray's default settings for sending requests with the given timeouts. Note this automatically sets two different idle timeouts to be 2 * requestTimeout: format: OFF

    1. The time after which an idle HTTP connection will be automatically closed. 2. The time after which idle HttpHostConnector actors (internal to spray) without open connections will automatically terminate themselves. format: ON
    host

    the name of the remote host you want to communicate with

    port

    the port the remote host is listening on

    connectionTimeout

    the timeout to use when establishing a remote connection to the remote host

    requestTimeout

    the timeout to use when waiting for a response from the remote host

    maxConnections

    the maximum number of connections to the remote host that will be held open at a time by the returned connector

    connectorId

    an optional unique ID to use as the User-Agent header for requests to this host. If you want to maintain multiple host connector pools to a single remote host (for example, to prevent long-running requests from interfering with quick requests), setting this field to different values for each will prevent spray from sharing a connector between the different types of requests.

  11. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  12. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  16. def sendRequest[T](request: HttpRequest, connectorSetup: HostConnectorSetup)(parseResponse: (HttpResponse) ⇒ T)(implicit actorSystem: ActorSystem): Future[T]

    Send an HTTP request through spray using a dedicated HostConnectorSetup, and process the response using the given function.

    Send an HTTP request through spray using a dedicated HostConnectorSetup, and process the response using the given function. This gives you much more control over how and when your request is sent over the wire / when it times out than does use of sendReceive. Caveat: this function is designed to practically prevent you from needing to worry about catching AskTimeoutExceptions when using spray, but it's not actually possible to guarantee. If you somehow send so many requests that one is internally buffered for more than Int.MaxValue.millis, you'll see an AskTimeoutException thrown.

    request

    the HttpRequest object to send over the wire

    connectorSetup

    the HostConnectorSetup object defining connection and timeout information for your request. See getConnectionSetup for a method of building these connectors.

    parseResponse

    a function from HttpResponse to a generic value T you want to extract from your request's response

  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  18. def toString(): String

    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped