concept response in category scala

appears as: responses, response, responses, response, The response, A response, The response
Reactive Design Patterns

This is an excerpt from Manning's book Reactive Design Patterns.

When interfacing with a system that does not provide bounded response latency, it is necessary to retrofit the ability to signal momentary overload situations. This can to some degree be achieved by employing circuit breakers, but in addition you must consider what the response to overload should be. The flow-control patterns described in chapter 16 help in this case as well.

Include a return address in the message to receive a response.

This is the most basic interaction pattern we know; it is the foundational building block for all natural communication, deeply ingrained in human training. A parent will say something to their infant child, who will initially gesture or make some sounds in response; later, the child will articulate words and sentences. Request–response is the way you learn how to speak, and it underlies all sophisticated forms of communication that you develop later. Although the response can be nonverbal (in particular, facial expressions, but also the deliberate absence thereof), in most cases you require a response before successfully concluding a conversation. The response can be a piece of information that you asked for or an acknowledgment of receipt for a command that was given.

In all these cases, there is one commonality that you need to make explicit when translating this basic means of communication into a programming pattern: the process begins with two participants A and B, where A knows how to address B; when receiving requests, B will need to learn or deduce how to address A in order to send back a response. Captured in a diagram, this looks like figure 15.1.

In this scheme, the router must be able to forward all the streams coming from the file servers back to all the clients, meaning its network interface’s bandwidth will place an upper limit on how many bytes per second can be streamed by the entire system. Network links are becoming faster and faster, but if this streaming service is intended to serve customers on the internet, then it will reach its limit fairly soon. Therefore, you must think a bit more about the meaning of request and response in this scenario: the client sends a request, and the logical destination is the file server, not the router. The response is a video stream whose source is the file server and whose destination is the client. Although you need the router as an intermediary for the request—balancing the load across a pool of replicated file servers hosting the particular video file that is requested—you do not need to involve it in handling the response. The resulting message-flow diagram is shown in figure 15.3.

Figure 15.3. Flow of messages when applying forward flow: although the request needs to pass through the router for load balancing, the (large) response does not need to take the same route. It can travel back directly from file server to client.

Here, the requests flow toward their destination via the shortest possible path, and the responses also do not waste any time on their way back. Because the file servers communicate directly with the clients, you are free to scale up the available computing and network resources without limit.

In the Actor model, the only defined means of communication is by way of sending single messages: no connections, responses, or similar are defined. Using the Akka implementation of this model, you might solve the task posed initially in the following fashion.

Listing 15.3. Untyped Akka Actors modeling request–response
object RequestResponseActors { case class Request(msg: String) case class Response(msg: String) class Responder extends Actor { def receive = { case Request(msg) => println(s"got request: $msg") sender() ! Response("got it!") } } class Requester(responder: ActorRef) extends Actor { responder ! Request("hello") def receive = { case Response(msg) => println(s"got response: $msg") context.system.terminate() } } def main(args: Array[String]): Unit = { val sys = ActorSystem("ReqRes") val responder = sys.actorOf(Props[Responder], "responder") val requester = sys.actorOf(Props(new Requester(responder)), "requester") } }

You first define the message types for the request and the response, and you formulate a Responder actor that, given a request, replies with a response, printing a message to the console so you can see that something happens. Then you define a second actor—Requester—that is instantiated with the ActorRef of a responder. This requester actor first sends a request and then reacts to the reception of a response by printing a message to the console and shutting down the entire process. The main program starts up first a responder and then an associated requester. When you run this program, you will see the expected output:

got request: hello
got response: got it!

This formulation of the Request–Response pattern is enabled by an Akka-specific feature: when sending the request message, the requester’s own ActorRef is implicitly picked up by the ! (tell) operator, and the Request message is placed in an Akka-internal envelope that also carries this sender ActorRef. This is how the sender() method can access the return address within the Responder actor to send back the response. The envelopes used behind the scenes are an implementation detail that has been introduced specifically because request–response is such a widely used communication pattern.

Play for Scala: Covers Play 2

This is an excerpt from Manning's book Play for Scala: Covers Play 2.

Validation happens at binding time. This makes validation as easy as calling bindFromRequest and then fold to transform the form into the right kind of response. In Scala, fold is often used as the name of a method that collapses (or folds) multiple possible values into a single value. In this case, we’re attempting to fold either a form with validation errors or one that validates correctly into a response. The fold method takes two parameters, both of which are functions. The first parameter (hasErrors) is called if validation failed; the other (success) if the form validated without errors. This is analogous to Scala’s Either type. This is exactly what our save action does.

But we’re not done here. When we redirect back to the new-product page—due to validation errors—the page will be rendered with an empty form and no indication to the user about what went wrong. One solution would be to render the editProduct template from the hasErrors function. We’d rather not do this because we’d be rendering a page in response to a POST and making things difficult for the users if they try to use the Back button. Remember, Play is about embracing HTTP, not fighting it. What we want to do is redirect the user back to the new-product page and somehow make the form data (including the validation errors) available to the next request. Let’s do that in listing 2.33, which shows an improved version of our save action.

We’ll get back to the interactions with the model and views later in the chapter. For now, let’s focus on the controller. We haven’t yet filled in the body of each action method, which is where we’ll process the request and generate a response to send back to the HTTP client (see figure 4.2).

Figure 10.3. Using the WS library with an iteratee to consume the response