Skip to content

Commit 6be60e8

Browse files
hexmindRobWin
authored andcommitted
Issue ReactiveX#531: Added events to TimeLimiter (ReactiveX#583)
1 parent 93b0819 commit 6be60e8

13 files changed

+487
-33
lines changed

resilience4j-timelimiter/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dependencies {
2-
2+
compile project(':resilience4j-core')
33
}
44
ext.moduleName='io.github.resilience4j.timelimiter'

resilience4j-timelimiter/src/main/java/io/github/resilience4j/timelimiter/TimeLimiter.java

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package io.github.resilience4j.timelimiter;
22

3+
import io.github.resilience4j.core.EventConsumer;
4+
import io.github.resilience4j.timelimiter.event.TimeLimiterEvent;
5+
import io.github.resilience4j.timelimiter.event.TimeLimiterOnFailureEvent;
6+
import io.github.resilience4j.timelimiter.event.TimeLimiterOnSuccessEvent;
7+
import io.github.resilience4j.timelimiter.event.TimeLimiterOnTimeoutEvent;
38
import io.github.resilience4j.timelimiter.internal.TimeLimiterImpl;
49

510
import java.time.Duration;
6-
import java.util.concurrent.*;
11+
import java.util.concurrent.Callable;
12+
import java.util.concurrent.Future;
713
import java.util.function.Supplier;
814

915
/**
1016
* A TimeLimiter decorator stops execution after a configurable duration.
1117
*/
1218
public interface TimeLimiter {
1319

20+
String DEFAULT_NAME = "UNDEFINED";
21+
1422
/**
1523
* Creates a TimeLimiter decorator with a default TimeLimiterConfig configuration.
1624
*
1725
* @return The {@link TimeLimiter}
1826
*/
1927
static TimeLimiter ofDefaults() {
20-
return new TimeLimiterImpl(TimeLimiterConfig.ofDefaults());
28+
return new TimeLimiterImpl(DEFAULT_NAME, TimeLimiterConfig.ofDefaults());
2129
}
2230

2331
/**
@@ -27,7 +35,18 @@ static TimeLimiter ofDefaults() {
2735
* @return The {@link TimeLimiter}
2836
*/
2937
static TimeLimiter of(TimeLimiterConfig timeLimiterConfig) {
30-
return new TimeLimiterImpl(timeLimiterConfig);
38+
return of(DEFAULT_NAME, timeLimiterConfig);
39+
}
40+
41+
/**
42+
* Creates a TimeLimiter decorator with a TimeLimiterConfig configuration.
43+
*
44+
* @param name the name of the TimeLimiter
45+
* @param timeLimiterConfig the TimeLimiterConfig
46+
* @return The {@link TimeLimiter}
47+
*/
48+
static TimeLimiter of(String name, TimeLimiterConfig timeLimiterConfig) {
49+
return new TimeLimiterImpl(name, timeLimiterConfig);
3150
}
3251

3352
/**
@@ -40,8 +59,7 @@ static TimeLimiter of(Duration timeoutDuration) {
4059
TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
4160
.timeoutDuration(timeoutDuration)
4261
.build();
43-
44-
return new TimeLimiterImpl(timeLimiterConfig);
62+
return new TimeLimiterImpl(DEFAULT_NAME, timeLimiterConfig);
4563
}
4664

4765
/**
@@ -54,26 +72,7 @@ static TimeLimiter of(Duration timeoutDuration) {
5472
* @return a future supplier which is restricted by a {@link TimeLimiter}.
5573
*/
5674
static <T, F extends Future<T>> Callable<T> decorateFutureSupplier(TimeLimiter timeLimiter, Supplier<F> futureSupplier) {
57-
return () -> {
58-
Future<T> future = futureSupplier.get();
59-
try {
60-
return future.get(timeLimiter.getTimeLimiterConfig().getTimeoutDuration().toMillis(), TimeUnit.MILLISECONDS);
61-
} catch (TimeoutException e) {
62-
if(timeLimiter.getTimeLimiterConfig().shouldCancelRunningFuture()){
63-
future.cancel(true);
64-
}
65-
throw e;
66-
} catch (ExecutionException e){
67-
Throwable t = e.getCause();
68-
if (t == null){
69-
throw e;
70-
}
71-
if (t instanceof Error){
72-
throw (Error) t;
73-
}
74-
throw (Exception) t;
75-
}
76-
};
75+
return timeLimiter.decorateFutureSupplier(futureSupplier);
7776
}
7877

7978
/**
@@ -104,7 +103,24 @@ default <T, F extends Future<T>> T executeFutureSupplier(Supplier<F> futureSuppl
104103
* @param <F> the future type supplied
105104
* @return a future supplier which is restricted by a {@link TimeLimiter}.
106105
*/
107-
default <T, F extends Future<T>> Callable<T> decorateFutureSupplier(Supplier<F> futureSupplier) {
108-
return decorateFutureSupplier(this, futureSupplier);
106+
<T, F extends Future<T>> Callable<T> decorateFutureSupplier(Supplier<F> futureSupplier);
107+
108+
EventPublisher getEventPublisher();
109+
110+
void onSuccess();
111+
112+
void onError(Exception exception);
113+
114+
/**
115+
* An EventPublisher which can be used to register event consumers.
116+
*/
117+
interface EventPublisher extends io.github.resilience4j.core.EventPublisher<TimeLimiterEvent> {
118+
119+
EventPublisher onSuccess(EventConsumer<TimeLimiterOnSuccessEvent> eventConsumer);
120+
121+
EventPublisher onFailure(EventConsumer<TimeLimiterOnFailureEvent> eventConsumer);
122+
123+
EventPublisher onTimeout(EventConsumer<TimeLimiterOnTimeoutEvent> eventConsumer);
124+
109125
}
110126
}

resilience4j-timelimiter/src/main/java/io/github/resilience4j/timelimiter/TimeLimiterConfig.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class TimeLimiterConfig {
88
private static final String TIMEOUT_DURATION_MUST_NOT_BE_NULL = "TimeoutDuration must not be null";
99

10-
private Duration timeoutDuration = Duration.ofSeconds(1);
10+
private Duration timeoutDuration = Duration.ofSeconds(1);
1111
private boolean cancelRunningFuture = true;
1212

1313
private TimeLimiterConfig() {
@@ -27,7 +27,7 @@ public static Builder custom() {
2727
*
2828
* @return a default TimeLimiter configuration.
2929
*/
30-
public static TimeLimiterConfig ofDefaults(){
30+
public static TimeLimiterConfig ofDefaults() {
3131
return new Builder().build();
3232
}
3333

@@ -39,7 +39,8 @@ public boolean shouldCancelRunningFuture() {
3939
return cancelRunningFuture;
4040
}
4141

42-
@Override public String toString() {
42+
@Override
43+
public String toString() {
4344
return "TimeLimiterConfig{" +
4445
"timeoutDuration=" + timeoutDuration +
4546
"cancelRunningFuture=" + cancelRunningFuture +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
*
3+
* Copyright 2019 authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package io.github.resilience4j.timelimiter.event;
20+
21+
import java.time.ZonedDateTime;
22+
23+
public abstract class AbstractTimeLimiterEvent implements TimeLimiterEvent {
24+
25+
private final String timeLimiterName;
26+
private Type eventType;
27+
private final ZonedDateTime creationTime;
28+
29+
AbstractTimeLimiterEvent(String timeLimiterName, Type eventType) {
30+
this.timeLimiterName = timeLimiterName;
31+
this.eventType = eventType;
32+
this.creationTime = ZonedDateTime.now();
33+
}
34+
35+
@Override
36+
public String getTimeLimiterName() {
37+
return timeLimiterName;
38+
}
39+
40+
@Override
41+
public ZonedDateTime getCreationTime() {
42+
return creationTime;
43+
}
44+
45+
@Override
46+
public Type getEventType() {
47+
return eventType;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "TimeLimiterEvent{" +
53+
"type=" + getEventType() +
54+
", timeLimiterName='" + getTimeLimiterName() + '\'' +
55+
", creationTime=" + getCreationTime() +
56+
'}';
57+
}
58+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
*
3+
* Copyright 2019 authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package io.github.resilience4j.timelimiter.event;
20+
21+
import java.time.ZonedDateTime;
22+
23+
/**
24+
* An event which is created by a {@link io.github.resilience4j.timelimiter.TimeLimiter}.
25+
*/
26+
public interface TimeLimiterEvent {
27+
28+
static TimeLimiterEvent of(String name, Type eventType) {
29+
switch (eventType) {
30+
case SUCCESS:
31+
return new TimeLimiterOnSuccessEvent(name);
32+
case TIMEOUT:
33+
return new TimeLimiterOnTimeoutEvent(name);
34+
default:
35+
return new TimeLimiterOnFailureEvent(name);
36+
}
37+
}
38+
39+
String getTimeLimiterName();
40+
41+
Type getEventType();
42+
43+
ZonedDateTime getCreationTime();
44+
45+
enum Type {
46+
SUCCESS,
47+
TIMEOUT,
48+
FAILURE
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
*
3+
* Copyright 2019 authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package io.github.resilience4j.timelimiter.event;
20+
21+
public class TimeLimiterOnFailureEvent extends AbstractTimeLimiterEvent {
22+
23+
public TimeLimiterOnFailureEvent(String timeLimiterName) {
24+
super(timeLimiterName, Type.FAILURE);
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
*
3+
* Copyright 2019 authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package io.github.resilience4j.timelimiter.event;
20+
21+
public class TimeLimiterOnSuccessEvent extends AbstractTimeLimiterEvent {
22+
23+
public TimeLimiterOnSuccessEvent(String timeLimiterName) {
24+
super(timeLimiterName, Type.SUCCESS);
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
*
3+
* Copyright 2019 authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package io.github.resilience4j.timelimiter.event;
20+
21+
public class TimeLimiterOnTimeoutEvent extends AbstractTimeLimiterEvent {
22+
23+
public TimeLimiterOnTimeoutEvent(String timeLimiterName) {
24+
super(timeLimiterName, Type.TIMEOUT);
25+
}
26+
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
*
3+
* Copyright 2019 authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
@NonNullApi
20+
@NonNullFields
21+
package io.github.resilience4j.timelimiter.event;
22+
23+
import io.github.resilience4j.core.lang.NonNullApi;
24+
import io.github.resilience4j.core.lang.NonNullFields;

0 commit comments

Comments
 (0)