Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 7b566f1

Browse files
authored
Merge pull request #143 from scalecube/refactor-websocket-receive-part
Refactored websocket sessions
2 parents 2bf2b9b + ebd2ef9 commit 7b566f1

File tree

5 files changed

+34
-33
lines changed

5 files changed

+34
-33
lines changed

services-gateway-client-transport/src/main/java/io/scalecube/services/gateway/transport/websocket/WebsocketGatewayClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ public GatewayClientCodec<ByteBuf> getCodec() {
137137
return codec;
138138
}
139139

140-
private Mono<WebsocketSession> getOrConnect() {
140+
private Mono<WebsocketGatewayClientSession> getOrConnect() {
141141
// noinspection unchecked
142142
return Mono.defer(() -> websocketMonoUpdater.updateAndGet(this, this::getOrConnect0));
143143
}
144144

145-
private Mono<WebsocketSession> getOrConnect0(Mono<WebsocketSession> prev) {
145+
private Mono<WebsocketGatewayClientSession> getOrConnect0(
146+
Mono<WebsocketGatewayClientSession> prev) {
146147
if (prev != null) {
147148
return prev;
148149
}
@@ -162,7 +163,8 @@ private Mono<WebsocketSession> getOrConnect0(Mono<WebsocketSession> prev) {
162163
: connection)
163164
.map(
164165
connection -> {
165-
WebsocketSession session = new WebsocketSession(codec, connection);
166+
WebsocketGatewayClientSession session =
167+
new WebsocketGatewayClientSession(codec, connection);
166168
LOGGER.info("Created {} on {}:{}", session, settings.host(), settings.port());
167169
// setup shutdown hook
168170
session
@@ -209,7 +211,7 @@ private void onReadIdle(Connection connection) {
209211
.subscribe(null, ex -> LOGGER.warn("Can't send keepalive on readIdle: " + ex));
210212
}
211213

212-
private void handleCancel(long sid, WebsocketSession session) {
214+
private void handleCancel(long sid, WebsocketGatewayClientSession session) {
213215
ByteBuf byteBuf =
214216
codec.encode(
215217
ServiceMessage.builder()

services-gateway-client-transport/src/main/java/io/scalecube/services/gateway/transport/websocket/WebsocketSession.java renamed to services-gateway-client-transport/src/main/java/io/scalecube/services/gateway/transport/websocket/WebsocketGatewayClientSession.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.scalecube.services.gateway.transport.websocket;
22

33
import io.netty.buffer.ByteBuf;
4-
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
5-
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
64
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
75
import io.scalecube.services.api.ErrorData;
86
import io.scalecube.services.api.ServiceMessage;
@@ -25,9 +23,9 @@
2523
import reactor.netty.http.websocket.WebsocketInbound;
2624
import reactor.netty.http.websocket.WebsocketOutbound;
2725

28-
public final class WebsocketSession {
26+
public final class WebsocketGatewayClientSession {
2927

30-
private static final Logger LOGGER = LoggerFactory.getLogger(WebsocketSession.class);
28+
private static final Logger LOGGER = LoggerFactory.getLogger(WebsocketGatewayClientSession.class);
3129

3230
private static final String STREAM_ID = "sid";
3331
private static final String SIGNAL = "sig";
@@ -40,19 +38,21 @@ public final class WebsocketSession {
4038
private final Map<Long, Processor<ServiceMessage, ServiceMessage>> inboundProcessors =
4139
new NonBlockingHashMapLong<>(1024);
4240

43-
WebsocketSession(GatewayClientCodec<ByteBuf> codec, Connection connection) {
41+
WebsocketGatewayClientSession(GatewayClientCodec<ByteBuf> codec, Connection connection) {
4442
this.id = Integer.toHexString(System.identityHashCode(this));
4543
this.codec = codec;
4644
this.connection = connection;
4745

4846
WebsocketInbound inbound = (WebsocketInbound) connection.inbound();
4947
inbound
50-
.aggregateFrames()
51-
.receiveFrames()
52-
.filter(f -> !(f instanceof PongWebSocketFrame || f instanceof PingWebSocketFrame))
53-
.map(f -> f.retain().content())
48+
.receive()
49+
.retain()
5450
.subscribe(
5551
byteBuf -> {
52+
if (!byteBuf.isReadable()) {
53+
ReferenceCountUtil.safestRelease(byteBuf);
54+
return;
55+
}
5656
// decode message
5757
ServiceMessage message;
5858
try {
@@ -186,7 +186,7 @@ private void handleResponse(
186186

187187
@Override
188188
public String toString() {
189-
return new StringJoiner(", ", WebsocketSession.class.getSimpleName() + "[", "]")
189+
return new StringJoiner(", ", WebsocketGatewayClientSession.class.getSimpleName() + "[", "]")
190190
.add("id=" + id)
191191
.toString();
192192
}

services-gateway-netty/src/main/java/io/scalecube/services/gateway/ws/WebsocketGatewayAcceptor.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,15 @@ private Mono<Void> onConnect(WebsocketGatewaySession session) {
115115
.receive()
116116
.doOnError(th -> gatewayHandler.onSessionError(session, th))
117117
.subscribe(
118-
byteBuf ->
119-
Mono.deferWithContext(context -> onRequest(session, byteBuf, context))
120-
.subscriberContext(
121-
context -> gatewayHandler.onRequest(session, byteBuf, context))
122-
.subscribe());
118+
byteBuf -> {
119+
if (!byteBuf.isReadable()) {
120+
ReferenceCountUtil.safestRelease(byteBuf);
121+
return;
122+
}
123+
Mono.deferWithContext(context -> onRequest(session, byteBuf, context))
124+
.subscriberContext(context -> gatewayHandler.onRequest(session, byteBuf, context))
125+
.subscribe();
126+
});
123127

124128
return session.onClose(() -> gatewayHandler.onSessionClose(session));
125129
}

services-gateway-netty/src/main/java/io/scalecube/services/gateway/ws/WebsocketGatewaySession.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.scalecube.services.gateway.ws;
22

33
import io.netty.buffer.ByteBuf;
4-
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
5-
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
64
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
75
import io.scalecube.services.api.ServiceMessage;
86
import io.scalecube.services.gateway.GatewaySession;
@@ -77,11 +75,7 @@ public Map<String, String> headers() {
7775
* @return flux websocket {@link ByteBuf}
7876
*/
7977
public Flux<ByteBuf> receive() {
80-
return inbound
81-
.aggregateFrames()
82-
.receiveFrames()
83-
.filter(f -> !(f instanceof PongWebSocketFrame || f instanceof PingWebSocketFrame))
84-
.map(f -> f.retain().content());
78+
return inbound.receive().retain();
8579
}
8680

8781
/**

services-gateway-tests/src/test/java/io/scalecube/services/gateway/websocket/WebsocketClientConnectionTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import io.scalecube.services.gateway.transport.GatewayClientTransports;
2424
import io.scalecube.services.gateway.transport.StaticAddressRouter;
2525
import io.scalecube.services.gateway.transport.websocket.WebsocketGatewayClient;
26-
import io.scalecube.services.gateway.transport.websocket.WebsocketSession;
26+
import io.scalecube.services.gateway.transport.websocket.WebsocketGatewayClientSession;
2727
import io.scalecube.services.gateway.ws.WebsocketGateway;
2828
import io.scalecube.services.transport.rsocket.RSocketServiceTransport;
2929
import java.io.IOException;
@@ -59,7 +59,6 @@ class WebsocketClientConnectionTest extends BaseTest {
5959
@BeforeEach
6060
void beforEach() {
6161
this.sessionEventHandler = new TestGatewaySessionHandler();
62-
//noinspection unchecked
6362
gateway =
6463
Microservices.builder()
6564
.discovery(ScalecubeServiceDiscovery::new)
@@ -179,11 +178,12 @@ void testKeepalive()
179178
.build(),
180179
CLIENT_CODEC);
181180

182-
Method getorConn = WebsocketGatewayClient.class.getDeclaredMethod("getOrConnect");
183-
getorConn.setAccessible(true);
181+
Method getOrConnect = WebsocketGatewayClient.class.getDeclaredMethod("getOrConnect");
182+
getOrConnect.setAccessible(true);
184183
//noinspection unchecked
185-
WebsocketSession session = ((Mono<WebsocketSession>) getorConn.invoke(client)).block(TIMEOUT);
186-
Field connectionField = WebsocketSession.class.getDeclaredField("connection");
184+
WebsocketGatewayClientSession session =
185+
((Mono<WebsocketGatewayClientSession>) getOrConnect.invoke(client)).block(TIMEOUT);
186+
Field connectionField = WebsocketGatewayClientSession.class.getDeclaredField("connection");
187187
connectionField.setAccessible(true);
188188
Connection connection = (Connection) connectionField.get(session);
189189
connection.addHandler(
@@ -192,8 +192,9 @@ void testKeepalive()
192192
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
193193
if (msg instanceof PongWebSocketFrame) {
194194
keepaliveLatch.countDown();
195+
} else {
196+
super.channelRead(ctx, msg);
195197
}
196-
super.channelRead(ctx, msg);
197198
}
198199
});
199200

0 commit comments

Comments
 (0)