refactor: improve system stability, performance, and code structure
This commit introduces several architectural improvements and optimizations across the codebase: - **Performance & Reliability**: - Implemented Redis pipelining in `ConversationCache.CacheMessage` to reduce network round-trips. - Added a circuit breaker to the JPush client to prevent cascading failures. - Introduced batch deletion and batch member addition capabilities in repositories. - Added message idempotency support using `client_msg_id` and a Redis-based cache. - Optimized WebSocket handling with connection limits (total and per-user) and improved error logging. - **Code Refactoring**: - Refactored `Router` to use a `RouterDeps` struct, simplifying the constructor and improving maintainability. - Unified model ID generation logic using new `id_helper.go` (supporting UUID and Snowflake). - Standardized JSON serialization/deserialization in models using `json_helper.go`. - Refactored DTO conversion logic, specifically for `UserResponse` (using functional options) and `Report` responses. - Removed redundant/deprecated DTOs like `PostDetailResponse` and `TradeItemDetailResponse`. - **Cache Improvements**: - Enhanced `LayeredCache` with `SetRaw` to avoid double-encoding when promoting values from Redis to local cache. - Added `DeleteBatch` support to the cache interface. - **Other Changes**: - Cleaned up `config.go` by removing redundant default values and explicit environment variable overrides. - Improved WebSocket registration flow to handle connection limits gracefully.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"with_you/internal/config"
|
||||
"with_you/internal/grpc/runner"
|
||||
"with_you/internal/pkg/ws"
|
||||
"with_you/internal/router"
|
||||
"with_you/internal/service"
|
||||
|
||||
@@ -24,6 +25,7 @@ type App struct {
|
||||
HotRankWorker *service.HotRankWorker
|
||||
GRPCServer *runner.Server
|
||||
Server *http.Server
|
||||
WsHub *ws.Hub
|
||||
Logger *zap.Logger
|
||||
}
|
||||
|
||||
@@ -35,6 +37,7 @@ func NewApp(
|
||||
pushService service.PushService,
|
||||
hotRankWorker *service.HotRankWorker,
|
||||
grpcServer *runner.Server,
|
||||
wsHub *ws.Hub,
|
||||
logger *zap.Logger,
|
||||
) *App {
|
||||
return &App{
|
||||
@@ -44,6 +47,7 @@ func NewApp(
|
||||
PushService: pushService,
|
||||
HotRankWorker: hotRankWorker,
|
||||
GRPCServer: grpcServer,
|
||||
WsHub: wsHub,
|
||||
Logger: logger,
|
||||
}
|
||||
}
|
||||
@@ -97,7 +101,13 @@ func (a *App) Start(ctx context.Context) error {
|
||||
func (a *App) Stop(ctx context.Context) error {
|
||||
a.Logger.Info("Shutting down server")
|
||||
|
||||
// 1. 停止 HTTP 服务器
|
||||
// 1. 关闭所有 WebSocket 连接
|
||||
if a.WsHub != nil {
|
||||
a.Logger.Info("Closing WebSocket connections")
|
||||
a.WsHub.CloseAllConnections()
|
||||
}
|
||||
|
||||
// 3. 停止 HTTP 服务器
|
||||
if a.Server != nil {
|
||||
a.Logger.Info("Stopping HTTP server")
|
||||
if err := a.Server.Shutdown(ctx); err != nil {
|
||||
@@ -107,13 +117,13 @@ func (a *App) Stop(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 停止 gRPC 服务器
|
||||
// 4. 停止 gRPC 服务器
|
||||
if a.GRPCServer != nil {
|
||||
a.Logger.Info("Stopping gRPC server")
|
||||
a.GRPCServer.Stop()
|
||||
}
|
||||
|
||||
// 3. 停止推送 Worker
|
||||
// 5. 停止推送 Worker
|
||||
a.Logger.Info("Stopping push worker")
|
||||
a.PushService.StopPushWorker()
|
||||
|
||||
@@ -122,7 +132,7 @@ func (a *App) Stop(ctx context.Context) error {
|
||||
a.HotRankWorker.Stop()
|
||||
}
|
||||
|
||||
// 4. 关闭数据库连接
|
||||
// 6. 关闭数据库连接
|
||||
a.Logger.Info("Closing database connection")
|
||||
sqlDB, err := a.DB.DB()
|
||||
if err == nil {
|
||||
|
||||
Reference in New Issue
Block a user