Skip to content

Instantly share code, notes, and snippets.

@iamthiago
Last active May 23, 2017 12:56
Show Gist options
  • Save iamthiago/c7fc390b184ebdfb560e2f5cb01fdcea to your computer and use it in GitHub Desktop.
Save iamthiago/c7fc390b184ebdfb560e2f5cb01fdcea to your computer and use it in GitHub Desktop.
Brief introduction to back-pressure and Akka Streams
//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