Last active
May 23, 2017 12:56
-
-
Save iamthiago/c7fc390b184ebdfb560e2f5cb01fdcea to your computer and use it in GitHub Desktop.
Brief introduction to back-pressure and Akka Streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//imports | |
object MyBackPressureTest extends App { | |
implicit val system = ActorSystem() | |
implicit val materializer = ActorMaterializer() | |
val addresses = List(Address...) //a list of some addresses | |
val createRequestFlow = Flow[Address].map { address => | |
val params = Map("state" -> address.state, "city" -> address.city, "street" -> address.street) | |
val uri = Uri("/geocode").withQuery(Uri.Query(params)) | |
HttpRequest(uri = uri) -> address | |
} | |
val httpFlow = Http().cachedHostConnectionPool[Adress](host = "my-api.company.com", port = 80) | |
val responseFlow = Flow[(Try[HttpResponse], Address)].map { tuple => | |
val (responseTry, address) = tuple | |
//here you can deal with the http response | |
} | |
Source(addresses) | |
.via(createRequestFlow) | |
.via(httpFlow) | |
.via(responseFlow) | |
.toMat(Sink.ignore)(Keep.right) | |
.run() | |
} | |
case class Address(state: String, city: String, street: String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment