Skip to content

Commit 072fe55

Browse files
committed
Added .mapConnectionAborted at RSocketClientChannel
1 parent e7d0b09 commit 072fe55

File tree

5 files changed

+68
-26
lines changed

5 files changed

+68
-26
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.scalecube.services.exceptions;
2+
3+
import java.util.regex.Pattern;
4+
5+
public class ConnectionClosedException extends InternalServiceException {
6+
7+
private static final Pattern GENERIC_CONNECTION_CLOSED =
8+
Pattern.compile(
9+
"^.*(?:connection.*(?:reset|closed|abort|broken)|broken.*pipe).*$",
10+
Pattern.CASE_INSENSITIVE);
11+
12+
public ConnectionClosedException() {
13+
super("Connection closed");
14+
}
15+
16+
public ConnectionClosedException(Throwable cause) {
17+
super(cause);
18+
}
19+
20+
public ConnectionClosedException(String message) {
21+
super(message);
22+
}
23+
24+
/**
25+
* Returns {@code true} if connection has been aborted on a tcp level by verifying error message
26+
* and matching it against predefined pattern.
27+
*
28+
* @param th error
29+
* @return {@code true} if connection has been aborted on a tcp level
30+
*/
31+
public static boolean isConnectionClosed(Throwable th) {
32+
if (th instanceof ConnectionClosedException) {
33+
return true;
34+
}
35+
36+
final String message = th != null ? th.getMessage() : null;
37+
38+
return message != null && GENERIC_CONNECTION_CLOSED.matcher(message).matches();
39+
}
40+
}

services-api/src/main/java/io/scalecube/services/exceptions/InternalServiceException.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ public InternalServiceException(int errorCode, String message) {
1111
public InternalServiceException(Throwable cause) {
1212
super(ERROR_TYPE, cause);
1313
}
14+
15+
public InternalServiceException(String message) {
16+
super(ERROR_TYPE, message);
17+
}
18+
19+
public InternalServiceException(String message, Throwable cause) {
20+
super(ERROR_TYPE, message, cause);
21+
}
1422
}
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
package io.scalecube.services.exceptions;
22

3-
public class MessageCodecException extends RuntimeException {
3+
public class MessageCodecException extends InternalServiceException {
44

55
public MessageCodecException(String message, Throwable cause) {
66
super(message, cause);
77
}
8-
9-
@Override
10-
public synchronized Throwable fillInStackTrace() {
11-
return this;
12-
}
13-
14-
@Override
15-
public String toString() {
16-
return getClass().getSimpleName() + "{errorMessage=" + getMessage() + '}';
17-
}
188
}

services-api/src/main/java/io/scalecube/services/exceptions/ServiceException.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.scalecube.services.exceptions;
22

3+
import java.util.StringJoiner;
4+
35
public abstract class ServiceException extends RuntimeException {
46

57
private final int errorCode;
@@ -14,6 +16,11 @@ public ServiceException(int errorCode, Throwable cause) {
1416
this.errorCode = errorCode;
1517
}
1618

19+
public ServiceException(int errorCode, String message, Throwable cause) {
20+
super(message, cause);
21+
this.errorCode = errorCode;
22+
}
23+
1724
@Override
1825
public synchronized Throwable fillInStackTrace() {
1926
return this;
@@ -25,11 +32,9 @@ public int errorCode() {
2532

2633
@Override
2734
public String toString() {
28-
return getClass().getSimpleName()
29-
+ "{errorCode="
30-
+ errorCode
31-
+ ", errorMessage="
32-
+ getMessage()
33-
+ '}';
35+
return new StringJoiner(", ", getClass().getSimpleName() + "[", "]")
36+
.add("errorCode=" + errorCode)
37+
.add("errorMessage='" + getMessage() + "'")
38+
.toString();
3439
}
3540
}

services-transport-parent/services-transport-rsocket/src/main/java/io/scalecube/services/transport/rsocket/RSocketClientChannel.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.rsocket.RSocket;
55
import io.rsocket.util.ByteBufPayload;
66
import io.scalecube.services.api.ServiceMessage;
7+
import io.scalecube.services.exceptions.ConnectionClosedException;
78
import io.scalecube.services.transport.api.ClientChannel;
89
import io.scalecube.services.transport.api.ServiceMessageCodec;
910
import java.lang.reflect.Type;
@@ -32,7 +33,7 @@ public Mono<ServiceMessage> requestResponse(ServiceMessage message, Type respons
3233
.flatMap(rsocket -> rsocket.requestResponse(toPayload(message)))
3334
.map(this::toMessage)
3435
.map(msg -> ServiceMessageCodec.decodeData(msg, responseType))
35-
.doOnError(RSocketClientChannel::handleConnectionReset);
36+
.onErrorMap(RSocketClientChannel::mapConnectionAborted);
3637
}
3738

3839
@Override
@@ -41,7 +42,7 @@ public Flux<ServiceMessage> requestStream(ServiceMessage message, Type responseT
4142
.flatMapMany(rsocket -> rsocket.requestStream(toPayload(message)))
4243
.map(this::toMessage)
4344
.map(msg -> ServiceMessageCodec.decodeData(msg, responseType))
44-
.doOnError(RSocketClientChannel::handleConnectionReset);
45+
.onErrorMap(RSocketClientChannel::mapConnectionAborted);
4546
}
4647

4748
@Override
@@ -51,7 +52,7 @@ public Flux<ServiceMessage> requestChannel(
5152
.flatMapMany(rsocket -> rsocket.requestChannel(Flux.from(publisher).map(this::toPayload)))
5253
.map(this::toMessage)
5354
.map(msg -> ServiceMessageCodec.decodeData(msg, responseType))
54-
.doOnError(RSocketClientChannel::handleConnectionReset);
55+
.onErrorMap(RSocketClientChannel::mapConnectionAborted);
5556
}
5657

5758
private Payload toPayload(ServiceMessage request) {
@@ -66,11 +67,9 @@ private ServiceMessage toMessage(Payload payload) {
6667
}
6768
}
6869

69-
private static void handleConnectionReset(Throwable throwable) {
70-
if (AbortedException.isConnectionReset(throwable)) {
71-
if (LOGGER.isDebugEnabled()) {
72-
LOGGER.debug("[requestResponse] Connection has been reset");
73-
}
74-
}
70+
private static Throwable mapConnectionAborted(Throwable t) {
71+
return AbortedException.isConnectionReset(t) || ConnectionClosedException.isConnectionClosed(t)
72+
? new ConnectionClosedException(t)
73+
: t;
7574
}
7675
}

0 commit comments

Comments
 (0)