Skip to content

Instantly share code, notes, and snippets.

View nraychaudhuri's full-sized avatar

Nilanjan Raychaudhuri nraychaudhuri

View GitHub Profile
sealed trait UrinalStatus { def value: String }
final case object Available extends UrinalStatus {
val value = "Avialable"
}
final case object Occupied extends UrinalStatus {
val value = "Occupied"
}

Keybase proof

I hereby claim:

  • I am nraychaudhuri on github.
  • I am nilanjan (https://keybase.io/nilanjan) on keybase.
  • I have a public key whose fingerprint is E5FB 9991 27E0 074C D2B7 7EF0 8CC4 C3BC B9D5 87B6

To claim this, I am signing this object:

private void busy(FiniteDuration duration) {
pi(duration.toMillis() * 800);
}
private BigDecimal pi(long m) {
int n = 0;
BigDecimal acc = new BigDecimal(0.0);
while(n < m) {
acc = acc.add(gregoryLeibnitz(n));
n += 1;
@nraychaudhuri
nraychaudhuri / CoffeeHouseTest
Created April 2, 2015 07:24
Testing Akka actors using JavaTestKit
import akka.actor.ActorIdentity;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.event.Logging;
import akka.testkit.JavaTestKit;
import akka.testkit.TestProbe;
import com.typesafe.training.coffeehouse.CoffeeHouse;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@nraychaudhuri
nraychaudhuri / TypedAkkaActorRef.scala
Last active August 29, 2015 14:05
Typed Akka actor ref
//PLEASE NOTE: Typing actor refs are really hard problem(take a look at the discussions in Akka mailing list regarding this subject). This is no way a complete solution. This will only work if you know all the possible
//messages an actor can handle (that means no become/unbecome business).
package experiment
import akka.actor.{Props, ActorSystem, Actor, ActorRef}
case class TypedActorRef[A](actorRef: ActorRef) extends AnyVal {
def ![B](msg: B)(implicit ev: A <:< B, sender: ActorRef = Actor.noSender) = actorRef ! msg
}
@nraychaudhuri
nraychaudhuri / backpressure
Created August 6, 2014 18:08
back pressure
I try to put things into the larger perspective here, so prepare, long mail.
== Defining backpressure
First of all, we need a working definition of "backpressure". I put it in quotes since I will attack things from a very general perspective which might not exactly match what others think as backpressure.
Sometimes it is easier to characterize the correct behavior of a system/protocol by defining what we consider incorrect behavior rather than trying to limit ourselves when approaching from correctness properties. In the case of backpressure there are two undesireable kinds of behavior:
- If the buffers in the system might growth without bounds then the backpressure protocol is not safe
- If the protocol can become stuck, although the sender has messages to deliver and the receiver is also available to process them, then the backpressure protocol is not safe

The default SupervisorStrategy in Akka

At times it's hard to remember all the details of restarting a faulty actor. So lets take a look at a simple example:

/user/grand-parent/parent/child

Here we have a Child actor with one Parent and one GrandParent actor.

@nraychaudhuri
nraychaudhuri / slick_crud.scala
Created May 27, 2014 16:12
Generic CRUD for Slick1
package solution
import scala.slick.driver.ExtendedProfile
object DI {
object models {
sealed trait Model { def id: Option[Long] }
case class ModelA(a: String, id: Option[Long] = None) extends Model
case class ModelB(b: String, id: Option[Long] = None) extends Model
@nraychaudhuri
nraychaudhuri / Play2SlickBridge.scala
Created May 14, 2014 18:51
Play2-Auth and Slick together
//Trait that bridges between slick and Play2-auth. Just mixin with controllers
trait Auth2SlickBridge {
self: StackableController =>
def AuthAction[A](p: BodyParser[A], params: (RequestAttributeKey[_], Any)*)(f: RequestWithAttributes[A] => Action[A]): Action[A] = {
AsyncStack(p, params:_*) { implicit rs =>
f(rs).apply(rs)
}
}