Skip to content

Instantly share code, notes, and snippets.

@patriknw
patriknw / FlowControlSample.scala
Last active May 10, 2018 18:35
Illustrates Actor message flow control with "work pulling pattern". This code is licensed under the Apache 2 license.
package flowcontrol
import scala.concurrent.duration._
import akka.actor.typed.ActorRef
import akka.actor.typed.ActorSystem
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors
/**
@atamborrino
atamborrino / ExecutionContextMonitor.scala
Last active April 14, 2022 09:10
Monitor Scala's ExecutionContext / Akka Dispatcher lag (number of tasks in waiting queues)
import java.util.concurrent._
import akka.dispatch.{Dispatcher, ExecutorServiceDelegate}
import config.Config
import helpers.ScalaLogger
class ExecutionContextMonitor()(implicit metricsService: MetricsClient, config: Config) {
private val log = ScalaLogger.get(this.getClass)
private val scheduler = Executors.newSingleThreadScheduledExecutor()
import java.util.concurrent.TimeUnit
import monix.execution.cancelables.MultiAssignmentCancelable
import monix.execution.{Cancelable, Scheduler}
import monix.execution.schedulers.{ExecutionModel, LocalBatchingExecutor}
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.FiniteDuration
class AkkaToMonixScheduler(
akkaScheduler: akka.actor.Scheduler,
context: ExecutionContext,
sealed trait JobDescription
sealed trait HasN {
def n: Int
}
case class JobOne(n: Int) extends JobDescription with HasN
case object JobTwo extends JobDescription
case class JobThree(n: Int) extends JobDescription with HasN
@arnolddevos
arnolddevos / DT.scala
Created February 5, 2016 02:01
Dependent Types Example
// Example from Odersky et al http://infoscience.epfl.ch/record/215280/files/paper.pdf
object DT {
trait Key { type Value }
class Setting(val str: String) extends Key
val sort = new Setting("sort") { type Value = String }
val width = new Setting("width") { type Value = Int }
val params = HMap.empty.add(width)(120).add(sort)("time")
@cvogt
cvogt / gist:b86aff8ac51f49a574cc
Last active August 29, 2015 14:11
Immutable cycles (as seen in "Case classes a la carte with shapeless, now!" at scalax2014 by @milessabin)
// immutable cycle
class Node[T]( val value: T, _next: => Node[T] ){
lazy val next = _next
}
val cycle: Node[Int] = new Node( 1, new Node( 2, cycle ) )
// prints List(1, 2, 1, 2, 1, 2, 1, 2, 1, 2)
println(cycle.iterator.take(10).map(_.value).toList)
implicit class NodeIterator[T](node: Node[T]){
case class ReportInvalidJsonActorHolder(ref: ActorRef)
case class ReportUnhandledExceptionActorHolder(ref: ActorRef)
case class HeartbestProcessingActorHolder(ref: ActorRef)
case class DebugEmailActorHolder(ref: ActorRef)
object ReportInvalidJsonActor {
def props(debugEmailActorHolder: DebugEmailActorHolder) =
Props(classOf[ReportInvalidJsonActor], debugEmailActorHolder)
case class InvalidJsonMessage(content : String, exceptionMessage: String)
package scalax.collection
import scala.collection.mutable.ListBuffer
/** FoldTransformers and the views based on them are a Scala
* adaptation, and to some degree an extension, of Rich Hickey's
* transducers for Clojure. They show that the concepts can be
* implemented in a type-safe way, and that the implementation is
* quite beautiful.
*/
object FoldingViews {
@milessabin
milessabin / gist:8833a1dbf7e8245b30f8
Last active April 16, 2018 02:39
Now in shapeless 2.1.0-SNAPSHOT: "shapeless.the" an enhanced alternative to "Predef.implicitly".
// Used as a term `the[T]` yields the unique implicit value of type `T` in the current
// implicit scope, if any. It is a compile time error if there is no such value. Its
// primary advantage over `Predef.implicitly` is that it will preserve any refinement that
// the implicit definition has, resulting in more precisely typed, and hence often more
// useful, values,
scala> trait Foo { type T ; val t: T }
defined trait Foo
scala> implicit val intFoo: Foo { type T = Int } = new Foo { type T = Int ; val t = 23 }
@jboner
jboner / how-akka-maps-to-eai-patterns.txt
Last active October 9, 2022 21:57
How Akka maps to EAI Patterns
# How Akka maps to EAI Patterns
Might be up for debate or just plain wrong. Just some notes I scribbled down some time ago.
-----------------------------------------------------------------------------------------------------------------
EAI PATTERN AKKA PATTERN REFERENCE
-----------------------------------------------------------------------------------------------------------------
Point to Point Channel Regular Actor Communication http://www.eaipatterns.com/PointToPointChannel.html
Event-Driven Consumer Regular Actor Receive http://www.eaipatterns.com/EventDrivenConsumer.html
Message Selector Actor with Stash http://www.eaipatterns.com/MessageSelector.html