Skip to content

Instantly share code, notes, and snippets.

View joost-de-vries's full-sized avatar

Joost de Vries joost-de-vries

View GitHub Profile
@joost-de-vries
joost-de-vries / map-concurrently.ts
Last active August 21, 2023 13:07
Batched concurrent mapping & stream handling in Typescript
/*
This builds on Promise.allSettled.
Adding:
- max parallelism. Handling a nr of calls concurrently, than the next, than the next
- make clear which input arguments gave which results and which failures
- support for infinite streams. Using async iterators / asyc generators
*/
export interface MapConcurrentOpts<A> {
args: A[]
@joost-de-vries
joost-de-vries / prometheus-recording-rules.md
Last active August 10, 2022 11:19
Trying out replacing a yaml DSL with Deno and Typescript

yaml, yaml everywhere!

For our services we make sure of course to record the right metrics in Prometheus. Which means creating recording rules for

  • 7 services,
  • two environments,
  • from both a latency - and throughput perspective,
  • on service internals like: outward http, circuitbreakers, persistence (mongo, hikari connection pooling), logging, exceptions, and more
  • different percentiles and
  • intervals and windows

All possible combinations make for a total of 4600 lines of yaml per environment.

@joost-de-vries
joost-de-vries / akka-and-kotlin-coroutines.md
Last active August 18, 2024 20:24
Akka and kotlin coroutines

Akka and Kotlin coroutines: ♡

I've experimented with Kotlin and coroutines in programming Akka. And I must say, I really like the combination so far.
But before I go into it some brief preliminaries for those who don't know Akka and actors.

Actors and Akka

Actors are a programming model that fits cloud native architectures particularly well. Being highly available and scaling horizontally. All while embracing the realities of multiple servers collaborating, server instances coming and going and the network having hickups.

On the JVM Akka is the prominent actor framework. It's been around for a while now and as a result it's highly reliable, well thought out and offers a wide programming eco system. My own interest in Akka is because of its suitability for software systems that can only be built with business events as a key construct and thinking model. And then of course materialized views, CQRS and near real-time data streams play a big role in constructing those systems.

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class Interview {
val input = "aaaabbbcca"
val expectedOutput = listOf('a' to 4, 'b' to 3, 'c' to 2, 'a' to 1)
@Test
fun `interview - fold`() {
@joost-de-vries
joost-de-vries / FunctionalProgramming.java
Last active December 18, 2020 17:32
"Functional programming"
class FunctionalProgramming {
private static <T> CheckoutModel<Optional<T>> oldHandleObservable(Observable<T> observable, RegisterFailed attempt) {
final Set<ChkNotificationMessage> errorMessages = new HashSet<>();
Optional<T> optional = observable
.doOnError(throwable -> errorMessages.addAll(mapChkAdapterExceptions(throwable, attempt)))
.onErrorResumeNext(Observable.empty())
.map(Optional::of)
.defaultIfEmpty(Optional.empty())
.toBlocking()
@joost-de-vries
joost-de-vries / recordSuspend.kt
Last active October 4, 2024 06:35
micrometer record Kotlin coroutine suspend functions
class Service(val microMeter: MicroMeterRegistry, val otherService: OtherService){
suspend fun service(id: Long){
recordAsync("theService") { otherService.getTheThing(id) }
}
private suspend fun <A> recordAsync(name: String, block: suspend () -> A): A = metricsRegistry.timer(name)
.recordSuspend(block)
}
suspend fun <A> Timer.recordSuspend(block: suspend () -> A): A =

Keybase proof

I hereby claim:

  • I am joost-de-vries on github.
  • I am joost_de_vries (https://keybase.io/joost_de_vries) on keybase.
  • I have a public key whose fingerprint is C350 26F3 D85E 08D8 C4A6 CC8A 0436 E7B7 BDD5 B446

To claim this, I am signing this object:

@joost-de-vries
joost-de-vries / observable.debug.ts
Created September 19, 2018 16:44
Debug function for RxJs 6 observable. Using `pipe`
import { MonoTypeOperatorFunction, Observable, Operator, Subscriber, TeardownLogic } from 'rxjs';
/**
* To debug RxJs 6. Provides 'debug' operator for Observable.
*
* example:
* import { debug } from './debug.observable'
*
* myObservable.pipe(
* .debug('from my observable')
import java.io.{BufferedWriter, File, FileOutputStream, OutputStreamWriter}
import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.util.{HashMap => JHMap, Map => JMap}
import akka.stream.alpakka.dynamodb.scaladsl.DynamoImplicits._
import akka.actor.ActorSystem
import akka.stream.alpakka.dynamodb.scaladsl.DynamoClient
import akka.stream.alpakka.dynamodb.scaladsl.DynamoImplicits._
import com.amazonaws.services.dynamodbv2.model.{AttributeValue, PutItemRequest}
import core.dynamodb.DynamodbAkka._
object Authentication {
def createHeader(userId: String, password: String): (String, String) = {
val concatenated = new String(Base64.encodeBase64(s"$userId:$password".getBytes("UTF-8")))
(AUTHORIZATION, s"Basic $concatenated")
}
def decodeBasicAuth(authHeader: String): Option[(String, String)] = {
val encoded = authHeader.replaceFirst("Basic ", "")
val decoded = new String(Base64.decodeBase64(encoded.getBytes("UTF-8")))
decoded.split(":").toList match {