Throttling in Akka and Spray

When you want to limit the amount of messages an actor gets, you can use the throttler in akka-contrib. This will let you limit the max transactions per second(tps). It will queue up the surplus.

Here I’ll describe another way. I’ll reject all the surplus messages. This has the advantage that the requester knows it’s sending too much and can act on that.

Both methods have their advantages. And both have limits, since they still require resources to queue or reject the messages.

In Akka we can create an Actor that sends messages through to the target actor, or rejects them when it exceeds the specified tps.

This will return an Accepted or ExceededMaxTps message to the sender.

In Spray we can make a directive that returns with http code 400 when you exceed the maximum.

This can then easily be used in our route.

And to see it in action, I quickly ran curl two times.

The whole code can be found here.



Iterating over a Map in Scala

Iterating over a map is slightly more complex than over other collections, because a Map is the combination of 2 collections. The keys and the values.

We make a Map using tuples. -> is just syntactic sugar for making a tuple.

We get these tuples when we iterate over a Map.


To get the key and the value we could use _1 and _2

But this looks confusing. It’s better to unpack it using case.

We can use map to change the keys of a Map.

If we just want to change the values, it’s better to use mapValues.

And of course our favorite method, flatMap, also works on Maps.

Chaining Options

When we have multiple Options and only want to do something when they’re all set.
In this example we have a property file with multiple configurations for one thing. A host and a port, we only want to use them if they’re both set.

We can chain them using map and flatMap.

Then the following statements are all true.

We can even use a for loop to make it more readable.

JSON parsing in Scala

We can use the Spray JSON parser for uses other than a REST API.

We add spray-json to our dependencies.
Our build.gradle:

We add the imports in JsonApp.scala:

We define our domain class. Then have an implicit variable in scope to define how it should be formatted:

Now we can parse JSON strings to vegetables:

Or output vegetables as JSON:


The whole App:

spray-json documentation:

Load testing our robots API with Gatling

In a previous blog post we made an API with spray.

Now we’re going to load test it.
For this, we will use

In a scala class we can write exactly what and how we want to run the test.
In this test, we will do a post to our API and create a new robot called C3PO.
We will do this 1000 times per second and keep doing this for 10 seconds.
For a total of 10000 C3POs!


If you want to use maven, there is a nice Gatling plugin.
But of course we’d rather use gradle. This works fine too, since we can just run Gatling as a java program.

We can run this test with gradle gatling to get our results.

This is already a pretty neat report. Opening the html file will be even prettier.

Seems like our API can handle 1000 requests/second at least fine. Going higher we’ll have to take some other things into consideration.

  • The amount of connection we can open at once.
  • We’re running both the API and gatling on the same machine now.
  • If we run it on another machine, the network may become the bottleneck
  • Gatling may be the bottleneck.

You can find the whole source here.

Options with flatMap

Suppose I have a List of things on which I want to do something that may fail.
In this example I have a List of Strings that I want to turn into a List of Integers.

The problem is that some of the strings can’t be parsed. And I don’t know which, until I try to parse them. So it’s impossible to filter them out beforehand.
flatMap comes to the rescue!

Because we’re flatMapping over a List, it’s normal to return a List.
If we look at the documentation we can return a lot more.

So we can write the code in a clearer way with Option.

Building a REST client with Spray

In a previous blog I wrote how to make an API. See here.

Now we’ll make a client to use that API. This can be done with spray-client.

First we add dependencies for spray-client and spray-json:

This is what the main class looks like:

You can get the whole code from github.

Scala Snippet: How to filter a list in Scala

In Scala, filtering and processing collections is easy and elegant. There are many filtermethods available, but the most used will probably the basic filter method.

Here’s a code example of some filtering on my (ex)camera collection.
The filter method will not only work on Lists, but on any Scala collection.

Running this example will give you the following result:

original article

Time libraries in Scala

Scala has no default way to deal with dates and times. We have a few options.

java.util.Date and java.util.Calendar
These come included with java, so they may do if you don’t need to do much and don’t want to add any dependencies. But they’re horrible and it’s best not to use them.

The de facto standard date and time library for Java.

A thin scala layer around Joda-Time. This adds some implicit conversions to make it easier to use, like the + and < operators.

Java 8
If you use java 8 it’s best to use the improved java.time.

Scala Snippet: multiline statements in the Scala REPL console

One of the coolest things a standard Scala install will give you, is the Scala interpreter.
Technically speaking, this is not an interpreter. In the background, each statement is quickly compiled into bytecode and executed on the jvm.
Therefore, most people refer to it as the Scala REPL: Read-Evaluate-Print-Loop.

You can access it by starting a command shell on your system and typing in ‘scala‘.
Do make sure your either run it from the place where scala is installed or have scala on your environment PATH.

By using the repl, you can quickly experiment and test out different statements. Once you press ENTER it will evaluate the statement and display the result.

Frequently you want to execute multi-line statements and luckily the repl has a solution for that.

Simply type in :paste and the repl will accept multiline statements.
To exit this mode and evaluate your code, simply type CTRL+D.


original article