- 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
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
use crate::auth::auth_service_server::AuthService;
|
use crate::auth::auth_service_server::AuthService;
|
||||||
use crate::auth::{
|
use crate::auth::{
|
||||||
LoginRequest, LoginResponse, LogoutRequest, PasswordResetRequest, PasswordResetResponse,
|
LoginRequest, LoginResponse, LogoutRequest, PasswordResetRequest, PasswordResetResponse,
|
||||||
RegisterRequest, RegisterResponse, ResetPasswordRequest, ResetPasswordResponse,
|
RefreshSessionResponse, RegisterRequest, RegisterResponse, ResetPasswordRequest,
|
||||||
ValidateSessionRequest, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse,
|
ResetPasswordResponse, ValidateSessionRequest, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse,
|
||||||
};
|
};
|
||||||
use crate::common::Empty;
|
use crate::common::Empty;
|
||||||
use crate::database_client::{DatabaseClient, DatabaseClientTrait};
|
use crate::database_client::{DatabaseClient, DatabaseClientTrait};
|
||||||
@@ -160,7 +160,7 @@ impl AuthService for MyAuthService {
|
|||||||
async fn refresh_session(
|
async fn refresh_session(
|
||||||
&self,
|
&self,
|
||||||
request: Request<ValidateSessionRequest>,
|
request: Request<ValidateSessionRequest>,
|
||||||
) -> Result<Response<ValidateSessionResponse>, Status> {
|
) -> Result<Response<RefreshSessionResponse>, Status> {
|
||||||
let req = request.into_inner();
|
let req = request.into_inner();
|
||||||
let response = self
|
let response = self
|
||||||
.session_client
|
.session_client
|
||||||
@@ -175,11 +175,11 @@ impl AuthService for MyAuthService {
|
|||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let res = res.into_inner();
|
let res = res.into_inner();
|
||||||
debug!("Session valid: {:?}", res);
|
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(_) => {
|
Err(_) => {
|
||||||
debug!("Session invalid or not found");
|
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 }))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::auth::auth_service_client::AuthServiceClient;
|
use crate::auth::auth_service_client::AuthServiceClient;
|
||||||
use crate::auth::{
|
use crate::auth::{
|
||||||
LoginRequest, LoginResponse, LogoutRequest, ValidateSessionRequest, ValidateSessionResponse,
|
LoginRequest, LoginResponse, LogoutRequest, RefreshSessionResponse, ValidateSessionRequest,
|
||||||
ValidateTokenRequest, ValidateTokenResponse,
|
ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse,
|
||||||
};
|
};
|
||||||
use crate::common::Empty;
|
use crate::common::Empty;
|
||||||
use tonic::transport::Channel;
|
use tonic::transport::Channel;
|
||||||
@@ -60,7 +60,7 @@ impl AuthClient {
|
|||||||
pub async fn refresh_session(
|
pub async fn refresh_session(
|
||||||
&mut self,
|
&mut self,
|
||||||
session_id: &str,
|
session_id: &str,
|
||||||
) -> Result<ValidateSessionResponse, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<RefreshSessionResponse, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let request = ValidateSessionRequest {
|
let request = ValidateSessionRequest {
|
||||||
session_id: session_id.to_string(),
|
session_id: session_id.to_string(),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ service AuthService {
|
|||||||
rpc Logout(LogoutRequest) returns (common.Empty);
|
rpc Logout(LogoutRequest) returns (common.Empty);
|
||||||
rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse);
|
rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse);
|
||||||
rpc ValidateSession(ValidateSessionRequest) returns (ValidateSessionResponse);
|
rpc ValidateSession(ValidateSessionRequest) returns (ValidateSessionResponse);
|
||||||
rpc RefreshSession(ValidateSessionRequest) returns (ValidateSessionResponse);
|
rpc RefreshSession(ValidateSessionRequest) returns (RefreshSessionResponse);
|
||||||
rpc Register (RegisterRequest) returns (RegisterResponse);
|
rpc Register (RegisterRequest) returns (RegisterResponse);
|
||||||
rpc RequestPasswordReset (PasswordResetRequest) returns (PasswordResetResponse);
|
rpc RequestPasswordReset (PasswordResetRequest) returns (PasswordResetResponse);
|
||||||
rpc ResetPassword (ResetPasswordRequest) returns (ResetPasswordResponse);
|
rpc ResetPassword (ResetPasswordRequest) returns (ResetPasswordResponse);
|
||||||
@@ -51,6 +51,10 @@ message ValidateSessionResponse {
|
|||||||
string user_id = 3;
|
string user_id = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message RefreshSessionResponse {
|
||||||
|
bool valid = 1;
|
||||||
|
}
|
||||||
|
|
||||||
message RegisterRequest {
|
message RegisterRequest {
|
||||||
string username = 1;
|
string username = 1;
|
||||||
string email = 2;
|
string email = 2;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import "common.proto";
|
|||||||
service SessionService {
|
service SessionService {
|
||||||
rpc CreateSession (CreateSessionRequest) returns (SessionResponse);
|
rpc CreateSession (CreateSessionRequest) returns (SessionResponse);
|
||||||
rpc GetSession (GetSessionRequest) returns (SessionResponse);
|
rpc GetSession (GetSessionRequest) returns (SessionResponse);
|
||||||
rpc RefreshSession (GetSessionRequest) returns (SessionResponse);
|
rpc RefreshSession (GetSessionRequest) returns (RefreshSessionResponse);
|
||||||
rpc DeleteSession (DeleteSessionRequest) returns (common.Empty);
|
rpc DeleteSession (DeleteSessionRequest) returns (common.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,3 +36,7 @@ message SessionResponse {
|
|||||||
string ip_address = 6;
|
string ip_address = 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message RefreshSessionResponse {
|
||||||
|
bool valid = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ service WorldService {
|
|||||||
rpc GetCharacter(CharacterRequest) returns (CharacterResponse);
|
rpc GetCharacter(CharacterRequest) returns (CharacterResponse);
|
||||||
rpc ChangeMap(ChangeMapRequest) returns (ChangeMapResponse);
|
rpc ChangeMap(ChangeMapRequest) returns (ChangeMapResponse);
|
||||||
rpc MoveCharacter(CharacterMoveRequest) returns (CharacterMoveResponse);
|
rpc MoveCharacter(CharacterMoveRequest) returns (CharacterMoveResponse);
|
||||||
|
rpc GetTargetHp(ObjectHpRequest) returns (ObjectHpResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
message CharacterRequest {
|
message CharacterRequest {
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
use crate::api::session_service_server::SessionService;
|
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 crate::common::Empty;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tonic::{Request, Response, Status};
|
use tonic::{Request, Response, Status};
|
||||||
|
use tracing::debug;
|
||||||
use utils::redis_cache::{Cache, RedisCache};
|
use utils::redis_cache::{Cache, RedisCache};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
@@ -100,34 +101,25 @@ impl SessionService for SessionServiceImpl {
|
|||||||
|
|
||||||
Ok(Response::new(Empty {}))
|
Ok(Response::new(Empty {}))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn refresh_session(
|
async fn refresh_session(
|
||||||
&self,
|
&self,
|
||||||
request: Request<GetSessionRequest>,
|
request: Request<GetSessionRequest>,
|
||||||
) -> Result<Response<SessionResponse>, Status> {
|
) -> Result<Response<RefreshSessionResponse>, Status> {
|
||||||
let req = request.into_inner();
|
let req = request.into_inner();
|
||||||
let conn = self.redis.lock().await;
|
let conn = self.redis.lock().await;
|
||||||
|
match conn.refresh(&req.session_id, 300).await {
|
||||||
if let Some(session_data) = conn
|
Ok(Response) => {
|
||||||
.get::<String>(&req.session_id)
|
let response = RefreshSessionResponse {
|
||||||
.await
|
valid: true
|
||||||
.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,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
debug!("Session refreshed: {:?}", response);
|
||||||
Ok(Response::new(response))
|
Ok(Response::new(response))
|
||||||
} else {
|
}
|
||||||
Err(Status::not_found("Session not found"))
|
Err(e) => {
|
||||||
|
Err(Status::unknown(e.to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ pub trait Cache {
|
|||||||
) -> Result<Option<T>, redis::RedisError>;
|
) -> Result<Option<T>, redis::RedisError>;
|
||||||
|
|
||||||
async fn delete(&mut self, key: &str) -> redis::RedisResult<()>;
|
async fn delete(&mut self, key: &str) -> redis::RedisResult<()>;
|
||||||
|
|
||||||
|
async fn refresh(&self, key: &str, ttl: i64) -> redis::RedisResult<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RedisCache {
|
pub struct RedisCache {
|
||||||
@@ -137,4 +139,15 @@ impl Cache for RedisCache {
|
|||||||
})?;
|
})?;
|
||||||
conn.del(key).await
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user