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

✨ feat(api): implement redisson lock for item creation to prevent duplication item registration #503

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/streetdrop-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ dependencies {

implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'io.awspring.cloud:spring-cloud-starter-aws:2.4.4'
// redisson
implementation group: 'org.redisson', name: 'redisson-spring-boot-starter', version: '3.34.1'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
public enum ItemErrorCode implements ErrorCodeInterface {
ITEM_NOT_FOUND(HttpStatus.NOT_FOUND, "ITEM_NOT_FOUND", "Item Not Found", "Item Not Found"),
ITEM_ALREADY_LIKED(HttpStatus.CONFLICT, "ITEM_ALREADY_LIKED", "Item Already Liked", "User already item liked"),
ITEM_ALREADY_REPORTED(HttpStatus.CONFLICT, " ITEM_ALREADY_REPORTED", "Item Already Reported", "User already item reported");
ITEM_ALREADY_REPORTED(HttpStatus.CONFLICT, " ITEM_ALREADY_REPORTED", "Item Already Reported", "User already item reported"),
ITEM_CREATE_PROCESSING(HttpStatus.PROCESSING, "ITEM_CREATED_PROCESSING", "Item Created Processing", "Another request is currently being processed. Please try again later"),
ITEM_CREATE_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "ITEM_CREATED_FAIL", "Item Created Fail", "Item Created Fail");

private final HttpStatus status;
private final String errorResponseCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
import com.depromeet.util.GeomUtil;
import lombok.RequiredArgsConstructor;
import org.locationtech.jts.geom.Point;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class ItemService {
private final RedissonClient redissonClient;
private final MusicService musicService;
private final ItemRepository itemRepository;
private final ItemLocationRepository itemLocationRepository;
Expand All @@ -53,30 +57,45 @@ public PoiResponseDto findNearItemsPoints(User user, NearItemPointRequestDto nea

@Transactional
public ItemResponseDto create(User user, ItemCreateRequestDto itemCreateRequestDto) {
var song = musicService.getOrCreateMusic(itemCreateRequestDto.getMusic());

var item = Item.builder()
.user(user)
.albumCover(song.getAlbum().getAlbumCover())
.song(song)
.content(itemCreateRequestDto.getContent())
.build();

ItemLocationRequestDto locationRequestDto = itemCreateRequestDto.getLocation();
Point point = GeomUtil.createPoint(locationRequestDto.getLongitude(), locationRequestDto.getLatitude());
String lockKey = "item:create" + user.getId();
RLock lock = redissonClient.getLock(lockKey);

try {
boolean creatable = lock.tryLock(5, 5, TimeUnit.SECONDS);
if (!creatable) {
throw new BusinessException(ItemErrorCode.ITEM_CREATE_PROCESSING);
}

var song = musicService.getOrCreateMusic(itemCreateRequestDto.getMusic());

var item = Item.builder()
.user(user)
.albumCover(song.getAlbum().getAlbumCover())
.song(song)
.content(itemCreateRequestDto.getContent())
.build();

ItemLocationRequestDto locationRequestDto = itemCreateRequestDto.getLocation();
Point point = GeomUtil.createPoint(locationRequestDto.getLongitude(), locationRequestDto.getLatitude());
VillageArea villageArea = villageAreaService.getVillageByLocationPoint(point);

ItemLocation itemLocation = ItemLocation.builder()
.name(locationRequestDto.getAddress())
.item(item)
ItemLocation itemLocation = ItemLocation.builder()
.name(locationRequestDto.getAddress())
.item(item)
.villageArea(villageArea)
.point(point)
.build();

item.setItemLocation(itemLocation);
var savedItem = itemRepository.save(item);

return new ItemResponseDto(savedItem);
.point(point)
.build();

item.setItemLocation(itemLocation);
var savedItem = itemRepository.save(item);

return new ItemResponseDto(savedItem);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new BusinessException(ItemErrorCode.ITEM_CREATE_FAIL);
} finally {
lock.unlock();
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.depromeet.external.redisson;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer()
.setAddress("redis://localhost:6379");

return Redisson.create(config);
}
}
Loading