feat: The size buffer for listeners to a chatroom is configurable
authorKai Moritz <kai@juplo.de>
Sat, 7 Jan 2023 20:05:30 +0000 (21:05 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 9 Jan 2023 19:55:49 +0000 (20:55 +0100)
src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java
src/main/java/de/juplo/kafka/chat/backend/domain/Chatroom.java
src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java

index aea5f8e..adabb92 100644 (file)
@@ -41,9 +41,9 @@ public class ChatBackendConfiguration
   }
 
   @Bean
-  ChatroomFactory chatroomFactory()
+  ChatroomFactory chatroomFactory(ChatBackendProperties properties)
   {
-    return new InMemoryChatroomFactory();
+    return new InMemoryChatroomFactory(properties.getChatroomBufferSize());
   }
 
   @Bean
index 122ac61..26149de 100644 (file)
@@ -14,4 +14,5 @@ public class ChatBackendProperties
 {
   private String datadir = Paths.get(System.getProperty("java.io.tmpdir"),"chat", "backend").toString();
   private String allowedOrigins = "http://localhost:4200";
+  private int chatroomBufferSize = 8;
 }
index 8f4a797..966a28e 100644 (file)
@@ -1,7 +1,6 @@
 package de.juplo.kafka.chat.backend.domain;
 
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
@@ -11,7 +10,6 @@ import java.time.LocalDateTime;
 import java.util.*;
 
 
-@RequiredArgsConstructor
 @Slf4j
 public class Chatroom
 {
@@ -20,7 +18,20 @@ public class Chatroom
   @Getter
   private final String name;
   private final PersistenceStrategy persistence;
-  private final Sinks.Many<Message> sink = Sinks.many().multicast().onBackpressureBuffer();
+  private final Sinks.Many<Message> sink;
+
+  public Chatroom(
+      UUID id,
+      String name,
+      PersistenceStrategy persistence,
+      int bufferSize)
+  {
+    this.id = id;
+    this.name = name;
+    this.persistence = persistence;
+    this.sink = Sinks.many().multicast().onBackpressureBuffer(bufferSize);
+  }
+
 
   synchronized public Mono<Message> addMessage(
       Long id,
index 415a71f..a405be9 100644 (file)
@@ -2,21 +2,24 @@ package de.juplo.kafka.chat.backend.persistence;
 
 import de.juplo.kafka.chat.backend.domain.Chatroom;
 import de.juplo.kafka.chat.backend.domain.ChatroomFactory;
-import de.juplo.kafka.chat.backend.domain.PersistenceStrategy;
 import lombok.RequiredArgsConstructor;
 
+import java.util.LinkedHashMap;
 import java.util.UUID;
 
 
 @RequiredArgsConstructor
 public class InMemoryChatroomFactory implements ChatroomFactory<InMemoryPersistenceStrategy>
 {
+  private final int bufferSize;
+
+
   @Override
   public Chatroom createChatroom(UUID id, String name)
   {
     InMemoryPersistenceStrategy persistenceStrategy =
         new InMemoryPersistenceStrategy(new LinkedHashMap<>());
-    return new Chatroom(id, name, persistenceStrategy);
+    return new Chatroom(id, name, persistenceStrategy, bufferSize);
   }
 
   @Override
@@ -25,6 +28,6 @@ public class InMemoryChatroomFactory implements ChatroomFactory<InMemoryPersiste
       String name,
       InMemoryPersistenceStrategy persistenceStrategy)
   {
-    return new Chatroom(id, name, persistenceStrategy);
+    return new Chatroom(id, name, persistenceStrategy, bufferSize);
   }
 }