-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathReTriable.java
More file actions
136 lines (119 loc) · 3.57 KB
/
Copy pathReTriable.java
File metadata and controls
136 lines (119 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package jaskell.util;
/**
* TODO
*
* @author mars
* @version 1.0.0
* @since 2020/12/06 13:56
*/
public class ReTriable<T> implements Supplier<T>, Triable<T> {
private final Supplier<T> supplier;
final private int times;
private Integer rest = 0;
private BiFunction<Exception, Integer, Integer> onErr;
public ReTriable(Supplier<T> supplier) {
this.supplier = supplier;
times = 3;
rest = 3;
onErr = (err, rest) -> {
try {
Thread.sleep(1000);
return rest;
} catch (InterruptedException e) {
return rest - 1;
}
};
}
@Override
public T get() throws Exception {
Exception e = null;
while (rest > 0) {
try {
return supplier.get();
} catch (Exception err) {
e = err;
try {
rest = onErr.apply(err, rest);
} catch (Exception exception) {
rest--;
}
rest--;
}
}
if (e != null) {
throw e;
} else {
throw new IllegalStateException(String.format("unknown exception after failed %d times", this.times));
}
}
@Override
public Try<T> collect() {
return Try.tryIt(this);
}
public ReTriable(Supplier<T> supplier, int times) {
this.supplier = supplier;
this.times = times;
this.rest = times;
}
public ReTriable(Supplier<T> supplier, int times, BiFunction<Exception, Integer, Integer> onErr) {
this.supplier = supplier;
this.times = times;
this.rest = times;
this.onErr = onErr;
}
public int getRetries() {
return times;
}
public int getRest() {
return rest;
}
public static <T> T times(int times, Supplier<T> supplier) throws Exception {
Exception e = null;
int rest = times;
while (rest > 0) {
try {
return supplier.get();
} catch (Exception err) {
e = err;
try {
Thread.sleep(1000);
} catch (InterruptedException interruptedException) {
rest--;
}
rest--;
}
}
if (e != null) {
throw e;
} else {
throw new IllegalStateException(String.format("unknown exception after failed %d times", times));
}
}
public static <T> T retries(int times, Supplier<T> supplier, BiFunction<Exception, Integer, Integer> onErr) throws Exception {
Exception e = null;
int rest = times;
while (rest > 0) {
try {
return supplier.get();
} catch (Exception err) {
e = err;
rest = onErr.apply(err, rest);
rest--;
}
}
if (e != null) {
throw new RuntimeException(e);
} else {
throw new IllegalStateException(String.format("unknown exception after failed %d times", times));
}
}
public static <T> ReTriable<T> retry(Supplier<T> supplier) {
return new ReTriable<>(supplier);
}
public static <T> ReTriable<T> retry(Supplier<T> supplier, int times) {
return new ReTriable<>(supplier, times);
}
public static <T> ReTriable<T> retry(Supplier<T> supplier, int times, BiFunction<Exception, Integer, Integer> onErr) {
return new ReTriable<>(supplier, times, onErr);
}
}