|
71 | 71 | [workflow]
|
72 | 72 | (Executors/newCachedThreadPool (counted-thread-factory (str "async-" (name workflow) "-%d") true)))
|
73 | 73 |
|
74 |
| -(def ^:private default-executor-factory |
75 |
| - (memoize |
76 |
| - (fn [workload] |
77 |
| - (case workload |
78 |
| - :compute (make-ctp-named :compute) |
79 |
| - :io (make-ctp-named :io) |
80 |
| - :mixed (make-ctp-named :mixed) |
81 |
| - :core-async-dispatch (make-ctp-named :io))))) |
82 |
| - |
83 |
| -(defn construct-executor |
84 |
| - ^ExecutorService [workload] |
85 |
| - (if-let [sysprop-ctor (when-let [esf (System/getProperty "clojure.core.async.executor-factory")] |
86 |
| - (requiring-resolve (symbol esf)))] |
87 |
| - (or (sysprop-ctor workload) (default-executor-factory workload)) |
88 |
| - (default-executor-factory workload))) |
89 |
| - |
90 |
| -(defn executor-for [workload] |
| 74 | +(defn ^:private default-executor-factory |
| 75 | + [workload] |
91 | 76 | (case workload
|
92 |
| - :compute (construct-executor :compute) |
93 |
| - :io (construct-executor :io) |
94 |
| - :mixed (construct-executor :mixed))) |
| 77 | + :compute (make-ctp-named :compute) |
| 78 | + :io (make-ctp-named :io) |
| 79 | + :mixed (make-ctp-named :mixed))) |
| 80 | + |
| 81 | +(def executor-for |
| 82 | + "Given a workload tag, returns an ExecutorService instance and memoizes the result. By |
| 83 | + default, core.async will construct a specialized ExecutorService instance for each tag |
| 84 | + according to the following tags and their expected workload profiles: |
| 85 | +
|
| 86 | + :io - may do blocking I/O but must not do extended computation |
| 87 | + :compute - must not ever block |
| 88 | + :mixed - anything else |
| 89 | +
|
| 90 | + This function will attempt to lookup a user-defined ExecutorService factory in the |
| 91 | + clojure.core.async.executor-factory system property, which should hold a string naming a |
| 92 | + namespace qualified var. The factory should accept a tag as listed above and return an |
| 93 | + ExecutorService instance for that workload or nil to accept the core.async default. |
| 94 | +
|
| 95 | + A user-defined ExecutorService factory may additionally accept a tag :core-async-dispatch |
| 96 | + and return a specialized core.async dispatch executor service. If the user factory returns |
| 97 | + nil instead then core.async will use the :io executor service (which may be handled by the |
| 98 | + user factory)." |
| 99 | + (memoize |
| 100 | + (fn ^ExecutorService [workload] |
| 101 | + (if-let [sysprop-factory (when-let [esf (System/getProperty "clojure.core.async.executor-factory")] |
| 102 | + (requiring-resolve (symbol esf)))] |
| 103 | + (if-let [sys-es (sysprop-factory workload)] |
| 104 | + sys-es |
| 105 | + (if (= workload :core-async-dispatch) |
| 106 | + (executor-for :io) |
| 107 | + (default-executor-factory workload))) |
| 108 | + (default-executor-factory workload))))) |
95 | 109 |
|
96 | 110 | (defn exec
|
97 | 111 | [^Runnable r workload]
|
|
0 commit comments