### Clojure Progressive Retry Strategy Example Source: https://github.com/grammarly/perseverance/blob/master/README.org Provides an example of configuring the `progressive-retry-strategy` in Clojure, specifying initial delay, stable length, multiplier, and maximum delay. ```clojure (progressive-retry-strategy :initial-delay 1000, :stable-length 4, :multiplier 2, :max-delay 10000) ``` -------------------------------- ### Demonstrate Perseverance with Unreliable Functions in Clojure Source: https://github.com/grammarly/perseverance/blob/master/README.org This example demonstrates how to use Perseverance to handle unreliable functions. It includes a fake function that fails a few times before succeeding and another that simulates download failures. The code shows how to wrap these functions with `retriable` and `retry` to manage retries and exception handling. ```clojure (require '[perseverance.core :as p]) ;; Fake function that returns a list of files but fails the first three times. (let [cnt (atom 0)] (defn list-s3-files [] (when (< @cnt 3) (swap! cnt inc) (throw (RuntimeException. "Failed to connect to S3."))) (range 10))) ;; Fake function that imitates downloading a file with 50/50 probability. (defn download-one-file [x] (if (> (rand) 0.5) (println (format "File #%d downloaded." x)) (throw (java.io.IOException. "Failed to download a file.")))) ;; Let's wrap the previous function in retriable. (defn download-one-file-safe [x] (p/retriable {} (download-one-file x))) ;; Now to a function that downloads all files. (defn download-all-files [] (let [files (p/retriable {:catch [RuntimeException] :tag ::list-files} (list-s3-files))] (mapv download-one-file-safe files))) ;; Let's call it and see what happens. (download-all-files) ;; Unhandled java.lang.RuntimeException: Failed to connect to S3. ;; Bam! The exception still happened. It's because we haven't established ;; the retry context. (p/retry {} (download-all-files)) ;; java.lang.RuntimeException: Failed to connect to S3., retrying in 0.5 seconds... ;; java.lang.RuntimeException: Failed to connect to S3., retrying in 0.5 seconds... ;; java.lang.RuntimeException: Failed to connect to S3., retrying in 0.5 seconds... ;; java.io.IOException: Failed to download a file., retrying in 0.5 seconds... ;; File #0 downloaded. ;; File #1 downloaded. ;; java.io.IOException: Failed to download a file., retrying in 0.5 seconds... ;; File #2 downloaded. ;; File #3 downloaded. ;; java.io.IOException: Failed to download a file., retrying in 0.5 seconds... ;; java.io.IOException: Failed to download a file., retrying in 0.5 seconds... ;; File #4 downloaded. ;; ... ;; The call eventually succeeds! ``` -------------------------------- ### Clojure Retry Macro with Constant Strategy Source: https://github.com/grammarly/perseverance/blob/master/README.org Demonstrates the basic usage of the `retry` macro in Clojure with a constant retry strategy and a selector for specific retriable exceptions. ```clojure (retry {:strategy (constant-retry-strategy 100) :selector ::list-files ;; :selector #(and (instance? ExceptionInfo %) (= (:foo (ex-data %)) :bar)) } (download-all-files)) ``` -------------------------------- ### Clojure Nested Retry Blocks Source: https://github.com/grammarly/perseverance/blob/master/README.org Illustrates how to use nested `retry` blocks in Clojure to apply different retry strategies and selectors for specific error conditions. ```clojure (retry {:strategy (constant-retry-strategy 500)} ;; Catches everything. (retry {:strategy (progressive-retry-strategy :initial-delay 200, :max-delay 10000) :selector ::list-files} (download-all-files))) ``` -------------------------------- ### Marking Code as Retriable with `retriable` Macro in Clojure Source: https://github.com/grammarly/perseverance/blob/master/README.org This snippet demonstrates the usage of the `retriable` macro from the Perseverance library. It shows how to mark a piece of code, like calling `list-s3-files`, as suitable for retrying, specifying which exceptions to catch and attaching a tag for outer retry mechanisms. ```clojure (retriable {:catch [RuntimeException] :tag ::list-files ;; :ex-wrapper #(ex-info "My wrapped exception". {:e %, :foo :bar}) } (list-s3-files)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.