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

[bug] redis accessTokenMap 만료시간 문제 #63

Closed
yonghwankim-dev opened this issue Dec 6, 2023 · 0 comments · Fixed by #64
Closed

[bug] redis accessTokenMap 만료시간 문제 #63

yonghwankim-dev opened this issue Dec 6, 2023 · 0 comments · Fixed by #64
Assignees
Labels
bug Something isn't working

Comments

@yonghwankim-dev
Copy link
Member

yonghwankim-dev commented Dec 6, 2023

상황

redis에 저장되는 데이터중 kis open api 서버의 accessToken을 하루동안 보관하고 있습니다. 그러나 다음 사진과 같이 access_token_token_expired의 값이 만료되었음에도 불구하고 데이터가 살아있는 것을 확인하였습니다.

image

위와 같은 이유로 로컬 환경에서 서버 실행시 만료된 accessTokenMap이 살아있어서 현재가 및 종가를 갱신하지 못하는 상황입니다.

redis 터미널에서 TTL 명령어로 수명을 확인했을때 7290초의 수명이 남은 것을 확인할 수 있었습니다.
image

원인

kis open api 서버로부터 발급받은 액세스 토큰의 만료시간은 발급받은 시점으로부터 22시간동안 유지되지만 만료초(expires_in)은 24시간(86400)이다. 그런데 KisRedisService 객체는 accessTokenMap을 저장시 expires_in 데이터를 가지고 저장하기 때문에 토큰이 만료되었음에도 불구하고 2시간동안 살아있기 때문에 갱신이 안되었던 것이다.

public void setAccessTokenMap(Map<String, Object> accessTokenMap) {
   	long expiresIn = ((Integer)accessTokenMap.get("expires_in")).longValue();
   	String json;
   	try {
   		json = objectMapper.writeValueAsString(accessTokenMap);
   	} catch (JsonProcessingException e) {
   		throw new RuntimeException(e);
   	}
   	redisTemplate.opsForValue().set(ACCESS_TOKEN_MAP_KEY, json, Duration.ofSeconds(expiresIn));
   }

해결방법

  • accessTokenMap을 redis에 access_token_token_expired 프로퍼티의 날짜까지만 저장하도록 변경합니다.

public void setAccessTokenMap(KisAccessToken accessToken, LocalDateTime now) {
try {
redisTemplate.opsForValue().set(ACCESS_TOKEN_MAP_KEY,
ObjectMapperUtil.serialize(accessToken),
accessToken.betweenSecondFrom(now));
} catch (RedisSystemException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
}

KisAccessToken 인스턴스의 betweenSecondFrom 메서드 내용은 다음과 같습니다.

public Duration betweenSecondFrom(LocalDateTime localDateTime) {
return Duration.ofSeconds(Duration.between(localDateTime, accessTokenExpired).toSeconds());
}

@yonghwankim-dev yonghwankim-dev added the bug Something isn't working label Dec 6, 2023
@yonghwankim-dev yonghwankim-dev added this to the [BE] Sprint #9 milestone Dec 6, 2023
@yonghwankim-dev yonghwankim-dev self-assigned this Dec 6, 2023
@yonghwankim-dev yonghwankim-dev linked a pull request Dec 6, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant