@Getter
private final String name;
private final PersistenceStrategy persistence;
- private final Sinks.Many<Message> sink;
+ private final int bufferSize;
+ private Sinks.Many<Message> sink;
public Chatroom(
UUID id,
this.id = id;
this.name = name;
this.persistence = persistence;
- this.sink = Sinks.many().multicast().onBackpressureBuffer(bufferSize);
+ this.bufferSize = bufferSize;
+ this.sink = createSink();
}
{
return persistence
.persistMessage(Message.MessageKey.of(user, id), timestamp, text)
- .doOnNext(message -> sink.tryEmitNext(message).orThrow());
+ .doOnNext(message ->
+ {
+ Sinks.EmitResult result = sink.tryEmitNext(message);
+ if (result.isFailure())
+ {
+ log.warn("Emitting of message failed with {} for {}", result.name(), message);
+ }
+ });
}
return persistence.getMessage(Message.MessageKey.of(username, messageId));
}
- public Flux<Message> listen()
+ synchronized public Flux<Message> listen()
{
- return sink.asFlux();
+ return sink
+ .asFlux()
+ .doOnCancel(() -> sink = createSink()); // Sink hast to be recreated on auto-cancel!
}
public Flux<Message> getMessages()
{
return persistence.getMessages(first, last);
}
+
+ private Sinks.Many<Message> createSink()
+ {
+ return Sinks
+ .many()
+ .multicast()
+ .onBackpressureBuffer(bufferSize);
+ }
}