Skip to content
Extraits de code Groupes Projets
Valider 1032c464 rédigé par oussama aftys's avatar oussama aftys
Parcourir les fichiers

Optimize performance for 1000+ concurrent connections

parent 4e715bdc
Branches
1 requête de fusion!24Optimize performance for 1000+ concurrent connections
Pipeline #15811 réussi avec les étapes
in 1 minute et 47 secondes
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
### Added ### Added
- optimize the push product and hide product features - optimize the push product and hide product features
- fixed live pagination bug - fixed live pagination bug
- Optimize performance for 1000+ concurrent connections
## [0.0.7-RELEASE] ## [0.0.7-RELEASE]
### Added ### Added
- added export messages feature - added export messages feature
......
package com.marketingconfort.mydressin.mydressinstreamservices.configs; package com.marketingconfort.mydressin.mydressinstreamservices.configs;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator; import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory; import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;
...@@ -26,18 +30,46 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { ...@@ -26,18 +30,46 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
.setAllowedOriginPatterns("*") .setAllowedOriginPatterns("*")
.withSockJS() .withSockJS()
.setWebSocketEnabled(true) .setWebSocketEnabled(true)
.setHeartbeatTime(25000)
.setDisconnectDelay(5000)
.setClientLibraryUrl("https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.5.1/sockjs.min.js"); .setClientLibraryUrl("https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.5.1/sockjs.min.js");
} }
@Override @Override
public void configureMessageBroker(@NonNull MessageBrokerRegistry config) { public void configureMessageBroker(@NonNull MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic"); // Configure thread pool for message broker
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(4);
taskScheduler.setThreadNamePrefix("ws-heartbeat-scheduler-");
taskScheduler.initialize();
// Configure in-memory broker with optimized settings
config.enableSimpleBroker("/topic")
.setTaskScheduler(taskScheduler)
.setHeartbeatValue(new long[]{10000, 10000});
config.setApplicationDestinationPrefixes("/app"); config.setApplicationDestinationPrefixes("/app");
// Configure message size limits
config.setPreservePublishOrder(true);
} }
@Override @Override
public void configureWebSocketTransport( public void configureWebSocketTransport(@NonNull WebSocketTransportRegistration registration) {
@NonNull final org.springframework.web.socket.config.annotation.WebSocketTransportRegistration registration) { // Configure message size and timeout limits
registration.setMessageSizeLimit(128 * 1024) // 128KB
.setSendTimeLimit(20 * 1000) // 20 seconds
.setSendBufferSizeLimit(512 * 1024) // 512KB
.setTimeToFirstMessage(30 * 1000); // 30 seconds
// Configure thread pools for message processing
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(1000);
taskExecutor.setThreadNamePrefix("ws-message-executor-");
taskExecutor.initialize();
registration.addDecoratorFactory(new WebSocketHandlerDecoratorFactory() { registration.addDecoratorFactory(new WebSocketHandlerDecoratorFactory() {
@Override @Override
public @NonNull WebSocketHandler decorate(@NonNull final WebSocketHandler handler) { public @NonNull WebSocketHandler decorate(@NonNull final WebSocketHandler handler) {
...@@ -45,6 +77,8 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { ...@@ -45,6 +77,8 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override @Override
public void afterConnectionEstablished(@NonNull final WebSocketSession session) throws Exception { public void afterConnectionEstablished(@NonNull final WebSocketSession session) throws Exception {
connectedClients.incrementAndGet(); connectedClients.incrementAndGet();
session.setTextMessageSizeLimit(64 * 1024); // 64KB per message
session.setBinaryMessageSizeLimit(128 * 1024); // 128KB for binary
super.afterConnectionEstablished(session); super.afterConnectionEstablished(session);
} }
......
0% ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter