Skip to content

Commit

Permalink
Use Vert.x instace of Netty to schedule the closing task
Browse files Browse the repository at this point in the history
  • Loading branch information
cescoffier committed Nov 16, 2023
1 parent 8099553 commit a5246bf
Showing 1 changed file with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package io.quarkus.smallrye.graphql.runtime;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.jboss.logging.Logger;

import io.netty.util.concurrent.ScheduledFuture;
import io.quarkus.security.identity.CurrentIdentityAssociation;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser;
import io.smallrye.graphql.websocket.GraphQLWebSocketSession;
import io.smallrye.graphql.websocket.GraphQLWebsocketHandler;
import io.smallrye.graphql.websocket.graphqltransportws.GraphQLTransportWSSubprotocolHandler;
import io.smallrye.graphql.websocket.graphqlws.GraphQLWSSubprotocolHandler;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.net.impl.ConnectionBase;
import io.vertx.ext.web.RoutingContext;

/**
Expand Down Expand Up @@ -60,27 +58,30 @@ protected void doHandle(final RoutingContext ctx) {
}

QuarkusHttpUser user = (QuarkusHttpUser) ctx.user();
ScheduledFuture<?> authExpiryFuture = null;
long cancellation = -1L; // Do not use 0, as you won't be able to distinguish between not set, and the first task Id
if (user != null) {
//close the connection when the identity expires
Long expire = user.getSecurityIdentity().getAttribute("quarkus.identity.expire-time");
if (expire != null) {
authExpiryFuture = ((ConnectionBase) ctx.request().connection()).channel().eventLoop()
.schedule(() -> {
if (!serverWebSocket.isClosed()) {
serverWebSocket.close((short) 1008, "Authentication expired");
cancellation = ctx.vertx().setTimer((expire * 1000) - System.currentTimeMillis(),
new Handler<Long>() {
@Override
public void handle(Long event) {
if (!serverWebSocket.isClosed()) {
serverWebSocket.close((short) 1008, "Authentication expired");
}
}
}, (expire * 1000) - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
});
}
}

log.debugf("Starting websocket with subprotocol = %s", subprotocol);
GraphQLWebsocketHandler finalHandler = handler;
ScheduledFuture<?> finalAuthExpiryFuture = authExpiryFuture;
long finalCancellation = cancellation;
serverWebSocket.closeHandler(v -> {
finalHandler.onClose();
if (finalAuthExpiryFuture != null) {
finalAuthExpiryFuture.cancel(false);
if (finalCancellation != -1) {
ctx.vertx().cancelTimer(finalCancellation);
}
});
serverWebSocket.endHandler(v -> finalHandler.onEnd());
Expand Down

0 comments on commit a5246bf

Please sign in to comment.