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

[STEP3-1] shorten url에 expiredAt 추가 #35

Merged
merged 10 commits into from
Feb 9, 2025

Conversation

seeeeeeong
Copy link
Collaborator

shorten url에 expiredAt 추가

  • shortenUrl에 expiredAt을 추가하여 조회 시, 만료된 url일 경우 에러를 반환하도록 하였습니다.
  • 스케줄러를 추가하여 매 정각에 만료된 url을 제거하도록 하였습니다.
  • 관련 테스트 코드를 추가하였습니다.

@seeeeeeong seeeeeeong requested a review from Hyune-c February 7, 2025 18:11
Comment on lines 8 to 9
// Authorization
EXPIRED_SHORTEN_URL(HttpStatus.UNAUTHORIZED, "만료된 단축 URL입니다."),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인증/인가와 비즈니스 검증은 분리되어야 된다고 생각 합니다.

  • 저는 유효 시간이 지난 url 에 접근할 수 없다. 라는 건 비즈니스 정책이라고 생각 합니다.


@Component
@Slf4j
@EnableScheduling
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스케줄러의 위험성은 무엇이 있을까요?

Copy link
Collaborator Author

@seeeeeeong seeeeeeong Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

작업이 누락되거나 리소스가 과부화 될 수 있는 문제점이 있습니다. 만료된 URL이 존재하는 여부만 확인하는 것으로 변경하였습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 어떤 경우에 누락될 수 있나요?
  • 어떤 경우에 과부하 될 수 있나요?
  • 서버가 2대면 어떻게 되나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여러 작업이 동시에 실행 될 때, 많은 작업을 처리할 때 또는 외부 시스템과 연동된 작업일 경우 외부 시스템에서 장애가 발생했을 때 문제가 될 수 있습니다.

서버가 2개일 경우 스케줄링 작업이 중복 실행될 수 있습니다. 하나의 서버에서만 작업을 처리하도록 설정이 필요할 것 같습니다.

Comment on lines 28 to 32
try {
urlShortenService.deleteByExpiredAtBefore(LocalDateTime.now());
} catch (Exception e) {
log.error("{} - Error occurred: {}", batchName, e.getMessage(), e);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit 저는 코드 중간에 try-catch 사용을 를 별로 좋아하지 않습니다.

  • Exception 을 catch 하는거면 global handler 에서 하는 것과 어떤 차이가 있나요?


import java.time.LocalDateTime;

public class UrlShorten {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getter 밖에 없다면 record 를 사용할 수도 있지 않을까요?

public class UrlShorten {

private String originUrl;
private String shortenUrlKey;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit id, key

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id나 key 필드를 추가해서 관리하라는 말씀이실까요?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

대리키를 두는 방법도 있다는 뜻 입니다. 사용할지 여부는 개발자의 판단 입니다. nit

@@ -22,13 +25,26 @@ public UrlShortenService(UrlShortenValidator urlShortenValidator,
public String createShortenUrl(String originUrl) {
urlShortenValidator.validateUrl(originUrl);
String shortenUrlKey = UrlShortener.shorten();
urlShortenRepository.save(shortenUrlKey, originUrl);

UrlShorten urlShorten = new UrlShorten(originUrl, shortenUrlKey, LocalDateTime.now().plusMinutes(1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정책이 바뀌어서 2분이 되면 어떻게 되나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request에서 만료시간을 받거나 yml로 관리하는 방법으로 변경할 것 같습니다.

Comment on lines 39 to 41
if (urlShorten.getExpiredAt().isBefore(LocalDateTime.now())) {
throw new UnAuthorizedException(ErrorCode.EXPIRED_SHORTEN_URL);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스케줄러의 로직을 보면 hard delete 되고 있습니다.
저는 이런 경우에는 존재만 하면 정상이라고 간주하곤 합니다.

@Test
void 잘못된_url일_경우_예외가_발생한다() {
String originUrl = "ftp://www.google.com";
String originUrl = "htp://www.google.com";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ftp 형태는 테스트 대상에서 배제 했나요?

  • fyi. @ParameterizedTest, Faker

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ftp의 경우 예외 케이스가 아니여서 수정하였습니다.
ParameterizedTest를 추가하였습니다.

@seeeeeeong seeeeeeong requested a review from Hyune-c February 8, 2025 18:57

public void setExpirationTime(int expirationTime) {
this.expirationTime = expirationTime;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi. duration

Arguments.of(Named.of("http가 누락된 URL", "://google.com"), ValidationException.class, ErrorCode.INVALID_URL_FORMAT.getMessage()),
Arguments.of(Named.of("잘못된 프로토콜", "htp://example.com"), ValidationException.class, ErrorCode.INVALID_URL_FORMAT.getMessage()),
Arguments.of(Named.of("차단된 URL", "https://www.example.com"), ValidationException.class, ErrorCode.BLOCKED_URL.getMessage())
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit 저는 MethodSource 는 잘 안씁니다. 너무 장황해져서 입니다. 주로 CsvSource 정도만 씁니다.


@Component
@Slf4j
@EnableScheduling
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 어떤 경우에 누락될 수 있나요?
  • 어떤 경우에 과부하 될 수 있나요?
  • 서버가 2대면 어떻게 되나요?

public class UrlShorten {

private String originUrl;
private String shortenUrlKey;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

대리키를 두는 방법도 있다는 뜻 입니다. 사용할지 여부는 개발자의 판단 입니다. nit

@seeeeeeong seeeeeeong merged commit f5e8077 into whatever-mentoring:seeeeeeong Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants