Skip to content

Commit 78758c4

Browse files
committed
allow user to define es-factory via sysprop
1 parent 3560c65 commit 78758c4

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/main/clojure/clojure/core/async/impl/dispatch.clj

+27-17
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,35 @@
8080
(.uncaughtException (Thread/currentThread) ex))
8181
nil)
8282

83-
(defonce ^ExecutorService mixed-executor
84-
(Executors/newCachedThreadPool (counted-thread-factory "async-mixed-%d" true)))
85-
86-
(defonce ^ExecutorService io-executor
87-
(Executors/newCachedThreadPool (counted-thread-factory "async-io-%d" true)))
88-
89-
(defonce ^ExecutorService compute-executor
90-
(Executors/newCachedThreadPool (counted-thread-factory "async-compute-%d" true)))
83+
(def construct-es
84+
(let [esf (System/getProperty "clojure.core.async.esfactory")]
85+
(or
86+
(and esf (requiring-resolve (symbol esf)))
87+
(fn [workload]
88+
(case workload
89+
:compute (Executors/newCachedThreadPool (counted-thread-factory "async-compute-%d" true))
90+
:io (Executors/newCachedThreadPool (counted-thread-factory "async-io-%d" true))
91+
:mixed (Executors/newCachedThreadPool (counted-thread-factory "async-mixed-%d" true))
92+
(throw (IllegalArgumentException. (str "Illegal workload tag " workload))))))))
93+
94+
(defonce ^ExecutorService mixed-executor (construct-es :mixed))
95+
96+
(defonce ^ExecutorService io-executor (construct-es :io))
97+
98+
(defonce ^ExecutorService compute-executor (construct-es :compute))
99+
100+
(defn es-for [workload]
101+
(case workload
102+
:compute compute-executor
103+
:io io-executor
104+
:mixed mixed-executor
105+
nil))
91106

92107
(defn exec
93-
[^Runnable r exec]
94-
(let [^ExecutorService e (case exec
95-
:compute compute-executor
96-
:io io-executor
97-
:mixed mixed-executor
98-
nil)]
99-
(if e
100-
(.execute e r)
101-
(impl/exec @executor r))))
108+
[^Runnable r workload]
109+
(if-let [^ExecutorService e (es-for workload)]
110+
(.execute e r)
111+
(impl/exec @executor r)))
102112

103113
(defn run
104114
"Runs Runnable r on current thread when :on-caller? meta true, else in a thread pool thread."

0 commit comments

Comments
 (0)