Skip to content

Commit

Permalink
用户积分系统+乐观锁
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jan 4, 2025
1 parent 917cfd3 commit 33904f9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import jakarta.persistence.LockModeType;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.List;
Expand Down Expand Up @@ -46,9 +48,9 @@ public PeerHistoryService(PeerHistoryRepository peerHistoryRepository, TorrentSe
// });
}

@Modifying
@Transactional
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Lock(LockModeType.OPTIMISTIC)
@Retryable(retryFor = OptimisticLockingFailureException.class, backoff = @Backoff(delay = 100, multiplier = 2))
public void saveHistories(List<PeerHistory> peerHistoryList) {
peerHistoryRepository.saveAll(peerHistoryList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class PeerHistory {
private String flags;
@Column(nullable = false)
private InetAddress submitterIp;
@Version
@Column(nullable = false)
private long version;
// @Column( columnDefinition = "jsonb")
// @Type(JsonType.class)
// private IPGeoData geoIP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import java.io.Serializable;
Expand Down Expand Up @@ -52,7 +51,6 @@ public SnapshotService(SnapshotRepository snapshotRepository, TorrentService tor
// });
}

@Modifying
@Transactional
//@Lock(LockModeType.PESSIMISTIC_WRITE)
public void saveSnapshots(List<Snapshot> snapshotList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import io.micrometer.core.instrument.MeterRegistry;
import jakarta.persistence.LockModeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -87,9 +89,9 @@ public User exchangeUserFromUserDto(UserDto dto) throws UserNotFoundException {
return optional.get();
}

@Modifying
@Transactional
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Lock(LockModeType.OPTIMISTIC)
@Retryable(retryFor = OptimisticLockingFailureException.class, backoff = @Backoff(delay = 100, multiplier = 2))
public User saveUser(User user) {
if (user.isSystemAccount()) {
throw new IllegalArgumentException("User email cannot ends with @sparkle.system, it's reserved by Sparkle system.");
Expand All @@ -100,9 +102,9 @@ public User saveUser(User user) {
return userRepository.save(user);
}

@Modifying
@Transactional
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Lock(LockModeType.OPTIMISTIC)
@Retryable(retryFor = OptimisticLockingFailureException.class, backoff = @Backoff(delay = 100, multiplier = 2))
public User saveSystemUser(User user) {
if (!user.isSystemAccount()) {
throw new IllegalArgumentException("System account email must ends with @sparkle.system");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class User implements Serializable {
private String bannedReason;
@Column(nullable = false)
private Integer randomGroup;
@Version
@Column(nullable = false)
private long version;

public boolean isSystemAccount() {
return email.endsWith("@sparkle.system");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.ghostchu.btn.sparkle.module.userscore.internal.UserScoreHistory;
import com.ghostchu.btn.sparkle.module.userscore.internal.UserScoreHistoryRepository;
import com.ghostchu.btn.sparkle.module.userscore.internal.UserScoreRepository;
import jakarta.persistence.LockModeType;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -34,6 +36,7 @@ public long getUserScoreBytes(User user) {

@Retryable(retryFor = OptimisticLockingFailureException.class, backoff = @Backoff(delay = 100, multiplier = 2))
@Transactional
@Lock(value = LockModeType.OPTIMISTIC)
public void addUserScoreBytes(User user, long changes, String reason) {
UserScore userScore = userScoreRepository.findByUser(user);
if (userScore != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public class UserScore {
@Column(nullable = false)
private Long scoreBytes;
@Version
private Long version;
@Column(nullable = false)
private long version;
}

0 comments on commit 33904f9

Please sign in to comment.