Skip to content

Commit

Permalink
Refactor/graceful shutdown 적용 (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
This2sho authored Nov 27, 2024
1 parent 8f33917 commit 280cc73
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
3 changes: 3 additions & 0 deletions app-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ cors:
allowedOrigins: ${ORIGIN:http://localhost:3000}

api-prefix: ${API_PREFIX:/}

server:
shutdown: graceful
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,13 @@ public ApiCounter(int minTotalCount) {
this.isOpened = false;
}

public void countUp() {
while (true) {
int expected = getTotalCount();
int newValue = expected + 1;
if (totalCount.compareAndSet(expected, newValue)) {
return;
}
}
public void totalCountUp() {
totalCount.incrementAndGet();
}

public void errorCountUp() {
countUp();
while (true) {
int expected = getErrorCount();
int newValue = expected + 1;
if (errorCount.compareAndSet(expected, newValue)) {
return;
}
}
totalCountUp();
errorCount.incrementAndGet();
}

public void reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Object around(ProceedingJoinPoint proceedingJoinPoint, CircuitBreaker ann
}
try {
Object result = proceedingJoinPoint.proceed();
apiCounter.countUp();
apiCounter.totalCountUp();
return result;
} catch (Throwable e) {
handleError(annotation, apiCounter);
Expand All @@ -37,10 +37,7 @@ public Object around(ProceedingJoinPoint proceedingJoinPoint, CircuitBreaker ann

private ApiCounter getApiCounter(ProceedingJoinPoint proceedingJoinPoint, int minTotalCount) {
Object target = proceedingJoinPoint.getTarget();
if (!map.containsKey(target)) {
map.put(target, new ApiCounter(minTotalCount));
}
return map.get(target);
return map.computeIfAbsent(target, key -> new ApiCounter(minTotalCount));
}

private void handleError(CircuitBreaker annotation, ApiCounter apiCounter) {
Expand Down
6 changes: 6 additions & 0 deletions app-scheduler/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
spring:
profiles:
active: ${PROFILE:dev}
lifecycle:
timeout-per-shutdown-phase: 5m

server:
shutdown: graceful

# API KEY
kakao:
key: ${KAKAO_API_KEY:kakao}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ApiCounterTest {
ApiCounter apiCounter = new ApiCounter();

for (int i = 0; i < 8; i++) {
apiCounter.countUp();
apiCounter.totalCountUp();
}
for (int i = 0; i < 2; i++) {
apiCounter.errorCountUp();
Expand Down Expand Up @@ -50,7 +50,7 @@ class ApiCounterTest {
}
executorService.submit(() -> {
try {
apiCounter.countUp();
apiCounter.totalCountUp();
} finally {
latch.countDown();
}
Expand All @@ -76,7 +76,7 @@ class ApiCounterTest {
for (int i = 0; i < threadCount; i++) {
executorService.submit(() -> {
try {
apiCounter.countUp();
apiCounter.totalCountUp();
} finally {
latch.countDown();
}
Expand Down

0 comments on commit 280cc73

Please sign in to comment.