Skip to content

Commit 8549060

Browse files
committed
Add start and stop for RSocket client and tester
Closes gh-378
1 parent 5b59e51 commit 8549060

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

spring-graphql-docs/src/docs/asciidoc/client.adoc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,17 @@ to execute GraphQL requests over RSocket requests.
178178
----
179179

180180
In contrast to `HttpGraphQlClient`, the `RSocketGraphQlClient` is connection oriented,
181-
which means it needs to establish a connection before making any requests. As you begin
182-
to make requests, the connection is established transparently.
181+
which means it needs to establish a session before making any requests. As you begin
182+
to make requests, the session is established transparently. Alternatively, use the
183+
client's `start()` method to establish the session explicitly before any requests.
183184

184-
`RSocketGraphQlClient` is also multiplexed. It maintains a single, shared connection for
185-
all requests. If the connection is lost, it is re-established on the next request. You
186-
can use the `dispose()` method on the underlying `RSocketRequester` to close the
187-
connection explicitly.
185+
`RSocketGraphQlClient` is also multiplexed. It maintains a single, shared session for
186+
all requests. If the session is lost, it is re-established on the next request or if
187+
`start()` is called again. You can also use the client's `stop()` method which cancels
188+
in-progress requests, closes the session, and rejects new requests.
188189

189190
TIP: Use a single `RSocketGraphQlClient` instance for each server in order to have a
190-
single, shared connection for all requests to that server. Each client instance
191+
single, shared session for all requests to that server. Each client instance
191192
establishes its own connection and that is typically not the intent for a single server.
192193

193194
Once `RSocketGraphQlClient` is created, you can begin to

spring-graphql-docs/src/docs/asciidoc/testing.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ requests over RSocket:
202202
----
203203

204204
`RSocketGraphQlTester` is connection oriented and multiplexed. Each instance establishes
205-
its own single, shared connection for all requests. Typically, you'll want to use a single
206-
instance only per server. You can use the `dispose()` method on the underlying
207-
`RSocketRequester` to close the connection explicitly.
205+
its own single, shared session for all requests. Typically, you'll want to use a single
206+
instance only per server. You can use the `stop()` method on the tester to close the
207+
session explicitly.
208208

209209
Once `RSocketGraphQlTester` is created, you can begin to
210210
<<testing-requests, execute requests>> using the same API, independent of the underlying

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultRSocketGraphQlTesterBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.function.Consumer;
2222

2323
import io.rsocket.transport.ClientTransport;
24+
import reactor.core.publisher.Mono;
2425

2526
import org.springframework.core.codec.Decoder;
2627
import org.springframework.core.codec.Encoder;
@@ -141,6 +142,16 @@ private static class DefaultRSocketGraphQlTester extends AbstractDelegatingGraph
141142
this.builderInitializer = builderInitializer;
142143
}
143144

