Skip to content

Commit

Permalink
Fix: 커넥션 풀 사이즈 조정
Browse files Browse the repository at this point in the history
  • Loading branch information
kwondongwook committed Dec 29, 2023
1 parent 5b53823 commit 74f11e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
26 changes: 13 additions & 13 deletions src/main/java/com/api/trip/common/sse/emitter/SseEmitterMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,40 @@
@Slf4j
public class SseEmitterMap {

private final Map<Long, SseEmitter> sseEmitterMap = new ConcurrentHashMap<>();
private final Map<String, SseEmitter> sseEmitterMap = new ConcurrentHashMap<>();

public void put(Long memberId, SseEmitter sseEmitter) {
sseEmitter.onCompletion(() -> remove(memberId));
public void put(String email, SseEmitter sseEmitter) {
sseEmitter.onCompletion(() -> remove(email));
sseEmitter.onTimeout(sseEmitter::complete);
sseEmitterMap.put(memberId, sseEmitter);
log.info("connected with {}, the number of connections is {}", memberId, sseEmitterMap.size());
sseEmitterMap.put(email, sseEmitter);
log.info("connected with {}, the number of connections is {}", email, sseEmitterMap.size());
}

public void remove(Long memberId) {
sseEmitterMap.remove(memberId);
log.info("disconnected with {}, the number of connections is {}", memberId, sseEmitterMap.size());
public void remove(String email) {
sseEmitterMap.remove(email);
log.info("disconnected with {}, the number of connections is {}", email, sseEmitterMap.size());
}

public void send(Long memberId, String eventName, Object eventData) {
SseEmitter sseEmitter = sseEmitterMap.get(memberId);
public void send(String email, String eventName, Object eventData) {
SseEmitter sseEmitter = sseEmitterMap.get(email);
try {
sseEmitter.send(
event()
.name(eventName)
.data(eventData)
);
} catch (IOException | IllegalStateException e) {
remove(memberId);
remove(email);
}
}

public void sendToAll(String eventName, Object eventData) {
SseEventBuilder sseEventBuilder = event().name(eventName).data(eventData);
sseEmitterMap.forEach((memberId, sseEmitter) -> {
sseEmitterMap.forEach((email, sseEmitter) -> {
try {
sseEmitter.send(sseEventBuilder);
} catch (IOException | IllegalStateException e) {
remove(memberId);
remove(email);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.api.trip.domain.notification.controller;

import com.api.trip.common.exception.CustomException;
import com.api.trip.common.exception.ErrorCode;
import com.api.trip.common.sse.emitter.SseEmitterMap;
import com.api.trip.domain.member.repository.MemberRepository;
import com.api.trip.domain.notification.controller.dto.DeleteNotificationRequest;
import com.api.trip.domain.notification.controller.dto.GetMyNotificationsResponse;
import com.api.trip.domain.notification.controller.dto.ReadNotificationRequest;
Expand All @@ -22,20 +19,15 @@
@RequiredArgsConstructor
public class NotificationController {

private final MemberRepository memberRepository;
private final NotificationService notificationService;
private final SseEmitterMap sseEmitterMap;

@GetMapping(value = "/connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public ResponseEntity<SseEmitter> connect() {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
Long memberId = memberRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.UNAUTHORIZED))
.getId();

SseEmitter sseEmitter = new SseEmitter(3600000L);
sseEmitterMap.put(memberId, sseEmitter);
sseEmitterMap.send(memberId, "connect", LocalDateTime.now());
sseEmitterMap.put(email, sseEmitter);
sseEmitterMap.send(email, "connect", LocalDateTime.now());
return ResponseEntity.ok(sseEmitter);
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
spring:
profiles:
active: dev
datasource:
hikari:
maximum-pool-size: 4
h2:
console:
enabled: true
Expand Down

0 comments on commit 74f11e3

Please sign in to comment.