-
Notifications
You must be signed in to change notification settings - Fork 0
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
[LIME-71 ] 투표 참여 API 데드락 & 동시성 이슈 해결 #75
Merged
Yiseull
merged 4 commits into
main
from
LIME-71-solve-concurrency-issue-in-vote-participation
Apr 1, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
34 changes: 34 additions & 0 deletions
34
lime-api/src/main/java/com/programmers/lime/domains/vote/application/VoteLockManager.java
This file contains 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,34 @@ | ||
package com.programmers.lime.domains.vote.application; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.programmers.lime.domains.vote.domain.Vote; | ||
import com.programmers.lime.domains.vote.implementation.VoteManager; | ||
import com.programmers.lime.redis.vote.VoteRedisManager; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class VoteLockManager { | ||
|
||
private final VoteManager voteManager; | ||
private final VoteRedisManager voteRedisManager; | ||
|
||
public void participate( | ||
final Vote vote, | ||
final Long memberId, | ||
final Long itemId | ||
) throws InterruptedException | ||
{ | ||
while (Boolean.FALSE.equals(voteRedisManager.lock(String.valueOf(vote.getId())))) { | ||
Thread.sleep(10); | ||
} | ||
|
||
try { | ||
voteManager.participate(vote, memberId, itemId); | ||
} finally { | ||
voteRedisManager.unlock(String.valueOf(vote.getId())); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
10ms마다 검사하는 방법이 가장 효율적이었던건지 궁금해서 질문드립니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제가 생각할 때는 10이 가장 적절한 것 같습니다!
락을 얻기 위해 계속 시도하면 레디스에 부하를 주기 때문에
Thread.sleep()
으로 약간의 시간차를 두고 락 획득을 시도하도록 하였습니다. 다만 이 시간을 어떻게 설정해야 될까 고민을 많이 했고, 100, 50, 10 이렇게 3가지를 고려했습니다. 아무래도 처리 로직이 간단하기 때문에 처리 속도도 빠릅니다. 따라서 tps량이 가장 많은 10으로 선택을 했고, 레디스가 그 정도 부하는 충분히 버틸 수 있다고 생각했습니다! 10으로도 문제 없이 처리할 수 있는데 100과 50으로 시간을 설정하는 것은 오히려 응답 시간을 지연시킬 수 있다고 생각했습니다.