145+
@Override
146+
public Mono<Void> start() {
147+
return this.rsocketGraphQlClient.start();
148+
}
149+
150+
@Override
151+
public Mono<Void> stop() {
152+
return this.rsocketGraphQlClient.stop();
153+
}
154+
144155
@Override
145156
public RSocketGraphQlTester.Builder<?> mutate() {
146157
DefaultRSocketGraphQlTesterBuilder builder = new DefaultRSocketGraphQlTesterBuilder(this.rsocketGraphQlClient);

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/RSocketGraphQlTester.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.util.function.Consumer;
2222

2323
import io.rsocket.transport.ClientTransport;
24+
import reactor.core.publisher.Mono;
2425

26+
import org.springframework.graphql.client.RSocketGraphQlClient;
2527
import org.springframework.messaging.rsocket.RSocketRequester;
2628
import org.springframework.util.MimeType;
2729

@@ -33,6 +35,19 @@
3335
*/
3436
public interface RSocketGraphQlTester extends GraphQlTester {
3537

38+
/**
39+
* Start the RSocket session.
40+
* @return {@code Mono} that completes when the underlying session is started
41+
*/
42+
Mono<Void> start();
43+
44+
/**
45+
* Stop the RSocket session.
46+
* @return {@code Mono} that completes when the underlying session is closed
47+
* <p>Note that currently this method not differed and does not wait,
48+
* see {@link RSocketGraphQlClient#stop()}
49+
*/
50+
Mono<Void> stop();
3651

3752
@Override
3853
RSocketGraphQlTester.Builder<?> mutate();

spring-graphql/src/main/java/org/springframework/graphql/client/DefaultRSocketGraphQlClientBuilder.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.rsocket.transport.ClientTransport;
2323
import io.rsocket.transport.netty.client.TcpClientTransport;
2424
import io.rsocket.transport.netty.client.WebsocketClientTransport;
25+
import reactor.core.publisher.Mono;
2526

2627
import org.springframework.lang.Nullable;
2728
import org.springframework.messaging.rsocket.RSocketRequester;
@@ -124,7 +125,7 @@ public RSocketGraphQlClient build() {
124125
RSocketGraphQlTransport graphQlTransport = new RSocketGraphQlTransport(this.route, requester, getJsonDecoder());
125126

126127
return new DefaultRSocketGraphQlClient(
127-
super.buildGraphQlClient(graphQlTransport),
128+
super.buildGraphQlClient(graphQlTransport), requester,
128129
this.requesterBuilder, this.clientTransport, this.route, getBuilderInitializer());
129130
}
130131

@@ -134,6 +135,8 @@ public RSocketGraphQlClient build() {
134135
*/
135136
private static class DefaultRSocketGraphQlClient extends AbstractDelegatingGraphQlClient implements RSocketGraphQlClient {
136137

138+
private final RSocketRequester requester;
139+
137140
private final RSocketRequester.Builder requesterBuilder;
138141

139142
private final ClientTransport clientTransport;
@@ -143,17 +146,30 @@ private static class DefaultRSocketGraphQlClient extends AbstractDelegatingGraph
143146
private final Consumer<AbstractGraphQlClientBuilder<?>> builderInitializer;
144147

145148
DefaultRSocketGraphQlClient(
146-
GraphQlClient graphQlClient, RSocketRequester.Builder requesterBuilder,
149+
GraphQlClient graphQlClient, RSocketRequester requester, RSocketRequester.Builder requesterBuilder,
147150
ClientTransport clientTransport, String route, Consumer<AbstractGraphQlClientBuilder<?>> builderInitializer) {
148151

149152
super(graphQlClient);
150153

154+
this.requester = requester;
151155
this.requesterBuilder = requesterBuilder;
152156
this.clientTransport = clientTransport;
153157
this.route = route;
154158
this.builderInitializer = builderInitializer;
155159
}
156160

161+
@Override
162+
public Mono<Void> start() {
163+
return this.requester.rsocketClient().source().then();
164+
}
165+
166+
@Override
167+
public Mono<Void> stop() {
168+
// Currently, no option to close and wait (see Javadoc)
169+
this.requester.dispose();
170+
return Mono.empty();
171+
}
172+
157173
@Override
158174
public RSocketGraphQlClient.Builder<?> mutate() {
159175
DefaultRSocketGraphQlClientBuilder builder = new DefaultRSocketGraphQlClientBuilder(this.requesterBuilder);

spring-graphql/src/main/java/org/springframework/graphql/client/RSocketGraphQlClient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.net.URI;
2020
import java.util.function.Consumer;
2121

22+
import io.rsocket.core.RSocketClient;
2223
import io.rsocket.transport.ClientTransport;
24+
import reactor.core.publisher.Mono;
2325

2426
import org.springframework.messaging.rsocket.RSocketRequester;
2527
import org.springframework.util.MimeType;
@@ -33,6 +35,21 @@
3335
*/
3436
public interface RSocketGraphQlClient extends GraphQlClient {
3537

38+
/**
39+
* Start the RSocket session.
40+
* @return {@code Mono} that completes when the underlying session is started
41+
*/
42+
Mono<Void> start();
43+
44+
/**
45+
* Stop the RSocket session.
46+
* @return {@code Mono} that completes when the underlying session is stopped.
47+
* <p>Note that currently this method calls {@link RSocketClient#dispose()}
48+
* which is not differed and does not wait, i.e. it triggers stopping
49+
* immediately and returns immediately.
50+
* See <a href="https://github.com/rsocket/rsocket-java/issues/1048">rsocket-java#1048</a>
51+
*/
52+
Mono<Void> stop();
3653

3754
@Override
3855
Builder<?> mutate();

0 commit comments

Comments
 (0)