Skip to content

Commit 9b7b250

Browse files
committed
Pass in keep alive duration
1 parent 5190917 commit 9b7b250

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/main/java/graphql/servlet/ApolloSubscriptionConnectionListener.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package graphql.servlet;
22

3+
import java.time.Duration;
34
import java.util.Optional;
45

56
public interface ApolloSubscriptionConnectionListener extends SubscriptionConnectionListener {
67

8+
long KEEP_ALIVE_INTERVAL_SEC = 15;
9+
710
String CONNECT_RESULT_KEY = "CONNECT_RESULT";
811

912
default boolean isKeepAliveEnabled() {
@@ -14,4 +17,8 @@ default Optional<Object> onConnect(Object payload) throws SubscriptionException
1417
return Optional.empty();
1518
}
1619

20+
default Duration getKeepAliveInterval() {
21+
return Duration.ofSeconds(KEEP_ALIVE_INTERVAL_SEC);
22+
}
23+
1724
}

src/main/java/graphql/servlet/internal/ApolloSubscriptionKeepAliveRunner.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.slf4j.LoggerFactory;
55

66
import javax.websocket.Session;
7+
import java.time.Duration;
78
import java.util.Map;
9+
import java.util.Objects;
810
import java.util.concurrent.ConcurrentHashMap;
911
import java.util.concurrent.Executors;
1012
import java.util.concurrent.Future;
@@ -16,19 +18,20 @@ class ApolloSubscriptionKeepAliveRunner {
1618

1719
private static final Logger LOG = LoggerFactory.getLogger(ApolloSubscriptionKeepAliveRunner.class);
1820

19-
private static final long KEEP_ALIVE_INTERVAL_SEC = 15;
2021
private static final int EXECUTOR_POOL_SIZE = 10;
2122

2223
private final ScheduledExecutorService executor;
2324
private final SubscriptionSender sender;
2425
private final ApolloSubscriptionProtocolHandler.OperationMessage keepAliveMessage;
2526
private final Map<Session, Future<?>> futures;
27+
private final long keepAliveIntervalSeconds;
2628

27-
ApolloSubscriptionKeepAliveRunner(SubscriptionSender sender) {
28-
this.sender = sender;
29+
ApolloSubscriptionKeepAliveRunner(SubscriptionSender sender, Duration keepAliveInterval) {
30+
this.sender = Objects.requireNonNull(sender);
2931
this.keepAliveMessage = ApolloSubscriptionProtocolHandler.OperationMessage.newKeepAliveMessage();
3032
this.executor = Executors.newScheduledThreadPool(EXECUTOR_POOL_SIZE);
3133
this.futures = new ConcurrentHashMap<>();
34+
this.keepAliveIntervalSeconds = keepAliveInterval.getSeconds();
3235
}
3336

3437
void keepAlive(Session session) {
@@ -48,7 +51,7 @@ private ScheduledFuture<?> startKeepAlive(Session session) {
4851
LOG.error("Cannot send keep alive message. Aborting keep alive", t);
4952
abort(session);
5053
}
51-
}, 0, KEEP_ALIVE_INTERVAL_SEC, TimeUnit.SECONDS);
54+
}, 0, keepAliveIntervalSeconds, TimeUnit.SECONDS);
5255
}
5356

5457
void abort(Session session) {
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
package graphql.servlet.internal;
22

3+
import graphql.servlet.ApolloSubscriptionConnectionListener;
4+
35
/**
46
* @author Andrew Potter
57
*/
68
public class ApolloSubscriptionProtocolFactory extends SubscriptionProtocolFactory {
79
private final SubscriptionHandlerInput subscriptionHandlerInput;
810
private final SubscriptionSender subscriptionSender;
911
private final ApolloSubscriptionKeepAliveRunner keepAliveRunner;
12+
private final ApolloSubscriptionConnectionListener connectionListener;
1013

1114
public ApolloSubscriptionProtocolFactory(SubscriptionHandlerInput subscriptionHandlerInput) {
1215
super("graphql-ws");
1316
this.subscriptionHandlerInput = subscriptionHandlerInput;
17+
this.connectionListener = subscriptionHandlerInput.getSubscriptionConnectionListener()
18+
.filter(ApolloSubscriptionConnectionListener.class::isInstance)
19+
.map(ApolloSubscriptionConnectionListener.class::cast)
20+
.orElse(new ApolloSubscriptionConnectionListener() {});
1421
subscriptionSender =
1522
new SubscriptionSender(subscriptionHandlerInput.getGraphQLObjectMapper().getJacksonMapper());
16-
keepAliveRunner = new ApolloSubscriptionKeepAliveRunner(subscriptionSender);
23+
keepAliveRunner = new ApolloSubscriptionKeepAliveRunner(subscriptionSender, connectionListener.getKeepAliveInterval());
1724
}
1825

1926
@Override
2027
public SubscriptionProtocolHandler createHandler() {
21-
return new ApolloSubscriptionProtocolHandler(subscriptionHandlerInput, subscriptionSender, keepAliveRunner);
28+
return new ApolloSubscriptionProtocolHandler(subscriptionHandlerInput, connectionListener, subscriptionSender, keepAliveRunner);
2229
}
2330
}

src/main/java/graphql/servlet/internal/ApolloSubscriptionProtocolHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ public class ApolloSubscriptionProtocolHandler extends SubscriptionProtocolHandl
3939
private final ApolloSubscriptionConnectionListener connectionListener;
4040

4141
public ApolloSubscriptionProtocolHandler(SubscriptionHandlerInput subscriptionHandlerInput,
42+
ApolloSubscriptionConnectionListener connectionListener,
4243
SubscriptionSender subscriptionSender,
4344
ApolloSubscriptionKeepAliveRunner keepAliveRunner) {
4445
this.input = subscriptionHandlerInput;
45-
this.connectionListener = subscriptionHandlerInput.getSubscriptionConnectionListener()
46-
.filter(ApolloSubscriptionConnectionListener.class::isInstance)
47-
.map(ApolloSubscriptionConnectionListener.class::cast)
48-
.orElse(new ApolloSubscriptionConnectionListener() {});
46+
this.connectionListener = connectionListener;
4947
this.sender = subscriptionSender;
5048
this.keepAliveRunner = keepAliveRunner;
5149
}

0 commit comments

Comments
 (0)