refactor(server): decouple services and improve architecture
- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion. - Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects. - Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management. - Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering. - Improve database initialization by moving it from `internal/model` to `internal/database`. - Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool. - Enhance error handling and security by implementing fail-fast checks for encryption key length during startup. - Clean up unused code, including the `avatar` package and several unused DTOs.
This commit is contained in:
@@ -38,14 +38,14 @@ type PushStreamMsg struct {
|
||||
|
||||
// PushWorker 从 Redis Stream 消费消息并异步推送
|
||||
type PushWorker struct {
|
||||
rdb *redis.Client
|
||||
publisher ws.MessagePublisher
|
||||
pushSvc PushService
|
||||
msgRepo repository.MessageRepository
|
||||
userRepo repository.UserRepository
|
||||
config PushWorkerConfig
|
||||
stopCh chan struct{}
|
||||
doneCh chan struct{}
|
||||
rdb *redis.Client
|
||||
publisher ws.MessagePublisher
|
||||
pushSvc PushService
|
||||
msgRepo repository.MessageRepository
|
||||
userRepo repository.UserRepository
|
||||
config PushWorkerConfig
|
||||
stopCh chan struct{}
|
||||
doneCh chan struct{}
|
||||
}
|
||||
|
||||
// NewPushWorker 创建推送 Worker
|
||||
@@ -79,14 +79,14 @@ func NewPushWorker(
|
||||
config.IdleTimeoutMs = 30000
|
||||
}
|
||||
return &PushWorker{
|
||||
rdb: rdb,
|
||||
publisher: publisher,
|
||||
pushSvc: pushSvc,
|
||||
msgRepo: msgRepo,
|
||||
userRepo: userRepo,
|
||||
config: config,
|
||||
stopCh: make(chan struct{}),
|
||||
doneCh: make(chan struct{}),
|
||||
rdb: rdb,
|
||||
publisher: publisher,
|
||||
pushSvc: pushSvc,
|
||||
msgRepo: msgRepo,
|
||||
userRepo: userRepo,
|
||||
config: config,
|
||||
stopCh: make(chan struct{}),
|
||||
doneCh: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,12 +191,12 @@ func (w *PushWorker) claimPendingLoop() {
|
||||
|
||||
// 查询 pending 消息
|
||||
pendingResult, err := w.rdb.XPendingExt(context.Background(), &redis.XPendingExtArgs{
|
||||
Stream: w.config.Stream,
|
||||
Group: w.config.Group,
|
||||
Start: "-",
|
||||
End: "+",
|
||||
Count: int64(w.config.BatchSize),
|
||||
Idle: time.Duration(w.config.IdleTimeoutMs) * time.Millisecond,
|
||||
Stream: w.config.Stream,
|
||||
Group: w.config.Group,
|
||||
Start: "-",
|
||||
End: "+",
|
||||
Count: int64(w.config.BatchSize),
|
||||
Idle: time.Duration(w.config.IdleTimeoutMs) * time.Millisecond,
|
||||
}).Result()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
@@ -213,7 +213,7 @@ func (w *PushWorker) claimPendingLoop() {
|
||||
Group: w.config.Group,
|
||||
Consumer: consumerName,
|
||||
MinIdle: time.Duration(w.config.IdleTimeoutMs) * time.Millisecond,
|
||||
Messages: []string{pending.ID},
|
||||
Messages: []string{pending.ID},
|
||||
}).Result()
|
||||
if err != nil || len(claimed) == 0 {
|
||||
continue
|
||||
@@ -378,4 +378,4 @@ func PublishToPush(ctx context.Context, rdb *redis.Client, stream string, msg *P
|
||||
"data": string(data),
|
||||
},
|
||||
}).Err()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user