From 14c6aa485a66b557e2509f69233608b597b7f9e0695342211c38fb3f6ee3565f Mon Sep 17 00:00:00 2001 From: RavenX8 <7156279+RavenX8@users.noreply.github.com> Date: Sun, 9 Mar 2025 17:08:56 -0400 Subject: [PATCH] - add: redis cache refresh function sets the ttl for a key - update: session service refresh session function now just updates the ttl of the session instead of calling set --- auth-service/src/grpc.rs | 10 ++-- packet-service/src/auth_client.rs | 6 +-- proto/auth.proto | 6 ++- proto/session_service_api.proto | 6 ++- proto/world.proto | 69 +++++++++++++------------- session-service/src/session_service.rs | 38 ++++++-------- utils/src/redis_cache.rs | 13 +++++ 7 files changed, 81 insertions(+), 67 deletions(-) diff --git a/auth-service/src/grpc.rs b/auth-service/src/grpc.rs index 1d98074..eeb1f2c 100644 --- a/auth-service/src/grpc.rs +++ b/auth-service/src/grpc.rs @@ -1,8 +1,8 @@ use crate::auth::auth_service_server::AuthService; use crate::auth::{ LoginRequest, LoginResponse, LogoutRequest, PasswordResetRequest, PasswordResetResponse, - RegisterRequest, RegisterResponse, ResetPasswordRequest, ResetPasswordResponse, - ValidateSessionRequest, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse, + RefreshSessionResponse, RegisterRequest, RegisterResponse, ResetPasswordRequest, + ResetPasswordResponse, ValidateSessionRequest, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse, }; use crate::common::Empty; use crate::database_client::{DatabaseClient, DatabaseClientTrait}; @@ -160,7 +160,7 @@ impl AuthService for MyAuthService { async fn refresh_session( &self, request: Request, - ) -> Result, Status> { + ) -> Result, Status> { let req = request.into_inner(); let response = self .session_client @@ -175,11 +175,11 @@ impl AuthService for MyAuthService { Ok(res) => { let res = res.into_inner(); debug!("Session valid: {:?}", res); - Ok(Response::new(ValidateSessionResponse { valid: true, session_id: res.session_id.to_string(), user_id: res.user_id.to_string() })) + Ok(Response::new(RefreshSessionResponse { valid: res.valid })) } Err(_) => { debug!("Session invalid or not found"); - Ok(Response::new(ValidateSessionResponse { valid: false, session_id: "".to_string(), user_id: "".to_string() })) + Ok(Response::new(RefreshSessionResponse { valid: false })) } } } diff --git a/packet-service/src/auth_client.rs b/packet-service/src/auth_client.rs index 083ea8b..abe70c2 100644 --- a/packet-service/src/auth_client.rs +++ b/packet-service/src/auth_client.rs @@ -1,7 +1,7 @@ use crate::auth::auth_service_client::AuthServiceClient; use crate::auth::{ - LoginRequest, LoginResponse, LogoutRequest, ValidateSessionRequest, ValidateSessionResponse, - ValidateTokenRequest, ValidateTokenResponse, + LoginRequest, LoginResponse, LogoutRequest, RefreshSessionResponse, ValidateSessionRequest, + ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse, }; use crate::common::Empty; use tonic::transport::Channel; @@ -60,7 +60,7 @@ impl AuthClient { pub async fn refresh_session( &mut self, session_id: &str, - ) -> Result> { + ) -> Result> { let request = ValidateSessionRequest { session_id: session_id.to_string(), }; diff --git a/proto/auth.proto b/proto/auth.proto index 3f45b4e..2b14938 100644 --- a/proto/auth.proto +++ b/proto/auth.proto @@ -9,7 +9,7 @@ service AuthService { rpc Logout(LogoutRequest) returns (common.Empty); rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse); rpc ValidateSession(ValidateSessionRequest) returns (ValidateSessionResponse); - rpc RefreshSession(ValidateSessionRequest) returns (ValidateSessionResponse); + rpc RefreshSession(ValidateSessionRequest) returns (RefreshSessionResponse); rpc Register (RegisterRequest) returns (RegisterResponse); rpc RequestPasswordReset (PasswordResetRequest) returns (PasswordResetResponse); rpc ResetPassword (ResetPasswordRequest) returns (ResetPasswordResponse); @@ -51,6 +51,10 @@ message ValidateSessionResponse { string user_id = 3; } +message RefreshSessionResponse { + bool valid = 1; +} + message RegisterRequest { string username = 1; string email = 2; diff --git a/proto/session_service_api.proto b/proto/session_service_api.proto index b65b27f..7737206 100644 --- a/proto/session_service_api.proto +++ b/proto/session_service_api.proto @@ -7,7 +7,7 @@ import "common.proto"; service SessionService { rpc CreateSession (CreateSessionRequest) returns (SessionResponse); rpc GetSession (GetSessionRequest) returns (SessionResponse); - rpc RefreshSession (GetSessionRequest) returns (SessionResponse); + rpc RefreshSession (GetSessionRequest) returns (RefreshSessionResponse); rpc DeleteSession (DeleteSessionRequest) returns (common.Empty); } @@ -36,3 +36,7 @@ message SessionResponse { string ip_address = 6; } +message RefreshSessionResponse { + bool valid = 1; +} + diff --git a/proto/world.proto b/proto/world.proto index 1772791..4974293 100644 --- a/proto/world.proto +++ b/proto/world.proto @@ -3,65 +3,66 @@ syntax = "proto3"; package world; service WorldService { - rpc GetCharacter(CharacterRequest) returns (CharacterResponse); - rpc ChangeMap(ChangeMapRequest) returns (ChangeMapResponse); - rpc MoveCharacter(CharacterMoveRequest) returns (CharacterMoveResponse); + rpc GetCharacter(CharacterRequest) returns (CharacterResponse); + rpc ChangeMap(ChangeMapRequest) returns (ChangeMapResponse); + rpc MoveCharacter(CharacterMoveRequest) returns (CharacterMoveResponse); + rpc GetTargetHp(ObjectHpRequest) returns (ObjectHpResponse); } message CharacterRequest { - string token = 1; - string user_id = 2; - string char_id = 3; - string session_id = 4; + string token = 1; + string user_id = 2; + string char_id = 3; + string session_id = 4; } message CharacterResponse { - int32 count = 1; + int32 count = 1; } message CharacterMoveRequest { - string session_id = 1; - uint32 target_id = 2; - float x = 3; - float y = 4; - float z = 5; + string session_id = 1; + uint32 target_id = 2; + float x = 3; + float y = 4; + float z = 5; } message CharacterMoveResponse { - int32 id = 1; - int32 target_id = 2; - int32 distance = 3; - float x = 4; - float y = 5; - float z = 6; + int32 id = 1; + int32 target_id = 2; + int32 distance = 3; + float x = 4; + float y = 5; + float z = 6; } message ChangeMapRequest { - int32 id = 1; - float x = 2; - float y = 3; + int32 id = 1; + float x = 2; + float y = 3; } message ChangeMapResponse { - int32 id = 1; - int32 map_id = 2; - float x = 3; - float y = 4; - int32 move_mode = 5; - int32 ride_mode = 6; + int32 id = 1; + int32 map_id = 2; + float x = 3; + float y = 4; + int32 move_mode = 5; + int32 ride_mode = 6; } message AttackRequest { - string session_id = 1; - uint32 target_id = 2; + string session_id = 1; + uint32 target_id = 2; } message ObjectHpRequest { - string session_id = 1; - uint32 target_id = 2; + string session_id = 1; + uint32 target_id = 2; } message ObjectHpResponse { - uint32 target_id = 1; - int32 hp = 2; + uint32 target_id = 1; + int32 hp = 2; } diff --git a/session-service/src/session_service.rs b/session-service/src/session_service.rs index 6925286..9e8d0ea 100644 --- a/session-service/src/session_service.rs +++ b/session-service/src/session_service.rs @@ -1,10 +1,11 @@ use crate::api::session_service_server::SessionService; -use crate::api::{CreateSessionRequest, DeleteSessionRequest, GetSessionRequest, SessionResponse}; +use crate::api::{CreateSessionRequest, DeleteSessionRequest, GetSessionRequest, RefreshSessionResponse, SessionResponse}; use crate::common::Empty; use serde::{Deserialize, Serialize}; use std::sync::Arc; use tokio::sync::Mutex; use tonic::{Request, Response, Status}; +use tracing::debug; use utils::redis_cache::{Cache, RedisCache}; #[derive(Serialize, Deserialize, Debug)] @@ -100,34 +101,25 @@ impl SessionService for SessionServiceImpl { Ok(Response::new(Empty {})) } + async fn refresh_session( &self, request: Request, - ) -> Result, Status> { + ) -> Result, Status> { let req = request.into_inner(); let conn = self.redis.lock().await; + match conn.refresh(&req.session_id, 300).await { + Ok(Response) => { + let response = RefreshSessionResponse { + valid: true + }; - if let Some(session_data) = conn - .get::(&req.session_id) - .await - .map_err(|_| Status::internal("Failed to fetch session from Redis"))? - { - let _ = conn.update(&req.session_id, Some(&session_data), Some(300)); - let session: Session = serde_json::from_str(&session_data) - .map_err(|_| Status::internal("Failed to deserialize session"))?; - - let response = SessionResponse { - session_id: req.session_id, - user_id: session.user_id, - username: session.username, - character_id: session.character_id, - login_time: session.login_time, - ip_address: session.ip_address, - }; - - Ok(Response::new(response)) - } else { - Err(Status::not_found("Session not found")) + debug!("Session refreshed: {:?}", response); + Ok(Response::new(response)) + } + Err(e) => { + Err(Status::unknown(e.to_string())) + } } } } diff --git a/utils/src/redis_cache.rs b/utils/src/redis_cache.rs index 142846c..ae63dcf 100644 --- a/utils/src/redis_cache.rs +++ b/utils/src/redis_cache.rs @@ -25,6 +25,8 @@ pub trait Cache { ) -> Result, redis::RedisError>; async fn delete(&mut self, key: &str) -> redis::RedisResult<()>; + + async fn refresh(&self, key: &str, ttl: i64) -> redis::RedisResult<()>; } pub struct RedisCache { @@ -137,4 +139,15 @@ impl Cache for RedisCache { })?; conn.del(key).await } + + async fn refresh(&self, key: &str, ttl: i64) -> redis::RedisResult<()> { + let mut conn = self.pool.get().await.map_err(|err| { + redis::RedisError::from(( + redis::ErrorKind::IoError, + "Failed to get Redis connection", + format!("{:?}", err), + )) + })?; + conn.expire(key, ttl).await + } }