Skip to content

Instantly share code, notes, and snippets.

View johanandren's full-sized avatar
👻
hakking

Johan Andrén johanandren

👻
hakking
View GitHub Profile
@johanandren
johanandren / RateLimit.scala
Created July 14, 2017 10:05
Sample for a custom rate limiting directive for Akka HTTP
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package http
import java.util.concurrent.atomic.AtomicInteger
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpResponse, StatusCodes, Uri}
@johanandren
johanandren / SimplePartitionSample.scala
Created July 26, 2016 08:25
Sample of using partition to split up incoming elements over multiple outgoing streams
import akka.actor.ActorSystem
import akka.stream._
import akka.stream.scaladsl._
import scala.io.StdIn
import scala.util.Random
object SimplePartitionSample extends App {
implicit val system = ActorSystem()
import akka.actor.ActorSystem
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.Tcp
import akka.util.ByteString
object TcpServerOneResponseThenComplete2 {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
import akka.stream.scaladsl.Framing
import akka.actor.ActorSystem
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.Tcp
import akka.util.ByteString
object TcpServerOneResponseThenComplete {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
import akka.stream.scaladsl.Framing
@johanandren
johanandren / RequestId.scala
Created September 6, 2016 07:17
Example showing how to ensure a unique id for each request, and how to log that from the Akka HTTP server DSL
import java.util.UUID
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.model.headers.{ModeledCustomHeader, ModeledCustomHeaderCompanion}
import akka.stream.ActorMaterializer
import akka.http.scaladsl.server._
import scala.io.StdIn
@johanandren
johanandren / numbers-as-a-service.scala
Last active April 17, 2020 12:17
Sample streaming response with akka http
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshalling.{Marshaller, ToResponseMarshaller}
import akka.http.scaladsl.model.HttpEntity.ChunkStreamPart
import akka.http.scaladsl.model.{HttpEntity, HttpResponse, MediaTypes}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
@johanandren
johanandren / stuff.java
Created January 2, 2020 15:34
Java mock actor child
class MyActor extends AbstractActor {
public Receive createReceive() {
return receiveBuilder()
.match(Request.class, this::onRequest)
.build();
}
private void onRequest(Request request) {
ActorRef child = createChildForRequest(request);
import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props}
import akka.cluster.{Cluster, MemberStatus}
import akka.routing.FromConfig
import com.typesafe.config.ConfigFactory
object ClusterRouting extends App {
class EchoActor extends Actor with ActorLogging {
log.info("Started")
def receive = {
@johanandren
johanandren / ClusteredRoundRobinPool.scala
Created September 9, 2015 14:30
Simple sample with clustered round robin pool
import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.cluster.Cluster
import akka.routing.FromConfig
import com.typesafe.config.ConfigFactory
import scala.concurrent.{Await, Future}
import scala.io.StdIn
object ClusteredRoundRobinPool extends App {
import akka.actor.ActorSystem
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import scala.concurrent.Await
// important - it needs to be this Seq and not the default one
import scala.collection.immutable.Seq
// this is just for the await, so not really needed
import scala.concurrent.duration._