Skip to content

Instantly share code, notes, and snippets.

View vkostyukov's full-sized avatar

Vladimir Kostyukov vkostyukov

View GitHub Profile
@vkostyukov
vkostyukov / results.txt
Created March 13, 2016 18:31
EvalFuture Bench
[info] Benchmark Mode Cnt Score Error Units
[info] RerunnableBenchmark.sumIntsEF thrpt 40 45.180 ± 2.361 ops/s
[info] RerunnableBenchmark.sumIntsEF:·gc.alloc.rate.norm thrpt 40 45596119.411 ± 32.597 B/op
[info] RerunnableBenchmark.sumIntsF thrpt 40 63.038 ± 3.683 ops/s
[info] RerunnableBenchmark.sumIntsF:·gc.alloc.rate.norm thrpt 40 28796020.355 ± 23.283 B/op
[info] RerunnableBenchmark.sumIntsPF thrpt 40 22.371 ± 2.129 ops/s
[info] RerunnableBenchmark.sumIntsPF:·gc.alloc.rate.norm thrpt 40 52033222.296 ± 2995947.605 B/op
[info] RerunnableBenchmark.sumIntsPR thrpt 40 16.022 ± 1.675 ops/s
[info] RerunnableBenchmark.sumIntsPR:·gc.alloc.rate.norm t
@vkostyukov
vkostyukov / te.scala
Last active March 11, 2016 21:46
TimeEndpoint
import io.finch._
import com.twitter.finagle.stats._
// See https://github.com/finagle/finch/issues/552
def time[A](stat: Stat, e: Endpoint[A]): Endpoint[A] = new Endpoint[A] {
def apply(: Input): Endpoint.Result[A] = {
e(i).map {
case (remainder, output) => remainder -> output.map { f =>
Stat.timeFuture(stat)(f)
}
@vkostyukov
vkostyukov / auth.scala
Created February 17, 2016 20:11
Simple Auth with Finch
scala> case class Auth(u: String)
defined class Auth
scala> val auth = headerOption("Auth").withDefault("anon").as[Auth].mapOutput { a =>
| if (a.u == "foo") Ok(a) else Unauthorized(new Exception("wrong credentials"))
| }
scala> val e = get("foo" :: auth).map(_.u)
e: io.finch.Endpoint[String] = GET /foo/header(Auth)
@vkostyukov
vkostyukov / Dict.scala
Last active February 4, 2016 20:16
Dict.scala
sealed trait Dict
object Dict {
// dict(“a”: “apple”, “b”: dict(“c”: “cat”, “d”: “dog”)) -> “{a:apple,b:{c:cat,d:dog}}”
case class Val(s: String) extends Dict
case class Obj(m: Map[String, Dict]) extends Dict
def encode(d: Dict): String = {
def loop(dd: Dict, sb: StringBuilder): StringBuilder = dd match {
case Val(s) => sb.append(s)
case Obj(m) =>
import io.finch._
import com.twitter.finagle.http.Status
import io.circe._
import io.circe.generic.auto._
import io.finch.circe._
// Here we tell Finch how to encode exceptions to send them over the wire.
val e: Encoder[Exception] = Encoder.instance {
case ce: MyError => MyError.jsonEncoder(ce)
@vkostyukov
vkostyukov / group.scala
Created December 23, 2015 19:37
Group Endpoints
scala> trait Foo1; trait Foo2
defined trait Foo1
defined trait Foo2
scala> trait Bar1; trait Bar2;
defined trait Bar1
defined trait Bar2
scala> object Foo {
| val endpoints = Endpoint(Ok(new Foo1{})) :+: Endpoint(Ok(new Foo2{}))
@vkostyukov
vkostyukov / statuses.md
Last active February 13, 2024 21:39
HTTP status codes used by world-famous APIs
API Status Codes
[Twitter][tw] 200, 304, 400, 401, 403, 404, 406, 410, 420, 422, 429, 500, 502, 503, 504
[Stripe][stripe] 200, 400, 401, 402, 404, 429, 500, 502, 503, 504
[Github][gh] 200, 400, 422, 301, 302, 304, 307, 401, 403
[Pagerduty][pd] 200, 201, 204, 400, 401, 403, 404, 408, 500
[NewRelic Plugins][nr] 200, 400, 403, 404, 405, 413, 500, 502, 503, 503
[Etsy][etsy] 200, 201, 400, 403, 404, 500, 503
[Dropbox][db] 200, 400, 401, 403, 404, 405, 429, 503, 507
@vkostyukov
vkostyukov / HelloWorld.scala
Created November 11, 2015 13:30
HelloWorld
import com.twitter.finagle.Http
import com.twitter.util.{Future, Await}
import io.finch._
object Test1 extends App {
implicit val encodeMap: EncodeResponse[Map[String, String]] =
EncodeResponse("text/plain")(map =>
Buf.Utf8(map.toSeq.map(kv => kv._1 + ":" + kv._2).mkString("\n"))
)
@vkostyukov
vkostyukov / migrate.sh
Last active June 6, 2019 18:55
Migrate to Finagle 6.30
# Migrate to Finagle 6.30 https://groups.google.com/forum/#!topic/finaglers/skmdgDhcP0c
git grep -lz 'finagle.httpx' | xargs -0 sed -i '' -e 's/finagle.httpx/finagle.http/g'
git grep -lz 'Httpx' | xargs -0 sed -i '' -e 's/Httpx/Http/g'
@vkostyukov
vkostyukov / finch-auth.scala
Last active October 17, 2015 17:57
Finch Auth
def auth0[A, B](doAuth: Request => Future[A])(e: Endpoint[B])(implicit
adjoin: PairAdjoin[A, B]
): Endpoint[adjoin.Out] = new Endpoint[adjoin.Out] {
def apply(input: Input): Option[(Input, () => Future[Output[adjoin.Out]])] =
e(input).map {
case (remainder, output) =>
(remainder, () => for {
a <- doAuth(input.request)
ob <- output()
} yield b.copy(value = adjoin(a, b.value)))