-
Notifications
You must be signed in to change notification settings - Fork 3
[refactor] redisson 적용 #177
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f0ee4f2
:recycle: 동시성 적용 중간 커밋
sehee123 24e3293
Merge remote-tracking branch 'origin/dev' into refactor/163
sehee123 82d1731
:recycle: redisson 적용
sehee123 c7f91ac
chore: Java 스타일 수정
2138e67
Merge remote-tracking branch 'origin/dev' into refactor/163
sehee123 aa744ce
:recycle: 이전 방 튕기기 roomId 고정
sehee123 e918864
Merge remote-tracking branch 'origin/refactor/163' into refactor/163
sehee123 76949ac
chore: Java 스타일 수정
976a5b9
:beers: globalExceptionHAndler 분기 추가
sehee123 21e8ea6
Merge remote-tracking branch 'origin/refactor/163' into refactor/163
sehee123 47e86c1
chore: Java 스타일 수정
3fff21b
:mute: 디버깅 로그 삭제
sehee123 3bdfb81
chore: Java 스타일 수정
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
279 changes: 172 additions & 107 deletions
279
backend/src/main/java/io/f1/backend/domain/game/app/RoomService.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
backend/src/main/java/io/f1/backend/global/lock/LockExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package io.f1.backend.global.lock; | ||
|
|
||
| import static io.f1.backend.global.lock.DistributedLockAspect.LOCK_KEY_FORMAT; | ||
|
|
||
| import io.f1.backend.global.exception.CustomException; | ||
| import io.f1.backend.global.exception.errorcode.CommonErrorCode; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import org.redisson.api.RLock; | ||
| import org.redisson.api.RedissonClient; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Supplier; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class LockExecutor { | ||
|
|
||
| private final RedissonClient redissonClient; | ||
|
|
||
| // 시간단위를 초로 변경 | ||
| private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS; | ||
|
|
||
| // 락 점유를 위한 대기 시간 | ||
| private static final long DEFAULT_WAIT_TIME = 5L; | ||
|
|
||
| // 락 점유 시간 | ||
| private static final long DEFAULT_LEASE_TIME = 3L; | ||
|
|
||
| public <T> T executeWithLock(String prefix, Object key, Supplier<T> supplier) { | ||
| String lockKey = formatLockKey(prefix, key); | ||
| RLock rlock = redissonClient.getLock(lockKey); | ||
|
|
||
| boolean acquired = false; | ||
| try { | ||
| acquired = rlock.tryLock(DEFAULT_WAIT_TIME, DEFAULT_LEASE_TIME, DEFAULT_TIME_UNIT); | ||
|
|
||
| if (!acquired) { | ||
| log.warn("[DistributedLock] Lock acquisition failed: {}", key); | ||
| throw new CustomException(CommonErrorCode.LOCK_ACQUISITION_FAILED); | ||
| } | ||
| log.info("[DistributedLock] Lock acquired: {}", key); | ||
|
|
||
| return supplier.get(); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new CustomException(CommonErrorCode.LOCK_INTERRUPTED); | ||
| } finally { | ||
| if (acquired && rlock.isHeldByCurrentThread()) { | ||
| rlock.unlock(); | ||
| log.info("[DistributedLock] Lock released: {}", key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void executeWithLock(String prefix, Object key, Runnable runnable) { | ||
| executeWithLock( | ||
| prefix, | ||
| key, | ||
| () -> { | ||
| runnable.run(); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| private String formatLockKey(String prefix, Object value) { | ||
| return String.format(LOCK_KEY_FORMAT, prefix, value); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.