- 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::{
|
||||
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<ValidateSessionRequest>,
|
||||
) -> Result<Response<ValidateSessionResponse>, Status> {
|
||||
) -> Result<Response<RefreshSessionResponse>, 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 }))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ValidateSessionResponse, Box<dyn std::error::Error + Send + Sync>> {
|
||||
) -> Result<RefreshSessionResponse, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let request = ValidateSessionRequest {
|
||||
session_id: session_id.to_string(),
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<GetSessionRequest>,
|
||||
) -> Result<Response<SessionResponse>, Status> {
|
||||
) -> Result<Response<RefreshSessionResponse>, 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::<String>(&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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ pub trait Cache {
|
||||
) -> Result<Option<T>, 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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user