Skip to content

Fix blocking calls #8

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions src/main/java/com/github/rawsanj/handler/ChatWebSocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
import reactor.core.scheduler.Schedulers;

@Slf4j
public class ChatWebSocketHandler implements WebSocketHandler {
Expand Down Expand Up @@ -40,15 +41,19 @@ public Mono<Void> handle(WebSocketSession webSocketSession) {
Mono<Void> inputMessage = webSocketSession.receive()
.flatMap(webSocketMessage -> redisChatMessagePublisher.publishChatMessage(webSocketMessage.getPayloadAsText()))
.doOnSubscribe(subscription -> {
long activeUserCount = activeUserCounter.incrementAndGet();
log.info("User '{}' Connected. Total Active Users: {}", webSocketSession.getId(), activeUserCount);
chatMessageSink.tryEmitNext(new ChatMessage(0, "CONNECTED", "CONNECTED", activeUserCount));
Mono.fromRunnable(() -> {
long activeUserCount = activeUserCounter.incrementAndGet();
log.info("User '{}' Connected. Total Active Users: {}", webSocketSession.getId(), activeUserCount);
chatMessageSink.tryEmitNext(new ChatMessage(0, "CONNECTED", "CONNECTED", activeUserCount));
}).subscribeOn(Schedulers.boundedElastic()).subscribe();
})
.doOnError(throwable -> log.error("Error Occurred while sending message to Redis.", throwable))
.doFinally(signalType -> {
long activeUserCount = activeUserCounter.decrementAndGet();
log.info("User '{}' Disconnected. Total Active Users: {}", webSocketSession.getId(), activeUserCount);
chatMessageSink.tryEmitNext(new ChatMessage(0, "DISCONNECTED", "DISCONNECTED", activeUserCount));
Mono.fromRunnable(() -> {
long activeUserCount = activeUserCounter.decrementAndGet();
log.info("User '{}' Disconnected. Total Active Users: {}", webSocketSession.getId(), activeUserCount);
chatMessageSink.tryEmitNext(new ChatMessage(0, "DISCONNECTED", "DISCONNECTED", activeUserCount));
}).subscribeOn(Schedulers.boundedElastic()).subscribe();
})
.then();

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/github/rawsanj/handler/WebHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.scheduler.Schedulers;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
Expand All @@ -23,7 +24,9 @@ public RouterFunction<ServerResponse> htmlRouter(@Value("classpath:/static/index
return route(GET("/"), request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(html))
.andRoute(POST("/message"), request -> request.bodyToMono(Message.class)
.flatMap(message -> redisChatMessagePublisher.publishChatMessage(message.getMessage()))
.flatMap(aLong -> ServerResponse.ok().bodyValue(new Message("Message Sent Successfully!."))));
.flatMap(aLong -> ServerResponse.ok().bodyValue(new Message("Message Sent Successfully!.")))
.subscribeOn(Schedulers.boundedElastic()));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to make this run on Schedulers.boundedElastic() Did the BlockHound detected blocking call over here or inside the RedisChatMessagePublisher?

Copy link
Author

@Arooba-git Arooba-git Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the first stack trace (first screenshot in the post), we see that the error propagates to RedisChatMessagePublisher but was triggered by WebHttpHandler, line 26 right? :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the first stack trace (first screenshot in the post), we see that the error propagates to RedisChatMessagePublisher but was triggered by WebHttpHandler, line 26 right? :)

The other way round. The actual blocking call is at line - https://github.com/RawSanj/spring-redis-websocket/blob/master/src/main/java/com/github/rawsanj/messaging/RedisChatMessagePublisher.java#L35C11-L35C28 the Atomic Increment is blocking.

I think we should be good if we move that blocking call Integer totalChatMessage = chatMessageCounter.incrementAndGet() inside the Mono.fromCallable() and then add the .subscribeOn(Schedulers.boundedElastic()) in the end.


}

}