|
49 | 49 | A function which the same args as :run-fn that calculates a cache key for the
|
50 | 50 | given args. Optional, defaults to nil, i.e. no caching.
|
51 | 51 |
|
| 52 | + :init-fn |
| 53 | +
|
| 54 | + A function that takes a definition map and HystrixCommand$Setter which should return |
| 55 | + a HystrixCommand$Setter (usually the one passed in) to ultimately be passed to the |
| 56 | + constructor of the HystrixCommand. For example, |
| 57 | +
|
| 58 | + (fn [_ setter] |
| 59 | + (.andCommandPropertiesDefaults setter ...)) |
| 60 | +
|
| 61 | + This is your escape hatch into raw Hystrix. |
| 62 | +
|
52 | 63 | The com.netflix.hystrix.core/defcommand macro is a helper for defining this map and storing it
|
53 | 64 | in a var. For example, here's a definition for an addition command:
|
54 | 65 |
|
|
118 | 129 | collapser. Optional, defaults to a function returning nil, i.e. no caching.
|
119 | 130 | This function should be completely free of side effects.
|
120 | 131 |
|
| 132 | + :init-fn |
| 133 | +
|
| 134 | + A function that takes a definition map and HystrixCollapser$Setter which should return |
| 135 | + a HystrixCollapser$Setter (usually the one passed in) to ultimately be passed to the |
| 136 | + constructor of the HystrixCollapser. For example, |
| 137 | +
|
| 138 | + (fn [_ setter] |
| 139 | + (.andCollapserPropertiesDefaults setter ...)) |
| 140 | +
|
| 141 | + This is your escape hatch into raw Hystrix. |
| 142 | +
|
121 | 143 | The com.netflix.hystric.core/defcollapser macro is a helper for defining this map and storing it
|
122 | 144 | in a callable var.
|
123 | 145 | "
|
|
236 | 258 | :hystrix/fallback-fn :fallback-fn
|
237 | 259 | :hystrix/group-key :group-key
|
238 | 260 | :hystrix/command-key :command-key
|
239 |
| - :hystrix/thread-pool-key :thread-pool-key }] |
| 261 | + :hystrix/thread-pool-key :thread-pool-key |
| 262 | + :hystrix/init-fn :init-fn }] |
240 | 263 | (set/rename-keys (select-keys meta-map (keys key-map)) key-map)))
|
241 | 264 |
|
242 | 265 | (defmacro defcommand
|
|
300 | 323 | (let [key-map {:hystrix/collapser-key :collapser-key
|
301 | 324 | :hystrix/shared-fn :shard-fn
|
302 | 325 | :hystrix/scope :scope
|
303 |
| - :hystrix/cache-key-fn :cache-key-fn }] |
| 326 | + :hystrix/cache-key-fn :cache-key-fn |
| 327 | + :hystrix/init-fn :init-fn }] |
304 | 328 | (set/rename-keys (select-keys meta-map (keys key-map)) key-map)))
|
305 | 329 |
|
306 | 330 | (defn collapser
|
|
492 | 516 | ((required-fn :run-fn))
|
493 | 517 | ((optional-fn :fallback-fn))
|
494 | 518 | ((optional-fn :cache-key-fn))
|
| 519 | + ((optional-fn :init-fn)) |
495 | 520 |
|
496 | 521 | (update-in [:group-key] group-key)
|
497 | 522 | (update-in [:command-key] command-key)
|
498 | 523 | (update-in [:thread-pool-key] thread-pool-key)))
|
499 | 524 |
|
500 | 525 | (defmethod instantiate* :command
|
501 |
| - [{:keys [group-key command-key thread-pool-key run-fn fallback-fn cache-key-fn]} & args] |
502 |
| - (let [setter (doto (HystrixCommand$Setter/withGroupKey group-key) |
503 |
| - ; TODO other properties |
| 526 | + [{:keys [group-key command-key thread-pool-key |
| 527 | + run-fn fallback-fn cache-key-fn |
| 528 | + init-fn] :as def-map} & args] |
| 529 | + (let [setter (-> (HystrixCommand$Setter/withGroupKey group-key) |
504 | 530 | (.andCommandKey command-key)
|
505 |
| - (.andThreadPoolKey thread-pool-key))] |
506 |
| - (proxy [HystrixCommand] [setter] |
| 531 | + (.andThreadPoolKey thread-pool-key)) |
| 532 | + setter (if init-fn |
| 533 | + (init-fn def-map setter) |
| 534 | + setter)] |
| 535 | + (when (not (instance? HystrixCommand$Setter setter)) |
| 536 | + (throw (IllegalStateException. (str ":init-fn didn't return HystrixCommand$Setter instance")))) |
| 537 | + (proxy [HystrixCommand] [^HystrixCommand$Setter setter] |
507 | 538 | (run [] (apply run-fn args))
|
508 | 539 | (getFallback [] (if fallback-fn
|
509 | 540 | (apply fallback-fn args)
|
|
522 | 553 | ((required-fn :map-fn))
|
523 | 554 | ((optional-fn :shard-fn))
|
524 | 555 | ((optional-fn :cache-key-fn))
|
| 556 | + ((optional-fn :init-fn)) |
525 | 557 |
|
526 | 558 | (update-in [:collapser-key] collapser-key)
|
527 | 559 | (update-in [:scope] (fnil collapser-scope HystrixCollapser$Scope/REQUEST))))
|
|
531 | 563 | (.getArgument request))
|
532 | 564 |
|
533 | 565 | (defmethod instantiate* :collapser
|
534 |
| - [{:keys [collapser-key scope collapse-fn map-fn shard-fn cache-key-fn]} & args] |
535 |
| - (let [setter (doto (HystrixCollapser$Setter/withCollapserKey collapser-key) |
536 |
| - ; TODO other properties |
537 |
| - (.andScope scope))] |
538 |
| - (proxy [HystrixCollapser] [setter] |
| 566 | + [{:keys [collapser-key scope |
| 567 | + collapse-fn map-fn shard-fn cache-key-fn |
| 568 | + init-fn] :as def-map} & args] |
| 569 | + (let [setter (-> (HystrixCollapser$Setter/withCollapserKey collapser-key) |
| 570 | + (.andScope scope)) |
| 571 | + setter (if init-fn |
| 572 | + (init-fn def-map setter) |
| 573 | + setter)] |
| 574 | + (when (not (instance? HystrixCollapser$Setter setter)) |
| 575 | + (throw (IllegalStateException. (str ":init-fn didn't return HystrixCollapser$Setter instance")))) |
| 576 | + (proxy [HystrixCollapser] [^HystrixCollapser$Setter setter] |
539 | 577 | (getCacheKey [] (if cache-key-fn
|
540 | 578 | (apply cache-key-fn args)))
|
541 | 579 |
|
|
0 commit comments