From 05cba292d46ababa83f24f78aa47986bd0f574ab Mon Sep 17 00:00:00 2001 From: zenfun Date: Sun, 4 Jan 2026 12:07:37 +0800 Subject: [PATCH] refactor(service): remove legacy routing and unused code Removes the legacy route table maintenance logic from the sync service that populated deprecated Redis keys. Additionally, deletes the unused TokenService and KeyDTO files to reduce technical debt. --- internal/dto/key.go | 10 ------ internal/service/sync.go | 15 -------- internal/service/token.go | 76 --------------------------------------- 3 files changed, 101 deletions(-) delete mode 100644 internal/dto/key.go delete mode 100644 internal/service/token.go diff --git a/internal/dto/key.go b/internal/dto/key.go deleted file mode 100644 index 685b98f..0000000 --- a/internal/dto/key.go +++ /dev/null @@ -1,10 +0,0 @@ -package dto - -// KeyDTO defines payload for key creation/update. -type KeyDTO struct { - Group string `json:"group"` - KeySecret string `json:"key_secret"` - Balance float64 `json:"balance"` - Status string `json:"status"` - Weight int `json:"weight"` -} diff --git a/internal/service/sync.go b/internal/service/sync.go index ba35817..b56374b 100644 --- a/internal/service/sync.go +++ b/internal/service/sync.go @@ -366,21 +366,6 @@ func (s *SyncService) writeProvidersSnapshot(ctx context.Context, pipe redis.Pip return fmt.Errorf("marshal provider %d: %w", k.ID, err) } pipe.HSet(ctx, "config:providers", fmt.Sprintf("%d", k.ID), payload) - - // Legacy route table maintenance for compatibility. - for _, m := range models { - if m == "" { - continue - } - if snap.Status != "active" { - continue - } - if snap.BanUntil > 0 && time.Now().Unix() < snap.BanUntil { - continue - } - routeKey := fmt.Sprintf("route:group:%s:%s", groupName, m) - pipe.SAdd(ctx, routeKey, k.ID) - } } return nil } diff --git a/internal/service/token.go b/internal/service/token.go deleted file mode 100644 index 5389a20..0000000 --- a/internal/service/token.go +++ /dev/null @@ -1,76 +0,0 @@ -package service - -import ( - "context" - "errors" - "fmt" - "strconv" - - "github.com/ez-api/foundation/tokenhash" - "github.com/redis/go-redis/v9" -) - -type TokenService struct { - rdb *redis.Client -} - -func NewTokenService(rdb *redis.Client) *TokenService { - return &TokenService{rdb: rdb} -} - -type TokenInfo struct { - MasterID uint - IssuedAtEpoch int64 - Status string - Group string -} - -// ValidateToken checks a child key against Redis for validity. -// This is designed to be called by the data plane (balancer). -func (s *TokenService) ValidateToken(ctx context.Context, token string) (*TokenInfo, error) { - tokenHash := tokenhash.HashToken(token) - tokenKey := fmt.Sprintf("auth:token:%s", tokenHash) - - // 1. Get token metadata from Redis - tokenData, err := s.rdb.HGetAll(ctx, tokenKey).Result() - if err != nil { - return nil, fmt.Errorf("failed to get token data: %w", err) - } - if len(tokenData) == 0 { - return nil, errors.New("token not found") - } - - if tokenData["status"] != "active" { - return nil, errors.New("token is not active") - } - - masterID, _ := strconv.ParseUint(tokenData["master_id"], 10, 64) - issuedAtEpoch, _ := strconv.ParseInt(tokenData["issued_at_epoch"], 10, 64) - - // 2. Get master metadata from Redis - masterKey := fmt.Sprintf("auth:master:%d", masterID) - masterData, err := s.rdb.HGetAll(ctx, masterKey).Result() - if err != nil { - return nil, fmt.Errorf("failed to get master metadata: %w", err) - } - if len(masterData) == 0 { - return nil, errors.New("master metadata not found") - } - masterStatus := masterData["status"] - if masterStatus != "" && masterStatus != "active" { - return nil, errors.New("master is not active") - } - masterEpoch, _ := strconv.ParseInt(masterData["epoch"], 10, 64) - - // 3. Core Epoch Validation - if issuedAtEpoch < masterEpoch { - return nil, errors.New("token revoked due to master key rotation") - } - - return &TokenInfo{ - MasterID: uint(masterID), - IssuedAtEpoch: issuedAtEpoch, - Status: tokenData["status"], - Group: tokenData["group"], - }, nil -}