Skip to content

Instantly share code, notes, and snippets.

Avatar

Daniel Spiewak djspiewak

View GitHub Profile
View microservices-bench-draft.md

Microservices Bench

This is intended to be a realistic benchmark for a typical production microservice. Each module builds to a Docker container which is intended to be deployed in an autoscaling cluster topology (usually Kubernetes) with mocked upstreams. All measurements are intended to be end-to-end using a scaled test harness like Gatling. In a meaningful sense, this is intended to be a more representative alternative to measurement frameworks such as TechEmpower.

Each module contains a fully independent, idiomatic, and independently-tuned implementation of the same service within a different ecosystem. Pull requests welcome! I'm not personally equally familiar with all of the different frameworks and languages represented within this repository, so if you see something that could be more optimal and/or idiomatic, please feel free to make the change! With that said, the goal is for all implementations to be roughly on the same level in terms

View raceSuccessAll.scala
def raceSuccessAll[F[_], G[_], E, A](
fas: G[F[A]])(
implicit F: GenConcurrent[F, E],
G: Traverse[G])
: F[Either[Chain[E], A]] = {
val permits = fas.size.toInt
F uncancelable { poll =>
for {
View raceAll.scala
def raceAll[F[_], E, A](fs: List[F[A]])(implicit F: GenConcurrent[F, E]): F[A] =
F.deferred[Outcome[F, E, A]] flatMap { d =>
F uncancelable { poll =>
val fibersF = fs traverse { fa =>
fa.guaranteeCase(d.complete(_).void).start
}
fibersF flatMap { fibers =>
poll(d.get.guarantee(fibers.parTraverse_(_.cancel)).flatMap(_.embedNever))
}
View integrated-runtime-strawman.md

Integrated Runtime Strawman

We need to add an IORuntimeConfig parameter for a PollingSystem:

abstract class PollingSystem {
  protected[unsafe] def init(): PollingState
  protected[unsafe] def poll(state: PollingState, timeoutNanos: Long): Boolean
  protected[unsafe] def unpark(thread: Thread): Unit
  protected[unsafe] def close(state: PollingState): Unit
View cps-scala.md

What follows are some of my (very) rough thoughts on what we can and should do with respect to CPS transformation in Scala at the language level. I'll try to start with some motivation behind my thinking, as well as some rambling observations on the nature of the problem space, but don't expect too much coherence here. :-)

The Problem

Async programming is hard.

Okay let's actually be more specific than that. High-performance I/O is hard. Signal multiplexing is a powerful technique for achieving high(er) performance I/O, particularly network I/O, but the tradeoff is that, in order to utilize it, the user-space programming model must allow for suspension and resumption of sequential continuations (often called "fibers" or "coroutines"). Achieving this type of programming model without significant tradeoffs in usability is what is exceptionally hard.

If that wasn't bad enough though, these problems are inextricably conflated with another set of problem spaces which are, themselves, very difficult. In

View dottydoc.txt
[error] java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
[error] at xsbt.DottydocRunner.run(DottydocRunner.java:65)
[error] at xsbt.ScaladocInterface.run(ScaladocInterface.java:11)
[error] at jdk.internal.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
[error] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[error] at sbt.internal.inc.AnalyzingCompiler.invoke(AnalyzingCompiler.scala:329)
[error] at sbt.internal.inc.AnalyzingCompiler.doc(AnalyzingCompiler.scala:175)
[error] at sbt.internal.inc.AnalyzingCompiler.doc(AnalyzingCompiler.scala:133)
[error] at sbt.Doc$.$anonfun$scaladoc$1(Doc.scala:52)
View shadowing.scala
/*
* Copyright 2020-2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
View workarounds.scala
// sbt-git workarounds (TODO upstream into sbt-tl)
inThisBuild {
git.uncommittedSignifier := Some("-SNAPSHOT")
git.formattedShaVersion := {
val Description = """^.*-(\d+)-[a-zA-Z0-9]+$""".r
val suffix = git.makeUncommittedSignifierSuffix(git.gitUncommittedChanges.value, git.uncommittedSignifier.value)
val description = Try("git describe --tags --match v*".!!.trim).toOption
View UnsafeUnbounded.scala
// ported with love from http://psy-lob-saw.blogspot.com/2014/04/notes-on-concurrent-ring-buffer-queue.html
private[effect] final class UnsafeBounded[A](bound: Int) {
private[this] val buffer = new Array[AnyRef](bound)
private[this] val sequenceBuffer = new AtomicLongArray(bound)
private[this] val head = new AtomicLong(0)
private[this] val tail = new AtomicLong(0)
0.until(bound).foreach(i => sequenceBuffer.set(i, i.toLong))
View Channel.scala
final class Channel[F[_], A] private (q: Queue[F, A], closed: Ref[F, Boolean])(implicit F: Monad[F]) {
// doesn't interrupt taking in progress
def close: F[Unit] = closed.set(true)
def isClosed: F[Boolean] = closed.get
def send(a: A): F[Option[Unit]] =
closed.get.ifM(q.offer(a).map(Some(_)), F.pure(None))