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

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -81,9 +80,7 @@ type AuditLog struct {
// BeforeCreate 创建前生成UUID
func (al *AuditLog) BeforeCreate(tx *gorm.DB) error {
if al.ID == "" {
al.ID = uuid.New().String()
}
SetUUIDIfEmpty(&al.ID)
return nil
}

View File

@@ -1,12 +1,9 @@
package model
import (
"strconv"
"time"
"gorm.io/gorm"
"with_you/internal/pkg/utils"
)
// CallType 通话类型
@@ -60,14 +57,7 @@ type CallSession struct {
// BeforeCreate 创建前生成雪花算法ID
func (c *CallSession) BeforeCreate(tx *gorm.DB) error {
if c.ID == "" {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
c.ID = strconv.FormatInt(id, 10)
}
return nil
return SetSnowflakeStringID(&c.ID)
}
func (CallSession) TableName() string {

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -20,9 +19,7 @@ type Channel struct {
}
func (c *Channel) BeforeCreate(tx *gorm.DB) error {
if c.ID == "" {
c.ID = uuid.New().String()
}
SetUUIDIfEmpty(&c.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -53,9 +52,7 @@ type Comment struct {
// BeforeCreate 创建前生成UUID
func (c *Comment) BeforeCreate(tx *gorm.DB) error {
if c.ID == "" {
c.ID = uuid.New().String()
}
SetUUIDIfEmpty(&c.ID)
return nil
}
@@ -73,9 +70,7 @@ type CommentLike struct {
// BeforeCreate 创建前生成UUID
func (cl *CommentLike) BeforeCreate(tx *gorm.DB) error {
if cl.ID == "" {
cl.ID = uuid.New().String()
}
SetUUIDIfEmpty(&cl.ID)
return nil
}

View File

@@ -1,12 +1,9 @@
package model
import (
"strconv"
"time"
"gorm.io/gorm"
"with_you/internal/pkg/utils"
)
// ConversationType 会话类型
@@ -36,14 +33,7 @@ type Conversation struct {
// BeforeCreate 创建前生成雪花算法ID
func (c *Conversation) BeforeCreate(tx *gorm.DB) error {
if c.ID == "" {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
c.ID = strconv.FormatInt(id, 10)
}
return nil
return SetSnowflakeStringID(&c.ID)
}
func (Conversation) TableName() string {

View File

@@ -4,8 +4,6 @@ import (
"time"
"gorm.io/gorm"
"with_you/internal/pkg/utils"
)
// DeviceType 设备类型
@@ -41,14 +39,7 @@ type DeviceToken struct {
// BeforeCreate 创建前生成雪花算法ID
func (d *DeviceToken) BeforeCreate(tx *gorm.DB) error {
if d.ID == 0 {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
d.ID = id
}
return nil
return SetSnowflakeInt64ID(&d.ID)
}
func (DeviceToken) TableName() string {

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -17,9 +16,7 @@ type Favorite struct {
// BeforeCreate 创建前生成UUID
func (f *Favorite) BeforeCreate(tx *gorm.DB) error {
if f.ID == "" {
f.ID = uuid.New().String()
}
SetUUIDIfEmpty(&f.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -17,9 +16,7 @@ type Follow struct {
// BeforeCreate 创建前生成UUID
func (f *Follow) BeforeCreate(tx *gorm.DB) error {
if f.ID == "" {
f.ID = uuid.New().String()
}
SetUUIDIfEmpty(&f.ID)
return nil
}

View File

@@ -4,8 +4,6 @@ import (
"strconv"
"time"
"with_you/internal/pkg/utils"
"gorm.io/gorm"
)
@@ -35,14 +33,7 @@ type Group struct {
// BeforeCreate 创建前生成雪花算法ID
func (g *Group) BeforeCreate(tx *gorm.DB) error {
if g.ID == "" {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
g.ID = strconv.FormatInt(id, 10)
}
return nil
return SetSnowflakeStringID(&g.ID)
}
// GetIDInt 获取数字类型的ID用于比较

View File

@@ -1,11 +1,8 @@
package model
import (
"strconv"
"time"
"with_you/internal/pkg/utils"
"gorm.io/gorm"
)
@@ -22,14 +19,7 @@ type GroupAnnouncement struct {
// BeforeCreate 创建前生成雪花算法ID
func (ga *GroupAnnouncement) BeforeCreate(tx *gorm.DB) error {
if ga.ID == "" {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
ga.ID = strconv.FormatInt(id, 10)
}
return nil
return SetSnowflakeStringID(&ga.ID)
}
// TableName 指定表名

View File

@@ -1,11 +1,8 @@
package model
import (
"strconv"
"time"
"with_you/internal/pkg/utils"
"gorm.io/gorm"
)
@@ -44,14 +41,7 @@ type GroupJoinRequest struct {
}
func (r *GroupJoinRequest) BeforeCreate(tx *gorm.DB) error {
if r.ID == "" {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
r.ID = strconv.FormatInt(id, 10)
}
return nil
return SetSnowflakeStringID(&r.ID)
}
func (GroupJoinRequest) TableName() string {

View File

@@ -1,11 +1,8 @@
package model
import (
"strconv"
"time"
"with_you/internal/pkg/utils"
"gorm.io/gorm"
)
@@ -24,14 +21,7 @@ type GroupMember struct {
// BeforeCreate 创建前生成雪花算法ID
func (gm *GroupMember) BeforeCreate(tx *gorm.DB) error {
if gm.ID == "" {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
gm.ID = strconv.FormatInt(id, 10)
}
return nil
return SetSnowflakeStringID(&gm.ID)
}
// TableName 指定表名

View File

@@ -0,0 +1,56 @@
package model
import (
"strconv"
"github.com/google/uuid"
"gorm.io/gorm"
"with_you/internal/pkg/utils"
)
func SetUUIDIfEmpty(id *string) {
if *id == "" {
*id = uuid.New().String()
}
}
func SetSnowflakeStringID(id *string) error {
if *id == "" {
sid, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
*id = strconv.FormatInt(sid, 10)
}
return nil
}
func SetSnowflakeInt64ID(id *int64) error {
if *id == 0 {
sid, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
*id = sid
}
return nil
}
func GenerateUUID() string {
return uuid.New().String()
}
func GenerateSnowflakeString() (string, error) {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return "", err
}
return strconv.FormatInt(id, 10), nil
}
func GenerateSnowflakeInt64() (int64, error) {
return utils.GetSnowflake().GenerateID()
}
func nop(_ *gorm.DB) error { return nil }

View File

@@ -0,0 +1,28 @@
package model
import (
"database/sql/driver"
"encoding/json"
)
// ScanJSON scans a database value (nil, []byte, or string) into any JSON-unmarshalable pointer.
func ScanJSON[T any](ptr *T, value any) error {
if value == nil {
return nil
}
var data []byte
switch v := value.(type) {
case []byte:
data = v
case string:
data = []byte(v)
default:
return nil
}
return json.Unmarshal(data, ptr)
}
// ValueJSON marshals any value to JSON for database storage.
func ValueJSON[T any](val T) (driver.Value, error) {
return json.Marshal(val)
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -17,9 +16,7 @@ type PostLike struct {
// BeforeCreate 创建前生成UUID
func (pl *PostLike) BeforeCreate(tx *gorm.DB) error {
if pl.ID == "" {
pl.ID = uuid.New().String()
}
SetUUIDIfEmpty(&pl.ID)
return nil
}

View File

@@ -2,10 +2,8 @@ package model
import (
"database/sql/driver"
"encoding/json"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -44,9 +42,7 @@ type MaterialSubject struct {
// BeforeCreate 创建前生成UUID
func (s *MaterialSubject) BeforeCreate(tx *gorm.DB) error {
if s.ID == "" {
s.ID = uuid.New().String()
}
SetUUIDIfEmpty(&s.ID)
return nil
}
@@ -62,7 +58,7 @@ func (a StringArray) Value() (driver.Value, error) {
if a == nil {
return nil, nil
}
return json.Marshal(a)
return ValueJSON(a)
}
// Scan 实现 sql.Scanner 接口
@@ -71,16 +67,7 @@ func (a *StringArray) Scan(value any) error {
*a = nil
return nil
}
var data []byte
switch v := value.(type) {
case []byte:
data = v
case string:
data = []byte(v)
default:
return nil
}
return json.Unmarshal(data, a)
return ScanJSON(a, value)
}
// MaterialFile 文件资料
@@ -107,9 +94,7 @@ type MaterialFile struct {
// BeforeCreate 创建前生成UUID
func (f *MaterialFile) BeforeCreate(tx *gorm.DB) error {
if f.ID == "" {
f.ID = uuid.New().String()
}
SetUUIDIfEmpty(&f.ID)
return nil
}

View File

@@ -3,15 +3,12 @@
import (
"database/sql/driver"
"encoding/json"
"errors"
"strconv"
"sync"
"time"
"gorm.io/gorm"
"with_you/internal/pkg/crypto"
"with_you/internal/pkg/utils"
)
// 系统消息相关常量
@@ -93,24 +90,12 @@ type ExtraData struct {
// Value 实现driver.Valuer接口用于数据库存储
func (e ExtraData) Value() (driver.Value, error) {
return json.Marshal(e)
return ValueJSON(e)
}
// Scan 实现sql.Scanner接口用于数据库读取
func (e *ExtraData) Scan(value any) error {
if value == nil {
return nil
}
var data []byte
switch v := value.(type) {
case []byte:
data = v
case string:
data = []byte(v)
default:
return errors.New("type assertion to []byte or string failed")
}
return json.Unmarshal(data, e)
return ScanJSON(e, value)
}
// MessageSegmentData 单个消息段的数据
@@ -127,7 +112,7 @@ type MessageSegments []MessageSegment
// Value 实现driver.Valuer接口用于数据库存储
func (s MessageSegments) Value() (driver.Value, error) {
return json.Marshal(s)
return ValueJSON(s)
}
// Scan 实现sql.Scanner接口用于数据库读取
@@ -136,16 +121,7 @@ func (s *MessageSegments) Scan(value any) error {
*s = nil
return nil
}
var data []byte
switch v := value.(type) {
case []byte:
data = v
case string:
data = []byte(v)
default:
return errors.New("type assertion to []byte or string failed")
}
return json.Unmarshal(data, s)
return ScanJSON(s, value)
}
// Message 消息实体
@@ -188,12 +164,8 @@ func (m *Message) SenderIDStr() string {
// BeforeCreate 创建前生成雪花算法ID并加密消息内容
func (m *Message) BeforeCreate(tx *gorm.DB) error {
if m.ID == "" {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
m.ID = strconv.FormatInt(id, 10)
if err := SetSnowflakeStringID(&m.ID); err != nil {
return err
}
// 加密消息内容

View File

@@ -1,19 +0,0 @@
package model
import (
"time"
)
// MessageRead 消息已读状态
// 记录每个用户在每个会话中的已读位置
type MessageRead struct {
ID uint `gorm:"primaryKey" json:"id"`
ConversationID int64 `gorm:"uniqueIndex:idx_conversation_user;not null" json:"conversation_id"`
UserID uint `gorm:"uniqueIndex:idx_conversation_user;not null" json:"user_id"`
LastReadSeq int64 `gorm:"not null" json:"last_read_seq"` // 已读到的seq位置
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}
func (MessageRead) TableName() string {
return "message_reads"
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -42,9 +41,7 @@ type Notification struct {
// BeforeCreate 创建前生成UUID
func (n *Notification) BeforeCreate(tx *gorm.DB) error {
if n.ID == "" {
n.ID = uuid.New().String()
}
SetUUIDIfEmpty(&n.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -72,9 +71,7 @@ type OperationLog struct {
// BeforeCreate 创建前生成TraceID
func (ol *OperationLog) BeforeCreate(tx *gorm.DB) error {
if ol.TraceID == "" {
ol.TraceID = uuid.New().String()
}
SetUUIDIfEmpty(&ol.TraceID)
if ol.OccurredAt.IsZero() {
ol.OccurredAt = time.Now()
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -66,9 +65,7 @@ type Post struct {
// BeforeCreate 创建前生成UUID
func (p *Post) BeforeCreate(tx *gorm.DB) error {
if p.ID == "" {
p.ID = uuid.New().String()
}
SetUUIDIfEmpty(&p.ID)
return nil
}
@@ -94,9 +91,7 @@ type PostImage struct {
// BeforeCreate 创建前生成UUID
func (pi *PostImage) BeforeCreate(tx *gorm.DB) error {
if pi.ID == "" {
pi.ID = uuid.New().String()
}
SetUUIDIfEmpty(&pi.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -29,9 +28,7 @@ type PostReference struct {
}
func (pr *PostReference) BeforeCreate(tx *gorm.DB) error {
if pr.ID == "" {
pr.ID = uuid.New().String()
}
SetUUIDIfEmpty(&pr.ID)
return nil
}

View File

@@ -4,8 +4,6 @@ import (
"time"
"gorm.io/gorm"
"with_you/internal/pkg/utils"
)
// PushChannel 推送通道类型
@@ -66,14 +64,7 @@ type PushRecord struct {
// BeforeCreate 创建前生成雪花算法ID
func (r *PushRecord) BeforeCreate(tx *gorm.DB) error {
if r.ID == 0 {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
r.ID = id
}
return nil
return SetSnowflakeInt64ID(&r.ID)
}
func (PushRecord) TableName() string {

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -60,9 +59,7 @@ type Report struct {
// BeforeCreate 创建前生成UUID
func (r *Report) BeforeCreate(tx *gorm.DB) error {
if r.ID == "" {
r.ID = uuid.New().String()
}
SetUUIDIfEmpty(&r.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -24,9 +23,7 @@ type ScheduleCourse struct {
}
func (s *ScheduleCourse) BeforeCreate(tx *gorm.DB) error {
if s.ID == "" {
s.ID = uuid.New().String()
}
SetUUIDIfEmpty(&s.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -46,9 +45,7 @@ type SensitiveWord struct {
// BeforeCreate 创建前生成UUID
func (sw *SensitiveWord) BeforeCreate(tx *gorm.DB) error {
if sw.ID == "" {
sw.ID = uuid.New().String()
}
SetUUIDIfEmpty(&sw.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -26,8 +25,6 @@ func (UserSticker) TableName() string {
// BeforeCreate 创建前生成UUID
func (s *UserSticker) BeforeCreate(tx *gorm.DB) error {
if s.ID == "" {
s.ID = uuid.New().String()
}
SetUUIDIfEmpty(&s.ID)
return nil
}

View File

@@ -2,13 +2,9 @@
import (
"database/sql/driver"
"encoding/json"
"errors"
"time"
"gorm.io/gorm"
"with_you/internal/pkg/utils"
)
// SystemNotificationType 系统通知类型
@@ -71,24 +67,12 @@ type SystemNotificationExtra struct {
// Value 实现driver.Valuer接口
func (e SystemNotificationExtra) Value() (driver.Value, error) {
return json.Marshal(e)
return ValueJSON(e)
}
// Scan 实现sql.Scanner接口
func (e *SystemNotificationExtra) Scan(value any) error {
if value == nil {
return nil
}
var data []byte
switch v := value.(type) {
case []byte:
data = v
case string:
data = []byte(v)
default:
return errors.New("type assertion to []byte or string failed")
}
return json.Unmarshal(data, e)
return ScanJSON(e, value)
}
// SystemNotification 系统通知(独立表,与消息完全分离)
@@ -113,14 +97,7 @@ type SystemNotification struct {
// BeforeCreate 创建前生成雪花算法ID
func (n *SystemNotification) BeforeCreate(tx *gorm.DB) error {
if n.ID == 0 {
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
return err
}
n.ID = id
}
return nil
return SetSnowflakeInt64ID(&n.ID)
}
// TableName 指定表名

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -57,9 +56,7 @@ type TradeItem struct {
}
func (t *TradeItem) BeforeCreate(tx *gorm.DB) error {
if t.ID == "" {
t.ID = uuid.New().String()
}
SetUUIDIfEmpty(&t.ID)
return nil
}
@@ -81,9 +78,7 @@ type TradeImage struct {
}
func (ti *TradeImage) BeforeCreate(tx *gorm.DB) error {
if ti.ID == "" {
ti.ID = uuid.New().String()
}
SetUUIDIfEmpty(&ti.ID)
return nil
}
@@ -99,9 +94,7 @@ type TradeFavorite struct {
}
func (f *TradeFavorite) BeforeCreate(tx *gorm.DB) error {
if f.ID == "" {
f.ID = uuid.New().String()
}
SetUUIDIfEmpty(&f.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -106,9 +105,7 @@ type User struct {
// BeforeCreate 创建前生成UUID
func (u *User) BeforeCreate(tx *gorm.DB) error {
if u.ID == "" {
u.ID = uuid.New().String()
}
SetUUIDIfEmpty(&u.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -16,9 +15,7 @@ type UserBlock struct {
}
func (b *UserBlock) BeforeCreate(tx *gorm.DB) error {
if b.ID == "" {
b.ID = uuid.New().String()
}
SetUUIDIfEmpty(&b.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -40,9 +39,7 @@ type UserProfileAudit struct {
}
func (a *UserProfileAudit) BeforeCreate(tx *gorm.DB) error {
if a.ID == "" {
a.ID = uuid.New().String()
}
SetUUIDIfEmpty(&a.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -29,9 +28,7 @@ type VerificationRecord struct {
}
func (v *VerificationRecord) BeforeCreate(tx *gorm.DB) error {
if v.ID == "" {
v.ID = uuid.New().String()
}
SetUUIDIfEmpty(&v.ID)
return nil
}

View File

@@ -3,7 +3,6 @@ package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -20,9 +19,7 @@ type VoteOption struct {
// BeforeCreate 创建前生成UUID
func (vo *VoteOption) BeforeCreate(tx *gorm.DB) error {
if vo.ID == "" {
vo.ID = uuid.New().String()
}
SetUUIDIfEmpty(&vo.ID)
return nil
}
@@ -41,9 +38,7 @@ type UserVote struct {
// BeforeCreate 创建前生成UUID
func (uv *UserVote) BeforeCreate(tx *gorm.DB) error {
if uv.ID == "" {
uv.ID = uuid.New().String()
}
SetUUIDIfEmpty(&uv.ID)
return nil
}