Skip to content

Commit 2df8e63

Browse files
committed
Add :ping-map-fn for shaping proc state in ping replies
1 parent a0e29f6 commit 2df8e63

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/main/clojure/clojure/core/async/flow.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@
135135

136136
(defn ping
137137
"pings all processes, returning a map of pid -> proc status and
138-
state, for those procs that reply within timeout-ms (default 1000)"
138+
state, for those procs that reply within timeout-ms (default 1000).
139+
The value at ::flow/state is shaped by the proc's :ping-map-fn (see
140+
process), defaulting to the proc's raw state."
139141
[g & {:keys [timeout-ms] :or {timeout-ms 1000}}]
140142
(g/ping g timeout-ms))
141143

@@ -186,6 +188,9 @@
186188
For the simple case of enumerated signal-ids, use a set,
187189
e.g. #{:this/signal :that/signal}
188190
If no :signal-select is provided, no signals will be received
191+
:ping-map-fn - (ping-map-fn state) -> ping-map. The ping-map is
192+
placed under ::flow/state in ping/ping-proc replies.
193+
Defaults to identity.
189194
:workload with possible values of :mixed :io :compute.
190195
All entries in the describe return map are optional.
191196

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@
244244
"see lib ns for docs"
245245
[step {:keys [workload compute-timeout-ms] :or {compute-timeout-ms 5000}}]
246246
(let [{:keys [params ins] :as desc} (step)
247-
workload (or workload (:workload desc) :mixed)]
247+
workload (or workload (:workload desc) :mixed)
248+
ping-map-fn (or (:ping-map-fn desc) identity)]
248249
;;(assert (or (not params) init) "must have :init if :params")
249250
(reify
250251
clojure.core.protocols/Datafiable
@@ -276,7 +277,7 @@
276277
#::flow{:pid pid, :status status
277278
:count count
278279
:ins pins :outs pouts})
279-
::flow/state state))))
280+
::flow/state (ping-map-fn state)))))
280281
handle-command (partial handle-command pid pong)
281282
[nstatus nstate count read-ins]
282283
(try

src/test/clojure/clojure/core/async/flow_test.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,10 @@
5353
(let [state-val {:a-var #'identity :a-fn inc}
5454
ping-state (ping-state-with (minimal-step state-val))]
5555
(is (= state-val ping-state) "state should be returned unchanged"))))
56+
57+
(deftest test-ping-map-fn
58+
(testing ":ping-map-fn can redact sensitive keys from state"
59+
(let [ping-state (ping-state-with (minimal-step {:visible 42 :hidden "secret"}
60+
{:ping-map-fn #(select-keys % [:visible])}))]
61+
(is (= {:visible 42} ping-state) "secret was not removed"))))
62+

0 commit comments

Comments
 (0)