]> gitweb.factorcode.org Git - factor.git/commitdiff
backoff: Add an exponential backoff combinator
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 26 May 2023 03:34:26 +0000 (22:34 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 6 Jun 2023 13:36:50 +0000 (08:36 -0500)
extra/backoff/authors.txt [new file with mode: 0644]
extra/backoff/backoff.factor [new file with mode: 0644]

diff --git a/extra/backoff/authors.txt b/extra/backoff/authors.txt
new file mode 100644 (file)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/extra/backoff/backoff.factor b/extra/backoff/backoff.factor
new file mode 100644 (file)
index 0000000..1d4112c
--- /dev/null
@@ -0,0 +1,22 @@
+! Copyright (C) 2023 Doug Coleman.
+! See https://factorcode.org/license.txt for BSD license.
+USING: calendar combinators.short-circuit kernel math math.order
+shuffle threads ;
+IN: backoff
+
+: exponential-backoff-from ( quot: ( -- success? ) max-seconds max-count/f n -- )
+    2dup { [ drop ] [ <= ] } 2&& [
+        4drop
+    ] [
+        [ nip 2^ min seconds sleep call ] 4keep 5roll [
+            4drop
+        ] [
+            1 + exponential-backoff-from
+        ] if
+    ] if ; inline recursive
+
+: exponential-backoff-count ( quot: ( -- success? ) max-seconds max-count -- )
+    0 exponential-backoff-from ; inline
+
+: exponential-backoff ( quot: ( -- success? ) max-seconds -- )
+    f exponential-backoff-count ; inline