@@ -38,10 +38,41 @@ retry(retrier : Function, opts : Object) => Promise
38
38
- The supplied function receives two parameters
39
39
1. A ` Function ` you can invoke to abort the retrying (bail)
40
40
2. A ` Number ` identifying the attempt. The absolute first attempt (before any retries) is ` 1 ` .
41
- - The ` opts` are same as for ` node - retry ` . Read [its docs](https://github.com/tim-kos/node-retry)
41
+ - The ` opts`
42
42
* ` retries` : The maximum amount of times to retry the operation. Default is ` 10 ` .
43
43
* ` factor` : The exponential factor to use. Default is ` 2 ` .
44
44
* ` minTimeout` : The number of milliseconds before starting the first retry. Default is ` 1000 ` .
45
45
* ` maxTimeout` : The maximum number of milliseconds between two retries. Default is ` Infinity ` .
46
46
* ` randomize` : Randomizes the timeouts by multiplying with a factor between ` 1 ` to ` 2 ` . Default is ` true ` .
47
47
* ` onRetry` : an optional ` Function ` that is invoked after a new retry is performed. It's passed the ` Error ` that triggered it as a parameter.
48
+
49
+ All time values are in milliseconds.
50
+
51
+ ### Retries Explained
52
+
53
+ The formula used to calculate the individual timeouts is:
54
+
55
+ ` ` `
56
+ Math .min (random * minTimeout * Math .pow (factor, attempt), maxTimeout)
57
+ ```
58
+
59
+ Have a look at [ this article] [ article ] for a better explanation of approach.
60
+
61
+ If you want to tune your ` factor ` / ` times ` settings to attempt the last retry
62
+ after a certain amount of time, you can use wolfram alpha. For example in order
63
+ to tune for ` 10 ` attempts in ` 5 minutes ` , you can use this equation:
64
+
65
+ ![ screenshot] ( https://github.com/OlliV/async-retry-ng/raw/master/equation.gif )
66
+
67
+ Explaining the various values from left to right:
68
+
69
+ * ` k = 0 ... 9 ` : The ` retries ` value (10)
70
+ * ` 1000 ` : The ` minTimeout ` value in ms (1000)
71
+ * ` x^k ` : No need to change this, ` x ` will be your resulting factor
72
+ * ` 5 * 60 * 1000 ` : The desired total amount of time for retrying in ms (5 minutes)
73
+
74
+ To make this a little easier for you, use wolfram alpha to do the calculations:
75
+
76
+ < http://www.wolframalpha.com/input/?i=Sum%5B1000*x^k%2C+{k%2C+0%2C+9}%5D+%3D+5+*+60+*+1000 >
77
+
78
+ [ article ] : http://dthain.blogspot.com/2009/02/exponential-backoff-in-distributed.html
0 commit comments