refactor: improve system stability, performance, and code structure
All checks were successful
Build Backend / build (push) Successful in 3m2s
Build Backend / build-docker (push) Successful in 2m44s

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:
2026-05-04 13:07:03 +08:00
parent b2b55ea52d
commit ee78071d4d
65 changed files with 1293 additions and 975 deletions

View File

@@ -1,6 +1,8 @@
package repository
import (
"errors"
"with_you/internal/model"
"gorm.io/gorm"
@@ -11,6 +13,7 @@ type GroupJoinRequestRepository interface {
GetByFlag(flag string) (*model.GroupJoinRequest, error)
Update(req *model.GroupJoinRequest) error
GetPendingByGroupAndTarget(groupID, targetUserID string, reqType model.GroupJoinRequestType) (*model.GroupJoinRequest, error)
GetPendingByGroupAndTargets(groupID string, targetUserIDs []string, reqType model.GroupJoinRequestType) (map[string]bool, error)
}
type groupJoinRequestRepository struct {
@@ -48,3 +51,25 @@ func (r *groupJoinRequestRepository) GetPendingByGroupAndTarget(groupID, targetU
}
return &req, nil
}
func (r *groupJoinRequestRepository) GetPendingByGroupAndTargets(groupID string, targetUserIDs []string, reqType model.GroupJoinRequestType) (map[string]bool, error) {
result := make(map[string]bool)
if len(targetUserIDs) == 0 {
return result, nil
}
var existingIDs []string
err := r.db.Model(&model.GroupJoinRequest{}).
Where("group_id = ? AND target_user_id IN ? AND request_type = ? AND status = ?",
groupID, targetUserIDs, reqType, model.GroupJoinRequestStatusPending).
Pluck("target_user_id", &existingIDs).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return result, nil
}
return nil, err
}
for _, id := range existingIDs {
result[id] = true
}
return result, nil
}

View File

@@ -21,6 +21,7 @@ type GroupRepository interface {
// 群成员操作
AddMember(member *model.GroupMember) error
BatchAddMembers(groupID string, members []*model.GroupMember) error
GetMember(groupID string, userID string) (*model.GroupMember, error)
GetMembers(groupID string, page, pageSize int) ([]model.GroupMember, int64, error)
UpdateMember(member *model.GroupMember) error
@@ -53,6 +54,9 @@ type GroupRepository interface {
GetUserGroupsByCursor(ctx context.Context, userID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Group], error)
GetMembersByCursor(ctx context.Context, groupID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.GroupMember], error)
GetAnnouncementsByCursor(ctx context.Context, groupID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.GroupAnnouncement], error)
// 批量操作
GetExistingMembers(groupID string, userIDs []string) (map[string]bool, error)
}
// GroupMemberWithUser 群成员带用户信息的结构
@@ -155,12 +159,24 @@ func (r *groupRepository) AddMember(member *model.GroupMember) error {
if err := tx.Create(member).Error; err != nil {
return err
}
// 更新群组成员数量
return tx.Model(&model.Group{}).Where("id = ?", member.GroupID).
Update("member_count", gorm.Expr("member_count + ?", 1)).Error
})
}
func (r *groupRepository) BatchAddMembers(groupID string, members []*model.GroupMember) error {
if len(members) == 0 {
return nil
}
return r.db.Transaction(func(tx *gorm.DB) error {
if err := tx.CreateInBatches(members, 100).Error; err != nil {
return err
}
return tx.Model(&model.Group{}).Where("id = ?", groupID).
Update("member_count", gorm.Expr("member_count + ?", len(members))).Error
})
}
// GetMember 获取群成员
func (r *groupRepository) GetMember(groupID string, userID string) (*model.GroupMember, error) {
var member model.GroupMember