Skip to content

Commit

Permalink
Polishing in GraphQlSseHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Oct 18, 2024
1 parent 3ea216c commit c784612
Showing 1 changed file with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import org.springframework.graphql.execution.SubscriptionPublisherException;
import org.springframework.graphql.server.WebGraphQlHandler;
import org.springframework.graphql.server.WebGraphQlResponse;
import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.IdGenerator;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;
Expand All @@ -49,9 +47,6 @@
*/
public class GraphQlSseHandler extends AbstractGraphQlHttpHandler {

private final IdGenerator idGenerator = new AlternativeJdkIdGenerator();


public GraphQlSseHandler(WebGraphQlHandler graphQlHandler) {
super(graphQlHandler, null);
}
Expand Down Expand Up @@ -94,12 +89,7 @@ private static final class SseSubscriber extends BaseSubscriber<Map<String, Obje

private SseSubscriber(ServerResponse.SseBuilder sseBuilder) {
this.sseBuilder = sseBuilder;
this.sseBuilder.onTimeout(this::onTimeout);
}

private void onTimeout() {
this.cancel();
this.sseBuilder.error(new AsyncRequestTimeoutException());
this.sseBuilder.onTimeout(() -> cancelWithError(new AsyncRequestTimeoutException()));
}

@Override
Expand All @@ -113,11 +103,15 @@ private void writeResult(Map<String, Object> value) {
this.sseBuilder.data(value);
}
catch (IOException exception) {
cancel();
hookOnError(exception);
cancelWithError(exception);
}
}

private void cancelWithError(Throwable ex) {
this.cancel();
this.sseBuilder.error(ex);
}

@Override
protected void hookOnError(Throwable ex) {
if (ex instanceof SubscriptionPublisherException spe) {
Expand Down

0 comments on commit c784612

Please sign in to comment.