Skip to content

Commit 8ca8625

Browse files
committed
reimplement executor-for to handle special tag and memoize
1 parent c365f93 commit 8ca8625

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

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

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,41 @@
7171
[workflow]
7272
(Executors/newCachedThreadPool (counted-thread-factory (str "async-" (name workflow) "-%d") true)))
7373

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]
9176
(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)))))
95109

96110
(defn exec
97111
[^Runnable r workload]

0 commit comments

Comments
 (0)