• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    Scala
  • License
    Other
  • Created about 11 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example spray application that uses the actor per request model

Spray Actor Per Request

This project provides an example spray application that uses the Actor per request model.

Why would you want to spin up an Actor for each HTTP request?

  • Easily manage a tree of request scoped Actors in the application core
  • The per request actor can clean them up in the event of a timeouts and failures
  • Leverage the actor supervision hierarchy to propogate failures up to the RequestContext, so you can return useful error responses
  • Promote Tell, Don't Ask
  • Using request scoped Actors in the application core can make it easier to use tell (!) over ask (?)

Resources:

  • Scala Exchange Presentation (video)
  • Mathias describes the actor per request approach against others. (mailing list)

Example App

Overview

This example application provides an API to get a list of pets with their owners. There are already two services that provide a list of pets and a list of animals and the responsibility of this application is to simply aggregate these two together.

Our application is made up of three modules:

  • Application Core - The core module contains the business logic for our application. In this example this is how we aggregate pets with their owners.
  • Routing - The routing module contains our spray routing which describes our RESTful endpoints. It also contains our PerRequest actor which bridges the gap between the routing and the core modules and contains the piece of code this example project aims to demonstrate.
  • Clients - Our code to consume two existing services that provide us with a list of pets and a list of owners. These services could be databases, RESTful APIs, etc. It doesn't really matter for the purposes of this example.

Ideally modules these would be in separate sub-projects to prevent unnecessary compile time dependencies, however for simplicity purposes they are just kept in separate packages in this example.

Running

sbt run

Successful request

If we request the pet Lassie:

GET http://localhost:38080/pets?names=Lassie

We get a successful response:

{
  "pets": [
    {
      "name": "Lassie",
      "owner": {
        "name": "Jeff Morrow"
      }
    }
  ]
}

In this scenario, any request scoped actors in the application core are stopped by the PerRequest actor.

Request Timeouts

Tortoises are slow. If we request a tortoise the PetClient will not reply to our application core quick enough. The timeout of 2 seconds in our PerRequest actor will happen first.

GET http://localhost:38080/pets?names=Tortoise

{
  "message": "Request timeout"
}

In this scenario, any request scoped actors in the application core are stopped by the PerRequest actor.

Validation

You shouldn't keep a Lion as a pet. Quite frankly they are too dangerous.

GET http://localhost:38080/pets?names=Lion

{
  "message": "Lions are too dangerous!"
}

In this scenario, our application core returns a generic Validation message to our PerRequest actor to complete. Any request scoped actors in the application core are stopped by the PerRequest actor.

Failures

What about unexpected failures? There is a "bug" in our application core that throws a PetOverflowException if we request too many pets:

GET http://localhost:38080/pets?names=Lassie,Tweety,Tom

{
  "message": "PetOverflowException: OMG. Pets. Everywhere."
}

Any failures that not handled by the application core can be escalated up to the supervision strategy in our PerRequest actor. The PerRequest actor is too generic to recover from any business logic failures, so it will simply handle all failures by completing the request with an error response. Any request scoped actors in the application core are stopped by the PerRequest actor.

More Repositories

1

scala-uri

Simple scala library for building and parsing URIs
Scala
261
star
2

pre-canned

Mocking HTTP services on spray-can for integration testing
Scala
32
star
3

salad-metrics-core

spray-metrics, spray-routing: Integrating Coda Hale Metrics
Scala
13
star
4

s3-spray

Support for calling the S3 Rest API via spray-client
Scala
9
star
5

NAP-policy

Policy / pragma for Perl code written at NAP
Perl
8
star
6

presentations-polymer-in-production

Slides from the NAP tech "Polymer in production" talk
HTML
6
star
7

nap-components

A repository for all NAP web components
HTML
5
star
8

scala-i18n

Scala
4
star
9

dot-net-magazine-google-maps-tutorial

Use JavaScript, CSS and the Google Maps API to build a custom-themed, realtime Flickr visualisation like NET-A-PORTER LIVE
JavaScript
4
star
10

dynamo-mapper

An experiment in making the Java SDK DynamoDB Client more Scala-esque
Scala
4
star
11

scala-beginner-workshop

JavaScript
3
star
12

jenkinsworld-2017-sample-app

CSS
2
star
13

gatling-on-aws

Shell
2
star
14

outnet-2015-heroku

Website for Outnet 2015 style guide
2
star
15

sandboxed-module-blanket

Node.js module to support blanket.js code coverage when injecting dependencies via sandboxed-module
JavaScript
2
star
16

jenkinsworld-2017-local-pipeline

Docker-Compose template for virtualized Jenkins pipeline
Shell
1
star
17

lsug-mar2015

Slide deck for the Net-A-Porter presentation at the LSUG in March 2015
JavaScript
1
star
18

akka-testeventlister-demo

Scala
1
star
19

mrporterfont

simplified Sketch to font
CSS
1
star
20

NAP-Messaging

STOMP-based messaging framework
Perl
1
star
21

scala-uri-benchmarks

JavaScript
1
star
22

preston

Tools to manage style guides
HTML
1
star
23

product-image-perf

Test performance of product assets across the group
JavaScript
1
star
24

outnet-2015

OUTNET.com 2015 style guide
CSS
1
star