|
| 1 | +* Dataflow |
| 2 | + |
| 3 | +This namespace is designed to compose simple core.async processes. |
| 4 | +It's probably suitable for micro-services. |
| 5 | + |
| 6 | +The configuration and data model are inspired by [[https://github.com/onyx-platform/onyx][Onyx]]. |
| 7 | + |
| 8 | +** Motivation |
| 9 | + |
| 10 | +There is usually little need to go through the logistics and ceremony, when |
| 11 | +using core.async, to manage the channels, puts and takes, and in most instances, |
| 12 | +lifecycle, as it naturally emerges from the topology. |
| 13 | + |
| 14 | +Processes' topologies are usually an emergent phenomenon and not explicitly stated. |
| 15 | +There is a mix between topology, business logic, and low level async APIs. |
| 16 | + |
| 17 | +The idea is to separate the topology of the process from logic as much as |
| 18 | +possible by providing a data language to describe the data flow, and functions |
| 19 | +and other vars are to be resolved when "compiling" the model. |
| 20 | + |
| 21 | +** Data Model |
| 22 | + |
| 23 | + - Edges: core.async channels |
| 24 | + - Vertices: processing units, connected by channels. Can be pipes, drivers, sinks. |
| 25 | + |
| 26 | + The graph is describe in terms of two collections: |
| 27 | + |
| 28 | + - Edges: data describing only the channels, including buffer types, buffer functions, size and transducers. |
| 29 | + - Nodes: data describing a pipeline between two channels, mult, producer or consumer. |
| 30 | + |
| 31 | +*** Buffers |
| 32 | + |
| 33 | +#+begin_src clojure |
| 34 | + {::buffer/type ::buffer/blocking |
| 35 | + ::buffer/size 8} |
| 36 | + |
| 37 | + {::buffer/type ::buffer/sliding |
| 38 | + ::buffer/size 8} |
| 39 | + |
| 40 | + {::buffer/type ::buffer/dropping |
| 41 | + ::buffer/size 8} |
| 42 | +#+end_src |
| 43 | + |
| 44 | +*** Channels |
| 45 | + |
| 46 | +#+begin_src clojure |
| 47 | + {::chan/name :a |
| 48 | + ::chan/type ::chan/simple} |
| 49 | + |
| 50 | + {::chan/name :b |
| 51 | + ::chan/type ::chan/sized |
| 52 | + ::chan/size 8} |
| 53 | + |
| 54 | + {::chan/name :c |
| 55 | + ::chan/type ::chan/buffered |
| 56 | + ::chan/buffer {::buffer/type ::buffer/blocking |
| 57 | + ::buffer/size 8}} |
| 58 | +#+end_src |
| 59 | + |
| 60 | +** Extension |
| 61 | + |
| 62 | +*** Buffers |
| 63 | + |
| 64 | +#+begin_src clojure |
| 65 | + (defmethod buffer/-type ::your-new-type [_] ::spec-for-your-type) |
| 66 | + |
| 67 | + (defmethod buffer/-compile ::your-new-type |
| 68 | + [{:keys [:buffer/arg1 :buffer/arg2]}] |
| 69 | + (your-buffer-fn arg1 arg2)) |
| 70 | + |
| 71 | + ;;; Example from buffer namespace |
| 72 | + |
| 73 | + (defmethod -compile ::dropping [{:keys [::size]}] (a/dropping-buffer size)) |
| 74 | +#+end_src |
| 75 | + |
| 76 | +*** Channels |
| 77 | + |
| 78 | +#+begin_src clojure |
| 79 | + (defmethod chan/-type ::your-new-type [_] ::spec-for-your-type) |
| 80 | + |
| 81 | + (defmethod chan/-compile ::your-new-type |
| 82 | + [{:keys [:chan/arg1 :chan/arg2]}] |
| 83 | + (your-chan-fn arg1 arg2)) |
| 84 | + |
| 85 | + ;; Example from channel namespace |
| 86 | + |
| 87 | + (defmethod -compile ::buffered [{:keys [::buffer]}] (a/chan (buffer/-compile buffer))) |
| 88 | +#+end_src |
| 89 | + |
| 90 | +*** Worker nodes |
| 91 | + |
| 92 | +Worker nodes compilers also take an environment argument which contains the channels |
| 93 | + |
| 94 | +#+begin_src clojure |
| 95 | + (defmethod node/-type ::your-new-type [_] ::spec-for-your-type) |
| 96 | + |
| 97 | + (defmethod node/-compile ::your-new-type |
| 98 | + [{:keys [:node/arg1 :node/arg2]} env] |
| 99 | + (your-node-fn arg1 arg2)) |
| 100 | + |
| 101 | + ;; Example from node namespace |
| 102 | + |
| 103 | + (defmethod -compile ::pipeline-blocking |
| 104 | + [{{to ::to from ::from size ::size xf ::xf} ::pipeline} env] |
| 105 | + (a/pipeline-blocking size (env to) xf (env from))) |
| 106 | +#+end_src |
| 107 | + |
| 108 | +** Usage |
| 109 | + |
| 110 | +*** Require dataflow namespaces |
| 111 | + |
| 112 | +#+begin_src clojure |
| 113 | + (require '[more.async.dataflow.node :as node] |
| 114 | + '[more.async.dataflow.channel :as chan] |
| 115 | + '[more.async.dataflow.buffer :as buffer] |
| 116 | + '[more.async.dataflow :as flow]) |
| 117 | +#+end_src |
| 118 | + |
| 119 | +*** Define a model |
| 120 | + |
| 121 | +- Define model with channels and nodes (can be verified using spec). |
| 122 | +- Define the required vars. |
| 123 | +- Validate the model using the ~::flow/model~ spec. |
| 124 | +- Try compiling the model using ~compile-model~. |
| 125 | + |
| 126 | +*** Example |
| 127 | + |
| 128 | +#+begin_src clojure |
| 129 | + (def model |
| 130 | + {::channels |
| 131 | + [{::chan/name :in |
| 132 | + ::chan/type ::chan/sized |
| 133 | + ::chan/size 1} |
| 134 | + {::chan/name :out |
| 135 | + ::chan/type ::chan/sized |
| 136 | + ::chan/size 1}] |
| 137 | + ::nodes |
| 138 | + [ |
| 139 | + |
| 140 | + {::node/name :producer |
| 141 | + ::node/type ::node/produce |
| 142 | + ::node/produce |
| 143 | + {::node/to :in |
| 144 | + ::node/async true |
| 145 | + ::node/fn (let [a (atom 0)] |
| 146 | + (fn drive [] |
| 147 | + (Thread/sleep 1000) |
| 148 | + (swap! a inc)))}} |
| 149 | + |
| 150 | + {::node/name :pipeline |
| 151 | + ::node/type ::node/pipeline-blocking |
| 152 | + ::node/pipeline |
| 153 | + {::node/from :in |
| 154 | + ::node/to :out |
| 155 | + ::node/size 4 |
| 156 | + ::node/xf (map (fn [x] (println x) (Thread/sleep 2500) x))}} |
| 157 | + |
| 158 | + {::node/name :consumer |
| 159 | + ::node/type ::node/consume |
| 160 | + ::node/consume |
| 161 | + {::node/from :out |
| 162 | + ::node/fn (fn [x] (println :OUT x)) |
| 163 | + ::node/async? true}}]}) |
| 164 | + |
| 165 | + (s/valid? ::flow/channels (::channels model)) |
| 166 | + |
| 167 | + (s/valid? ::flow/nodes (::nodes model)) |
| 168 | + |
| 169 | + (s/valid? ::flow/model model) |
| 170 | + |
| 171 | + (s/valid? ::flow/connected model) |
| 172 | + |
| 173 | + (def system (compile-model model)) |
| 174 | + |
| 175 | + (a/close! (:in (::channels system))) |
| 176 | +#+end_src |
| 177 | + |
| 178 | +** Status |
| 179 | + |
| 180 | +Experimental. Looking for user reports. |
| 181 | + |
| 182 | +** Roadmap |
| 183 | + |
| 184 | +- [ ] Tests |
| 185 | +- [ ] Analyze the topology to find any dangling channels or disconnected pipes before instancing the pipes. |
| 186 | +- [ ] Implement ~select~ based on ~alt!~ and/or ~alts!~. |
| 187 | +- [ ] Find an idiomatic way to connect a web handler as driver. |
| 188 | +- [ ] Refine specs, currently have no way to differentiate transducers from regular functions. |
0 commit comments