Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: 대기열 시스템 구조 개선 #66

Merged
merged 9 commits into from
Aug 16, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.thirdparty.ticketing.domain.common;

public interface Event {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.thirdparty.ticketing.domain.common;

public interface EventPublisher {

void publish(Event event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.List;
import java.util.Map;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waiting.room.RunningRoom;
import com.thirdparty.ticketing.domain.waiting.room.WaitingRoom;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public class DefaultWaitingManager extends WaitingManager {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.thirdparty.ticketing.domain.waiting.manager;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waiting.room.RunningRoom;
import com.thirdparty.ticketing.domain.waiting.room.WaitingRoom;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

import lombok.RequiredArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public class DefaultRunningRoom implements RunningRoom {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public class DefaultWaitingCounter implements WaitingCounter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public class DefaultWaitingLine implements WaitingLine {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public class DefaultWaitingRoom extends WaitingRoom {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public interface RunningRoom {
boolean contains(WaitingMember waitingMember);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.thirdparty.ticketing.domain.waiting.room;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public interface WaitingCounter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public interface WaitingLine {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

import lombok.RequiredArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thirdparty.ticketing.domain.waitingsystem;

import com.thirdparty.ticketing.domain.common.Event;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class PollingEvent implements Event {

private final long performanceId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.thirdparty.ticketing.domain.waitingsystem;

import java.util.Set;

public interface RunningManager {
boolean isReadyToHandle(String email, long performanceId);

long getRunningCount(long performanceId);

long getAvailableToRunning(long performanceId);

void enterRunningRoom(long performanceId, Set<WaitingMember> waitingMembers);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.thirdparty.ticketing.domain.waitingsystem;

public interface RunningRoom {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thirdparty.ticketing.domain.waiting;
package com.thirdparty.ticketing.domain.waitingsystem;

import jakarta.servlet.http.HttpServletRequest;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package com.thirdparty.ticketing.domain.waiting;
package com.thirdparty.ticketing.domain.waitingsystem;

import java.util.Map;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.thirdparty.ticketing.domain.common.LoginMember;
import com.thirdparty.ticketing.domain.waiting.manager.WaitingManager;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class WaitingController {

private final WaitingManager waitingManager;
private final WaitingSystem waitingSystem;

@GetMapping("/performances/{performanceId}/wait")
public ResponseEntity<Map<String, Long>> getCounts(
@LoginMember String email, @PathVariable("performanceId") Long performanceId) {
long remainingCount = waitingManager.getRemainingCount(email, performanceId);
long remainingCount = waitingSystem.getRemainingCount(email, performanceId);
return ResponseEntity.ok(Map.of("remainingCount", remainingCount));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thirdparty.ticketing.domain.waitingsystem;

import java.util.Set;

public interface WaitingManager {
void enterWaitingRoom(String email, long performanceId);

WaitingMember findWaitingMember(String email, long performanceId);

Set<WaitingMember> pullOutMembers(long performanceId, long availableToRunning);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thirdparty.ticketing.domain.waiting;
package com.thirdparty.ticketing.domain.waitingsystem;

import java.time.ZonedDateTime;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.thirdparty.ticketing.domain.waitingsystem;

public interface WaitingRoom {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thirdparty.ticketing.domain.waitingsystem;

import java.util.Set;

import com.thirdparty.ticketing.domain.common.EventPublisher;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class WaitingSystem {

private final WaitingManager waitingManager;
private final RunningManager runningManager;
private final EventPublisher eventPublisher;

public boolean isReadyToHandle(String email, long performanceId) {
return runningManager.isReadyToHandle(email, performanceId);
}

public void enterWaitingRoom(String email, long performanceId) {
waitingManager.enterWaitingRoom(email, performanceId);
}

public long getRemainingCount(String email, long performanceId) {
WaitingMember waitingMember = waitingManager.findWaitingMember(email, performanceId);
long runningCount = runningManager.getRunningCount(performanceId);
eventPublisher.publish(new PollingEvent(performanceId));
return waitingMember.getWaitingCount() - runningCount;
}

public void moveUserToRunning(long performanceId) {
long availableToRunning = runningManager.getAvailableToRunning(performanceId);
Set<WaitingMember> waitingMembers =
waitingManager.pullOutMembers(performanceId, availableToRunning);
runningManager.enterRunningRoom(performanceId, waitingMembers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waiting.manager.WaitingManager;
import com.thirdparty.ticketing.domain.waiting.room.RunningRoom;
import com.thirdparty.ticketing.domain.waiting.room.WaitingRoom;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public class RedisWaitingManager extends WaitingManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waiting.room.RunningRoom;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public class RedisRunningRoom implements RunningRoom {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waiting.room.WaitingCounter;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

public class RedisWaitingCounter implements WaitingCounter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.springframework.data.redis.core.ZSetOperations;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waiting.room.WaitingLine;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;
import com.thirdparty.ticketing.global.waiting.ObjectMapperUtils;

public class RedisWaitingLine implements WaitingLine {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.springframework.data.redis.core.RedisTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waiting.room.WaitingCounter;
import com.thirdparty.ticketing.domain.waiting.room.WaitingLine;
import com.thirdparty.ticketing.domain.waiting.room.WaitingRoom;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;
import com.thirdparty.ticketing.global.waiting.ObjectMapperUtils;

public class RedisWaitingRoom extends WaitingRoom {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waiting.room.DefaultRunningRoom;
import com.thirdparty.ticketing.domain.waiting.room.DefaultWaitingCounter;
import com.thirdparty.ticketing.domain.waiting.room.DefaultWaitingLine;
import com.thirdparty.ticketing.domain.waiting.room.DefaultWaitingRoom;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;

class DefaultWaitingManagerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;
import com.thirdparty.ticketing.global.waiting.room.RedisRunningRoom;
import com.thirdparty.ticketing.support.TestContainerStarter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;
import com.thirdparty.ticketing.global.waiting.room.RedisWaitingCounter;
import com.thirdparty.ticketing.support.TestContainerStarter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;
import com.thirdparty.ticketing.global.waiting.room.RedisWaitingLine;
import com.thirdparty.ticketing.support.TestContainerStarter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;
import com.thirdparty.ticketing.global.waiting.manager.RedisWaitingManager;
import com.thirdparty.ticketing.support.TestContainerStarter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirdparty.ticketing.domain.waiting.WaitingMember;
import com.thirdparty.ticketing.domain.waitingsystem.WaitingMember;
import com.thirdparty.ticketing.global.waiting.room.RedisWaitingRoom;
import com.thirdparty.ticketing.support.TestContainerStarter;

Expand Down
Loading