Slide 1

Slide 1 text

Mastering time with Clojure core.async Howard M. Lewis Ship TWD Consulting © 2013 Howard M. Lewis Ship

Slide 2

Slide 2 text

(defn request-handler [request] (let [data1 (do-query-1 request) data2 (do-query-2 request) data3 (do-query-3 request)] (send-response data1 data2 data3)))

Slide 3

Slide 3 text

1 3 2

Slide 4

Slide 4 text

Channel Writer Writer Writer Reader Reader

Slide 5

Slide 5 text

(>!! chan val) (

Slide 6

Slide 6 text

Channel Writer Writer Writer Reader Reader

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Channel – (chan 1) (>!! chan val) (>!! chan val) (

Slide 9

Slide 9 text

using multiple threads

Slide 10

Slide 10 text

(defn request-handler [request] (let [data1 (do-query-1 request) data2 (do-query-2 request) data3 (do-query-3 request)] (send-response data1 data2 data3)))

Slide 11

Slide 11 text

(defn request-handler [request] (let [query1 (thread (do-query-1 request)) query2 (thread (do-query-2 request)) query3 (thread (do-query-3 request)) data1 (

Slide 12

Slide 12 text

(defn request-handler [request] (let [query1 (thread (do-query-1 request)) query2 (thread (do-query-2 request)) query3 (thread (do-query-3 request)) data1 (

Slide 13

Slide 13 text

(thread (do-query-1 request)) (

Slide 14

Slide 14 text

1 3 2

Slide 15

Slide 15 text

What if request is modified before pooled thread executes? (defn request-handler [request] (let [query1 (thread (do-query-1 request)) query2 (thread (do-query-2 request)) query3 (thread (do-query-3 request)) data1 (

Slide 16

Slide 16 text

Answer: Mu Clojure data structures are immutable - free to share across threads

Slide 17

Slide 17 text

freedom from blocking

Slide 18

Slide 18 text

(defn request-handler [request] (let [result (go (let [query1 (thread (do-query-1 request)) query2 (thread (do-query-2 request)) query3 (thread (do-query-3 request)) data1 (

Slide 19

Slide 19 text

(go (let [… data1 (

Slide 20

Slide 20 text

Blocking

Slide 21

Slide 21 text

Parking

Slide 22

Slide 22 text

1 3 2

Slide 23

Slide 23 text

(defn request-handler [request] (go (let [query1 (thread (do-query-1 request)) query2 (thread (do-query-2 request)) query3 (thread (do-query-3 request)) data1 (

Slide 24

Slide 24 text

Inversion of Control query1 (thread (do-query-1 request)) query2 (thread (do-query-2 request)) query3 (thread (do-query-3 request)) 0 data1 (

Slide 25

Slide 25 text

What is an API?

Slide 26

Slide 26 text

public interface RequestProcessor { Response process(Request request); … } (defprotocol RequestProcessor (process! [this request] … ) (ns gigantor.request.processor …) (defn process! [request] …) POST http://gigantor.com/request …

Slide 27

Slide 27 text

Input ➠ Process ➠ Result

Slide 28

Slide 28 text

Synchronous (let [result (process! request)] Asynchronous

Slide 29

Slide 29 text

Synchronous What parameters do I pass to the function? Asynchronous What value do I send to the channel? How & when do I get the result?

Slide 30

Slide 30 text

(defn start-request-processor [request-channel] (go (while true (let [[request response-channel] (! response-channel response)))) nil)

Slide 31

Slide 31 text

(defn start-request-processor [request-channel] (go (while true (let [[request response-channel] (! response-channel response)))) nil) request channel is a parameter

Slide 32

Slide 32 text

(defn start-request-processor [request-channel] (go (while true (let [[request response-channel] (! response-channel response)))) nil) Client sends the request and a channel for the response

Slide 33

Slide 33 text

(defn start-request-processor [request-channel] (go (while true (let [[request response-channel] (! response-channel response)))) nil) timeout is part of core.async

Slide 34

Slide 34 text

(defn start-request-processor [request-channel] (go (while true (let [[request response-channel] (! response-channel response)))) nil) Send the response where the client specified

Slide 35

Slide 35 text

(defn process-request [request processor-channel] (let [response-channel (chan 1)] (>!! processor-channel [request response-channel]) response-channel))

Slide 36

Slide 36 text

(defn process-request [request processor-channel] (let [response-channel (chan 1)] (>!! processor-channel [request response-channel]) response-channel)) Buffer for 1 response, to keep from blocking/parking

Slide 37

Slide 37 text

(defn process-request [request processor-channel] (let [response-channel (chan 1)] (>!! processor-channel [request response-channel]) response-channel)) The API: which channel, what data

Slide 38

Slide 38 text

0ms 1,500ms 3,000ms 4,500ms 6,000ms 7,500ms 9,000ms 10,500ms 12,000ms 13,500ms 15,000ms 1 2 5 10 25 Total Processing Time Number of processing go blocks 100 Requests 250 Requests Caution: Very Not Scientific! ⚠

Slide 39

Slide 39 text

How deep a buffer on the channel? How many channel processors? Is the channel buffer lossy or blocking? Timeout the operation if not fast enough?

Slide 40

Slide 40 text

beyond simply concurrent

Slide 41

Slide 41 text

(defn get-stock-price [ticker-name] (go (let [disk-response-channel (chan 1) _ (>! disk-cache-channel [ticker-name disk-response-channel]) api-response-channel (chan 1) _ (!> api-channel [ticker-name api-response-channel]) [stock-value from-channel] (alts! [disk-response-channel api-response-channel (timeout 200)])] (if stock-value (:price stock-value) :unknown))))

Slide 42

Slide 42 text

(defn get-stock-price [ticker-name] (go (let [disk-response-channel (chan 1) _ (>! disk-cache-channel [ticker-name disk-response-channel]) api-response-channel (chan 1) _ (!> api-channel [ticker-name api-response-channel]) [stock-value from-channel] (alts! [disk-response-channel api-response-channel (timeout 200)])] (if stock-value (:price stock-value) :unknown)))) First async request

Slide 43

Slide 43 text

(defn get-stock-price [ticker-name] (go (let [disk-response-channel (chan 1) _ (>! disk-cache-channel [ticker-name disk-response-channel]) api-response-channel (chan 1) _ (!> api-channel [ticker-name api-response-channel]) [stock-value from-channel] (alts! [disk-response-channel api-response-channel (timeout 200)])] (if stock-value (:price stock-value) :unknown)))) Second async request

Slide 44

Slide 44 text

(defn get-stock-price [ticker-name] (go (let [disk-response-channel (chan 1) _ (>! disk-cache-channel [ticker-name disk-response-channel]) api-response-channel (chan 1) _ (!> api-channel [ticker-name api-response-channel]) [stock-value from-channel] (alts! [disk-response-channel api-response-channel (timeout 200)])] (if stock-value (:price stock-value) :unknown)))) Parks until one operation has completed

Slide 45

Slide 45 text

(defn get-stock-price [ticker-name] (go (let [disk-response-channel (chan 1) _ (>! disk-cache-channel [ticker-name disk-response-channel]) api-response-channel (chan 1) _ (!> api-channel [ticker-name api-response-channel]) [stock-value from-channel] (alts! [disk-response-channel api-response-channel (timeout 200)])] (if stock-value (:price stock-value) :unknown)))) alts! returns the value and the channel that provided the value

Slide 46

Slide 46 text

(defn get-stock-price [ticker-name] (go (let [disk-response-channel (chan 1) _ (>! disk-cache-channel [ticker-name disk-response-channel]) api-response-channel (chan 1) _ (!> api-channel [ticker-name api-response-channel]) [stock-value from-channel] (alts! [disk-response-channel api-response-channel (timeout 200)])] (if stock-value (:price stock-value) :unknown)))) stock-value is nil if timeout

Slide 47

Slide 47 text

Channel Writer Writer Writer Reader Reader Reader Reader Live Restarts

Slide 48

Slide 48 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true))))

Slide 49

Slide 49 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true)))) Passed in from "supervisor"

Slide 50

Slide 50 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true)))) Handle a request, recur to handle the next

Slide 51

Slide 51 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true)))) Like alts!, but evaluates matching expression

Slide 52

Slide 52 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true)))) Exit the loop

Slide 53

Slide 53 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true)))) Destructure the value taken from channel

Slide 54

Slide 54 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true)))) Could be a go block, or function that uses a go block

Slide 55

Slide 55 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true)))) Handle the next request

Slide 56

Slide 56 text

(defn start-request-processor [request-channel poison-pill-channel] (go (loop [] (alt! poison-pill-channel nil request-channel ([[request response-channel]] (let [_ (! response-channel response) (recur))) :priority true)))) Option: select 1st clause if both ready (otherwise random)

Slide 57

Slide 57 text

notes and gotchas

Slide 58

Slide 58 text

ClojureScript

Slide 59

Slide 59 text

thread pools (thread) ➠ async-thread-macro-# cached thread pool threads discarded after 60 seconds (go …) etc ➠ async-dispatch-# fixed thread pool, 2 * # of processors + 42 All daemon threads

Slide 60

Slide 60 text

thread pools (go …) blocks should never block (only park) Use (thread …) for code that might block: file system access, database access

Slide 61

Slide 61 text

java.lang.AssertionError: Assert failed: No more than 1024 pending puts are allowed on a single channel. Consider using a windowed buffer. (< (.size puts) impl/MAX-QUEUE-SIZE) at clojure.core.async.impl.channels.ManyToManyChannel.put_BANG_(channels.clj: 95) at clojure.core.async.impl.ioc_macros$put_BANG_.invoke(ioc_macros.clj: 824) at poc.async_alts$get_answer $fn__1764$state_machine__1239__auto____1765.invoke(async_alts.clj:23) at clojure.core.async.impl.ioc_macros $run_state_machine.invoke(ioc_macros.clj:812) at poc.async_alts$get_answer$fn__1764.invoke(async_alts.clj:23) at clojure.lang.AFn.run(AFn.java:24) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1145) at java.util.concurrent.ThreadPoolExecutor $Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:724)

Slide 62

Slide 62 text

Testing Easy to provide mock channel handlers alts! may be harder to deal with

Slide 63

Slide 63 text

wrap up

Slide 64

Slide 64 text

https://github.com/clojure/core.async

Slide 65

Slide 65 text

http://swannodette.github.io/ David Nolen

Slide 66

Slide 66 text

http://howardlewisship.com

Slide 67

Slide 67 text

Image Credits http://www.flickr.com/photos/8704943@N07/3491779722 - magro_kr http://www.flickr.com/photos/39779537@N05/4589192110 - Lucian Bickerton http://www.flickr.com/photos/38102495@N00/394602448 - jasoneppink http://www.flickr.com/photos/27874172@N03/2682162358/ - Peter Gorges

Slide 68

Slide 68 text

Q & A