Skip to content

Instantly share code, notes, and snippets.

View kermitas's full-sized avatar

Artur Stanek kermitas

View GitHub Profile
@kermitas
kermitas / PriorityThreadsDispatcher.scala
Last active January 30, 2019 13:47
Akka dispatcher with configurable priority for created threads.
package akka.dispatch
import com.typesafe.config.Config
object PriorityThreadsDispatcher {
/**
* Configuration key under which int value should be placed.
*/
val threadPriorityConfigKey = "thread-priority"
}
@kermitas
kermitas / PriorityThreadsDispatcherPrerequisites.scala
Created July 6, 2015 11:11
Prerequisites for Akka dispatcher.
package akka.dispatch
/**
* Composition over [[DefaultDispatcherPrerequisites]] that replaces thread factory with one that allow to configure thread priority.
*
* @param newThreadPriority priority that will be set to each newly created thread
* should be between Thread.MIN_PRIORITY (which is 1) and Thread.MAX_PRIORITY (which is 10)
*/
class PriorityThreadsDispatcherPrerequisites(prerequisites: DispatcherPrerequisites, newThreadPriority: Int) extends DefaultDispatcherPrerequisites(
eventStream = prerequisites.eventStream,
@kermitas
kermitas / PriorityThreadFactory.scala
Created July 6, 2015 11:19
ThreadFactory with configurable priority.
package akka.dispatch
import java.util.concurrent.ThreadFactory
/**
* Composition over the [[DispatcherPrerequisites.threadFactory]] that set priority for newly created threads.
*
* @param newThreadPriority priority that will be set to each newly created thread
* should be between Thread.MIN_PRIORITY (which is 1) and Thread.MAX_PRIORITY (which is 10)
*/
@kermitas
kermitas / application.conf
Created July 6, 2015 11:23
Configuration for Akka dispatcher that contains low priority threads.
# Low priority threads protects system from CPU starvation.
low-priority-threads-dispatcher {
# For more details please see documentation of this class.
type = akka.dispatch.PriorityThreadsDispatcher
executor = "thread-pool-executor"
# Priority of threads in this thread pool, should be between Thread.MIN_PRIORITY (which is 1) and Thread.MAX_PRIORITY (which is 10).
thread-priority = 1
@kermitas
kermitas / ExampleOfLowPriorityThreadsDispatcherUsage.scala
Created July 6, 2015 11:28
Example of usage low priority threads Akka dispatcher.
val lowPriorityThreadsDispatcherName = "low-priority-threads-dispatcher"
val lowPriorityThreadsExecutionContext = play.libs.Akka.system.dispatchers.lookup(lowPriorityThreadsDispatcherName)
Future {
require(Thread.currentThread.getPriority == Thread.MIN_PRIORITY, s"current thread's priority should be ${Thread.MIN_PRIORITY} but it is ${Thread.currentThread.getPriority}")
1 + 1 // do something that would be executed on low pririty thread
}(lowPriorityThreadsExecutionContext)
@kermitas
kermitas / CallingThreadExecutionContext.scala
Last active November 21, 2017 05:37
Maybe not perfect but simple calling thread ExecutionContext can help if we already have thread (for example: taken from thread pool) and want to execute few operations in a row.
/**
* [[ExecutionContext]] that will execute actions in calling thread (and by that making them blocking).
*/
class CallingThreadExecutionContext extends ExecutionContext {
override def execute(runnable: Runnable): Unit = runnable.run
override def reportFailure(t: Throwable): Unit = throw t
}
@kermitas
kermitas / FutureStackTraceTest.scala
Last active October 21, 2015 20:59
By using future.recover() we can easily have informative stack traces.
package scala.concurrent
import scala.concurrent.duration._
import org.scalatest.FeatureSpec
class FutureStackTraceTest extends FeatureSpec {
ignore("stack trace of Future") {
implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global
@kermitas
kermitas / FutureUtils.scala
Last active October 22, 2015 23:31
Future.sequence() is fine but it will fail if any of passed Futures will fail. Sometimes we would like to have the same functionality but it should not fail - it should just have a collection of completed Futures (completed with Success or Failure).
package scala.concurrent
import scala.util.{ Try, Success }
object FutureUtils {
/**
* @return resulted future will be completed when all passed futures will be completed (either with Success or Future)
*/
def completeAll[T](fs: Future[T]*)(implicit ec: ExecutionContext): Future[Seq[Future[T]]] = Future.successful(fs).flatMap { fs =>
@kermitas
kermitas / Scala45Pl20151024FutureTest.scala
Last active February 16, 2016 16:18
Short lecture about Future(s) at #scala45pl, 2015-10-24, http://www.meetup.com/WarszawScaLa/events/225320171/ .
package scala.concurrent
import duration._
import scala.util.{ Success, Try }
import org.scalatest.FeatureSpec
/**
* #scala45pl http://www.meetup.com/WarszawScaLa/events/225320171/
*
* Also during this presentation:
@kermitas
kermitas / Build.scala
Last active January 27, 2016 00:16
Easy to read and easy to maintain Build.scala (SBT's main file).
object Build extends sbt.Build {
implicit val artifactConfig = ArtifactConfig(
name = "lift-problem",
organization = "org.stanek",
version = "0.1.0-SNAPSHOT"
)
// ==== projects definition