Skip to content

Commit

Permalink
Increase SSE timeout from 10 to 90 secs
Browse files Browse the repository at this point in the history
  • Loading branch information
amengus87 committed Jun 11, 2024
1 parent c48f52c commit 0ae9240
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions backend/src/main/java/ai/dragon/service/SseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

@Service
public class SseService {
// TODO Custom timeout :
public static final long DEFAULT_TIMEOUT = 90L * 1000;

private final Logger logger = LoggerFactory.getLogger(this.getClass());

private ConcurrentHashMap<UUID, SseEmitter> emitters = new ConcurrentHashMap<>();

public UUID createEmitter() {
return createEmitter(10L * 1000L);
return createEmitter(DEFAULT_TIMEOUT);
}

public UUID createEmitter(Long timeout) {
Expand All @@ -36,27 +39,31 @@ public SseEmitter retrieveEmitter(UUID id) {
return emitters.get(id);
}

public void complete(UUID id) {
public boolean complete(UUID id) {
SseEmitter emitter = emitters.get(id);
if (emitter == null) {
logger.warn("No emitter found for id: {}", id);
return;
logger.warn("Can't complete : No emitter found for id '{}'", id);
return false;
}
emitter.complete();
emitters.remove(id);
return true;
}

public void sendEvent(UUID id, Object event) {
public boolean sendEvent(UUID id, Object event) {
SseEmitter emitter = emitters.get(id);
if (emitter == null) {
logger.warn("No emitter found for id: {}", id);
return;
logger.info(event.toString());
logger.warn("Can't send event : No emitter found for id '{}'", id);
return false;
}
try {
emitter.send(event);
} catch (IOException e) {
emitter.complete();
emitters.remove(id);
return false;
}
return true;
}
}

0 comments on commit 0ae9240

Please sign in to comment.