Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate operation IDs using an incrementing number instead of random… #1293

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
Expand All @@ -24,6 +23,8 @@
import io.smallrye.graphql.client.InvalidResponseException;
import io.smallrye.graphql.client.impl.ResponseReader;
import io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler;
import io.smallrye.graphql.client.vertx.websocket.opid.IncrementingNumberOperationIDGenerator;
import io.smallrye.graphql.client.vertx.websocket.opid.OperationIDGenerator;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.subscription.Cancellable;
import io.smallrye.mutiny.subscription.MultiEmitter;
Expand Down Expand Up @@ -51,6 +52,8 @@ public class GraphQLTransportWSSubprotocolHandler implements WebSocketSubprotoco

private final Runnable onClose;

private final OperationIDGenerator operationIdGenerator;

public GraphQLTransportWSSubprotocolHandler(WebSocket webSocket, Integer subscriptionInitializationTimeout,
Runnable onClose) {
this.webSocket = webSocket;
Expand All @@ -59,6 +62,7 @@ public GraphQLTransportWSSubprotocolHandler(WebSocket webSocket, Integer subscri
this.multiOperations = new ConcurrentHashMap<>();
this.initialization = initialize().subscribeAsCompletionStage();
this.onClose = onClose;
this.operationIdGenerator = new IncrementingNumberOperationIDGenerator();
}

@Override
Expand Down Expand Up @@ -229,7 +233,7 @@ private void failAllActiveOperationsWith(Throwable throwable) {

@Override
public String executeUni(JsonObject request, UniEmitter<? super String> emitter) {
String id = UUID.randomUUID().toString();
String id = operationIdGenerator.generate();
ensureInitialized().subscribe().with(ready -> {
uniOperations.put(id, emitter);
JsonObject subscribe = createSubscribeMessage(request, id);
Expand All @@ -240,7 +244,7 @@ public String executeUni(JsonObject request, UniEmitter<? super String> emitter)

@Override
public String executeMulti(JsonObject request, MultiEmitter<? super String> emitter) {
String id = UUID.randomUUID().toString();
String id = operationIdGenerator.generate();
ensureInitialized().subscribe().with(ready -> {
multiOperations.put(id, emitter);
JsonObject subscribe = createSubscribeMessage(request, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.StringReader;
import java.time.Duration;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -21,6 +20,8 @@
import io.smallrye.graphql.client.InvalidResponseException;
import io.smallrye.graphql.client.impl.ResponseReader;
import io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler;
import io.smallrye.graphql.client.vertx.websocket.opid.IncrementingNumberOperationIDGenerator;
import io.smallrye.graphql.client.vertx.websocket.opid.OperationIDGenerator;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.subscription.Cancellable;
import io.smallrye.mutiny.subscription.MultiEmitter;
Expand All @@ -45,13 +46,16 @@ public class GraphQLWSSubprotocolHandler implements WebSocketSubprotocolHandler

private final Runnable onClose;

private final OperationIDGenerator operationIdGenerator;

public GraphQLWSSubprotocolHandler(WebSocket webSocket, Integer subscriptionInitializationTimeout, Runnable onClose) {
this.webSocket = webSocket;
this.subscriptionInitializationTimeout = subscriptionInitializationTimeout;
this.uniOperations = new ConcurrentHashMap<>();
this.multiOperations = new ConcurrentHashMap<>();
this.initialization = initialize().subscribeAsCompletionStage();
this.onClose = onClose;
this.operationIdGenerator = new IncrementingNumberOperationIDGenerator();
}

private Uni<Void> initialize() {
Expand Down Expand Up @@ -150,7 +154,7 @@ public Uni<Void> ensureInitialized() {

@Override
public String executeUni(JsonObject request, UniEmitter<? super String> emitter) {
String id = UUID.randomUUID().toString();
String id = operationIdGenerator.generate();
ensureInitialized().subscribe().with(ready -> {
uniOperations.put(id, emitter);
JsonObject subscribe = createSubscribeMessage(request, id);
Expand All @@ -161,7 +165,7 @@ public String executeUni(JsonObject request, UniEmitter<? super String> emitter)

@Override
public String executeMulti(JsonObject request, MultiEmitter<? super String> emitter) {
String id = UUID.randomUUID().toString();
String id = operationIdGenerator.generate();
ensureInitialized().subscribe().with(ready -> {
multiOperations.put(id, emitter);
JsonObject subscribe = createSubscribeMessage(request, id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.smallrye.graphql.client.vertx.websocket.opid;

import java.util.concurrent.atomic.AtomicLong;

public class IncrementingNumberOperationIDGenerator implements OperationIDGenerator {

private AtomicLong generator = new AtomicLong(1);

@Override
public String generate() {
return String.valueOf(generator.getAndIncrement());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.smallrye.graphql.client.vertx.websocket.opid;

public interface OperationIDGenerator {

String generate();

}