Finagle & Clojure
by Alexey Kachayev for #FinagleCon 2015
Slide 2
Slide 2 text
About Me
‣ Alexey Kachayev, @kachayev
‣ CTO at Attendify.com
• Almost-all-day-coding-kind-of-CTO
‣ Active open source contributor
Slide 3
Slide 3 text
Attendify Use Case
Detailed Event
information.
Private event
community and
social network.
A fully featured
event app with
essential content.
Slide 4
Slide 4 text
Attendify Use Case
‣ Private social networks for events (thousands)
‣ A lot of microservices both in Scala & Clojure
‣ Finagle to run services written in Scala
‣ Finagle to run services written in Clojure (???)
Slide 5
Slide 5 text
finagle-clojure
‣ https://github.com/finagle/finagle-clojure
‣ “A thin Clojure wrapper around Finagle”
‣ Scala interop
‣ Bridge to Thrift/ThriftMux and more
‣ lein template (quick start)
finagle-clojure?
‣ Scala API is not idiomatic for Clojure
• easy to translate examples from the Internet
• hard to play with the rest of the code
‣ Should we write Scala code using Clojure syntax?
Slide 12
Slide 12 text
Everything is a Data
‣ Builder API for servers & clients
‣ Hash-maps instead chained mutators
‣ Hash-maps instead of HTTP Request/Response
builders
Scala Futures in Clojure
‣ Two branches of execution (success & errors)
‣ Exception-driven errors handling
‣ Hard to manage execution context
‣ There is a better way to work with chaining
Slide 16
Slide 16 text
Clojure Futures
‣ A lot of “native” primitives:
• futures, promises
• agents
• pmap, parallel reducers
‣ Clojure Futures are not that great for doing I/O
core.async
‣ https://github.com/clojure/core.async
‣ “Library designed to provide facilities for async
programming and communication”
‣ CSP as a library
‣ go macro to turn async code into a state machine
‣ Widely adopted by Clojure community
What About Server
(:import
[com.twitter.util
Promise])
(defn
propagate-‐to
[promise
value]
(if
(e/left?
value)
(.setException
promise
(e/left-‐value
value))
(.setValue
promise
(e/right-‐value
value))))
(defn
chan-‐>future
[c]
(let
[promise
(Promise.)]
(a/take!
c
(partial
propagate-‐to
promise))
promise))
Slide 27
Slide 27 text
What About Server
(:import
[com.twitter.util
Promise])
(defn
propagate-‐to
[promise
value]
(if
(e/left?
value)
(.setException
promise
(e/left-‐value
value))
(.setValue
promise
(e/right-‐value
value))))
(defn
chan-‐>future
[c]
(let
[promise
(Promise.)]
(a/take!
c
(partial
propagate-‐to
promise))
promise))
Slide 28
Slide 28 text
Finagle & core.async
‣ Clean & concise async code
‣ A lot of built-in primitives (timeout, pub/sub etc)
‣ Compatibility with tons of Clojure libraries based on
core.async
‣ Limited usage in request-reply world though
Slide 29
Slide 29 text
Stitch → Muse
‣ Stitch is a Scala library for composing RPC services
‣ Created in Twitter, introduces by Jake Donham
‣ As Stitch is not open sourced…
‣ github.com/kachayev/muse
‣ EuroClojure talk about it
Slide 30
Slide 30 text
Stitch → Muse
‣ Runs independent data fetches concurrently
• Uses BFS to group fetches level-by-level
‣ Caches previously made fetches during execution
‣ Batches requests when applicable
‣ Uses the idea of building and interpreting AST
‣ Uses core.async to deal with concurrency
Slide 31
Slide 31 text
More Clojure Codec
‣ Scala has Scodec & finagle-serial
‣ Clojure has Fressian
• Designed with EDN in mind
• Rich set of core types & extensions
• Middleware-friendly with tagged objects
‣ Working on Fressian codec right now
Conclusion
‣ Finagle is great
‣ Clojure is amazing
‣ Finagle can solve a lot of problems for Clojure
ecosystem
‣ Finagle & Clojure integration still requires a lot of work