Skip to content

Instantly share code, notes, and snippets.

@hamnis
Created April 15, 2015 10:11
Show Gist options
  • Save hamnis/0a2a03c159f21dfc658b to your computer and use it in GitHub Desktop.
Save hamnis/0a2a03c159f21dfc658b to your computer and use it in GitHub Desktop.
import com.netflix.hystrix.{HystrixCommandGroupKey, HystrixCommand}
import rx.Subscriber
import scala.concurrent.{Future, Promise}
package object hystrix {
implicit class HystrixCommandToFuture[A](val cmd: HystrixCommand[A]) extends AnyVal {
def futureNoRepeat: Future[A] = {
val p = Promise[A]()
cmd.observe().subscribe(new Subscriber[A]() {
override def onError(e: Throwable): Unit = p.failure(e)
override def onCompleted(): Unit = ()
override def onNext(t: A): Unit = p.success(t)
})
p.future
}
}
implicit class StringHystrixOps(val s: String) extends AnyVal {
def asKey = HystrixCommandGroupKey.Factory.asKey(s)
}
implicit class ByNameOps[B](f: => B) {
def command(key: String = "byName"): HystrixCommand[B] = new HystrixCommand[B](key.asKey) {
override def run(): B = f
}
def commandWithFallback(key: String = "byNameFallback")(f2: => B) = new HystrixCommand[B](key.asKey) {
override def run(): B = f
override def getFallback: B = f2
}
}
implicit class Function1Ops[A,B](val f: A => B) extends AnyVal {
def command(a: A, key: String = "function1"): HystrixCommand[B] = new HystrixCommand[B](key.asKey) {
override def run(): B = f(a)
}
def commandWithFallback(a: A, key: String = "function1Fallback")(f2: => B) = new HystrixCommand[B](key.asKey) {
override def run(): B = f(a)
override def getFallback: B = f2
}
def commandWithFallback1(a: A, key: String = "function1Fallback")(f2: A => B) = new HystrixCommand[B](key.asKey) {
override def run(): B = f(a)
override def getFallback: B = f2(a)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment