feat(ws): implement WebSocket cluster mode with Redis Pub/Sub
All checks were successful
Build Backend / build (push) Successful in 2m0s
Build Backend / build-docker (push) Successful in 1m26s

Introduce a new WebSocket messaging architecture that supports both standalone and cluster modes. This allows for horizontal scaling of WebSocket servers by using Redis Pub/Sub to synchronize messages across multiple instances.

Key changes:
- Added `ws.MessagePublisher` interface to abstract message distribution.
- Implemented `ws.Bus` to handle cluster-mode messaging via Redis.
- Added `ws.OnlineTracker` to manage user online status across the cluster.
- Refactored multiple services (Chat, Group, Push, Call, etc.) to use the new `MessagePublisher` instead of a concrete `ws.Hub`.
- Added WebSocket configuration options (mode, instance ID, channel, TTL, heartbeat) to `config.yaml` and `config.go`.
- Updated dependency injection with Wire to support the new publisher and Redis client.
- Improved logging by replacing standard `log` with `zap` in several service components.
This commit is contained in:
2026-05-06 12:39:11 +08:00
parent d481742790
commit c630cbf4d0
22 changed files with 1082 additions and 120 deletions

View File

@@ -1,4 +1,4 @@
// Code generated by Wire. DO NOT EDIT.
// Code generated by Wire. DO NOT EDIT.
//go:generate go run -mod=mod github.com/google/wire/cmd/wire
//go:build !wireinject
@@ -31,12 +31,12 @@ func InitializeApp() (*App, error) {
pushRecordRepository := repository.NewPushRecordRepository(db)
deviceTokenRepository := repository.NewDeviceTokenRepository(db)
messageRepository := repository.NewMessageRepository(db)
hub := wire.ProvideWSHub()
client := wire.ProvideRedisClient(config)
messagePublisher := wire.ProvideWSMessagePublisher(config, client)
logger := wire.ProvideLogger()
jpushClient := wire.ProvideJPushClient(config, logger)
pushService := wire.ProvidePushService(pushRecordRepository, deviceTokenRepository, messageRepository, hub, jpushClient)
pushService := wire.ProvidePushService(pushRecordRepository, deviceTokenRepository, messageRepository, messagePublisher, jpushClient)
postRepository := repository.NewPostRepository(db)
client := wire.ProvideRedisClient(config)
cache := wire.ProvideCache(config, client)
systemMessageService := wire.ProvideSystemMessageService(systemNotificationRepository, pushService, userRepository, postRepository, cache)
emailClient := wire.ProvideEmailClient(config)
@@ -66,10 +66,10 @@ func InitializeApp() (*App, error) {
transactionManager := wire.ProvideTransactionManager(db)
builtinHooks := wire.ProvideBuiltinHooks(manager)
postService := wire.ProvidePostService(postRepository, systemMessageService, postAIService, cache, transactionManager, manager, moderationHooks, builtinHooks, logService)
channelRepository := repository.NewChannelRepository(db)
channelService := wire.ProvideChannelService(channelRepository, cache)
postRefRepository := repository.NewPostRefRepository(db)
postRefService := wire.ProvidePostRefService(postRefRepository, postRepository)
channelRepository := repository.NewChannelRepository(db)
channelService := wire.ProvideChannelService(channelRepository, cache)
postHandler := handler.NewPostHandler(postService, postRefService, userService, channelService)
commentService := wire.ProvideCommentService(commentRepository, postRepository, systemMessageService, postAIService, cache, manager, logService)
commentHandler := handler.NewCommentHandler(commentService)
@@ -78,12 +78,12 @@ func InitializeApp() (*App, error) {
return nil, err
}
uploadService := wire.ProvideUploadService(s3Client, userService)
chatService := wire.ProvideChatService(messageRepository, userRepository, hub, cache, uploadService, pushService)
chatService := wire.ProvideChatService(messageRepository, userRepository, messagePublisher, cache, uploadService, pushService)
messageService := wire.ProvideMessageService(messageRepository, cache, uploadService)
groupRepository := repository.NewGroupRepository(db)
groupJoinRequestRepository := repository.NewGroupJoinRequestRepository(db)
groupService := wire.ProvideGroupService(groupRepository, userRepository, messageRepository, groupJoinRequestRepository, systemNotificationRepository, hub, cache)
messageHandler := wire.ProvideMessageHandler(chatService, messageService, userService, groupService, hub)
groupService := wire.ProvideGroupService(groupRepository, userRepository, messageRepository, groupJoinRequestRepository, systemNotificationRepository, messagePublisher, cache)
messageHandler := wire.ProvideMessageHandler(chatService, messageService, userService, groupService, messagePublisher)
notificationRepository := repository.NewNotificationRepository(db)
notificationService := wire.ProvideNotificationService(notificationRepository, cache)
notificationHandler := handler.NewNotificationHandler(notificationService)
@@ -121,14 +121,14 @@ func InitializeApp() (*App, error) {
adminDashboardService := wire.ProvideAdminDashboardService(db, userRepository, postRepository, commentRepository, groupRepository, userActivityRepository)
adminDashboardHandler := handler.NewAdminDashboardHandler(adminDashboardService)
adminLogHandler := handler.NewAdminLogHandler(operationLogService, loginLogService, dataChangeLogService)
qrCodeLoginService := wire.ProvideQRCodeLoginService(client, hub, jwtService, userService, userActivityService, logService)
qrCodeLoginService := wire.ProvideQRCodeLoginService(client, messagePublisher, jwtService, userService, userActivityService, logService)
qrCodeHandler := handler.NewQRCodeHandler(qrCodeLoginService)
materialSubjectRepository := repository.NewMaterialSubjectRepository(db)
materialFileRepository := repository.NewMaterialFileRepository(db)
materialService := wire.ProvideMaterialService(materialSubjectRepository, materialFileRepository)
materialHandler := handler.NewMaterialHandler(materialService)
callRepository := repository.NewCallRepository(db)
callService := wire.ProvideCallService(callRepository, hub, config, db)
callService := wire.ProvideCallService(callRepository, messagePublisher, config, db, client)
callHandler := handler.NewCallHandler(callService)
reportRepository := repository.NewReportRepository(db)
reportService := wire.ProvideReportService(reportRepository, postRepository, commentRepository, messageRepository, userRepository, systemNotificationRepository, pushService, transactionManager, operationLogService, config)
@@ -143,13 +143,10 @@ func InitializeApp() (*App, error) {
adminProfileAuditHandler := handler.NewAdminProfileAuditHandler(userProfileAuditService)
setupService := wire.ProvideSetupService(userRepository, roleRepository, casbinService, config)
setupHandler := wire.ProvideSetupHandler(setupService)
wsHandler := wire.ProvideWSHandler(hub, chatService, groupService, jwtService, callService, userRepository)
tradeRepository := repository.NewTradeRepository(db)
tradeService := wire.ProvideTradeService(tradeRepository)
tradeHandler := handler.NewTradeHandler(tradeService)
router := ProvideRouter(userRepository, userHandler, postHandler, commentHandler, messageHandler, notificationHandler, uploadHandler, jwtService, pushHandler, systemMessageHandler, groupHandler, stickerHandler, voteHandler, channelHandler, scheduleHandler, roleHandler, adminUserHandler, adminPostHandler, adminCommentHandler, adminGroupHandler, adminDashboardHandler, adminLogHandler, qrCodeHandler, materialHandler, callHandler, reportHandler, adminReportHandler, verificationHandler, adminVerificationHandler, adminProfileAuditHandler, setupHandler, tradeHandler, logService, userActivityService, casbinService, wsHandler)
wsHandler := wire.ProvideWSHandler(messagePublisher, chatService, groupService, jwtService, callService, userRepository)
router := ProvideRouter(userRepository, userHandler, postHandler, commentHandler, messageHandler, notificationHandler, uploadHandler, jwtService, pushHandler, systemMessageHandler, groupHandler, stickerHandler, voteHandler, channelHandler, scheduleHandler, roleHandler, adminUserHandler, adminPostHandler, adminCommentHandler, adminGroupHandler, adminDashboardHandler, adminLogHandler, qrCodeHandler, materialHandler, callHandler, reportHandler, adminReportHandler, verificationHandler, adminVerificationHandler, adminProfileAuditHandler, setupHandler, logService, userActivityService, casbinService, wsHandler)
hotRankWorker := wire.ProvideHotRankWorker(config, postRepository, channelRepository, cache)
app := NewApp(config, db, router, pushService, hotRankWorker, server, hub, logger)
app := NewApp(config, db, router, pushService, hotRankWorker, server, messagePublisher, logger)
return app, nil
}
@@ -188,7 +185,6 @@ func ProvideRouter(
adminVerificationHandler *handler.AdminVerificationHandler,
adminProfileAuditHandler *handler.AdminProfileAuditHandler,
setupHandler *handler.SetupHandler,
tradeHandler *handler.TradeHandler,
logService *service.LogService,
activityService service.UserActivityService,
casbinService service.CasbinService,
@@ -226,7 +222,6 @@ func ProvideRouter(
AdminVerificationHandler: adminVerificationHandler,
AdminProfileAuditHandler: adminProfileAuditHandler,
SetupHandler: setupHandler,
TradeHandler: tradeHandler,
LogService: logService,
ActivityService: activityService,
CasbinService: casbinService,