Skip to content

Instantly share code, notes, and snippets.

View shankarshastri's full-sized avatar
💻
Think -> Code -> Solve

ShankarShastri shankarshastri

💻
Think -> Code -> Solve
View GitHub Profile
@viktorklang
viktorklang / Actor.java
Last active February 13, 2023 12:13
Minimalist Java Actors
/*
Copyright 2012-2021 Viktor Klang
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
@viktorklang
viktorklang / InterruptibleCancellableFuture.scala
Last active June 1, 2020 13:45
Interruptible-Cancellable-scala.concurrent.Future
/*
Copyright 2018 Viktor Klang
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
@viktorklang
viktorklang / Future-retry.scala
Last active July 23, 2023 23:48
Asynchronous retry for Future in Scala
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.pattern.after
import akka.actor.Scheduler
/**
* Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
* in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
* the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.
@eulerfx
eulerfx / CSvsES.md
Last active May 21, 2023 08:44
Command Sourcing vs Event Sourcing

Both command sourcing (CS) and event sourcing (ES) rely on determinism for correctness.

The correctness condition for ES is the determinism (purity) of the function State -> Event -> State. Given that this function is deterministic, aka it always maps the same inputs to the same ouputs, we can rely on it to reconstitute state at any point in time. Determinism is typically achieved by ensuring that the Event has all required information to make the state transition, ie no side effects. The Event then is a sort of "closure" of all pertinent information about the event.

The correctness condition for CS is the determinism of the function State -> Command -> Event. Herein lies one of the distinctions between command sourcing and event sourcing - a program can control its output, but it not its input. Since one can't control the input, aka command, one can't in general, enrich it with all required information to make the above function deterministic. A consequence of this is that you can't simply replay a

@Geal
Geal / future.c
Last active January 22, 2023 10:07
small future and promise library in C with pthreads
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include "future.h"
@rklaehn
rklaehn / Client example
Last active June 8, 2020 12:38
akka http file server
package akkahttptest
import akka.http.Http
import akka.stream.ActorFlowMaterializer
import akka.actor.ActorSystem
import akka.stream.scaladsl.{Sink, Source}
import akka.http.model._
object TestClient extends App {
@platy
platy / ScheduledExecutor.scala
Created January 25, 2015 01:11
Scala wrapper around java's ScheduledExecutorService
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy
import java.util.concurrent._
import scala.concurrent.{ Promise, Future }
import scala.concurrent.duration.FiniteDuration
import scala.language.implicitConversions
import scala.util.Try
object ScheduledExecutor {
private val defaultHandler: RejectedExecutionHandler = new AbortPolicy
@thomasst
thomasst / migrate-redis.py
Created May 14, 2015 18:26
Migrate Redis data on Amazon ElastiCache
"""
Copies all keys from the source Redis host to the destination Redis host.
Useful to migrate Redis instances where commands like SLAVEOF and MIGRATE are
restricted (e.g. on Amazon ElastiCache).
The script scans through the keyspace of the given database number and uses
a pipeline of DUMP and RESTORE commands to migrate the keys.
Requires Redis 2.8.0 or higher.
@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 {
@swsnr
swsnr / ShapelessCaseClassAttributes.scala
Created April 6, 2016 13:40
Get a list of all case class attributes with shapeless
import shapeless._
import shapeless.poly._
import shapeless.record._
import shapeless.ops.record._
import shapeless.ops.hlist.{Mapper,ToTraversable}
import shapeless.tag._
final case class Message(id: Int, title: String, body: String)
trait ToAttributes[T] {