2025-12-19 23:39:28 +08:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2026-01-08 23:07:00 +08:00
|
|
|
|
"fmt"
|
2025-12-19 23:39:28 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
2025-12-25 17:15:01 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
2025-12-19 23:39:28 +08:00
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const stickySessionPrefix = "sticky_session:"
|
|
|
|
|
|
|
|
|
|
|
|
type gatewayCache struct {
|
|
|
|
|
|
rdb *redis.Client
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 17:15:01 +08:00
|
|
|
|
func NewGatewayCache(rdb *redis.Client) service.GatewayCache {
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return &gatewayCache{rdb: rdb}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 23:07:00 +08:00
|
|
|
|
// buildSessionKey 构建 session key,包含 groupID 实现分组隔离
|
|
|
|
|
|
// 格式: sticky_session:{groupID}:{sessionHash}
|
|
|
|
|
|
func buildSessionKey(groupID int64, sessionHash string) string {
|
|
|
|
|
|
return fmt.Sprintf("%s%d:%s", stickySessionPrefix, groupID, sessionHash)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *gatewayCache) GetSessionAccountID(ctx context.Context, groupID int64, sessionHash string) (int64, error) {
|
|
|
|
|
|
key := buildSessionKey(groupID, sessionHash)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return c.rdb.Get(ctx, key).Int64()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 23:07:00 +08:00
|
|
|
|
func (c *gatewayCache) SetSessionAccountID(ctx context.Context, groupID int64, sessionHash string, accountID int64, ttl time.Duration) error {
|
|
|
|
|
|
key := buildSessionKey(groupID, sessionHash)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return c.rdb.Set(ctx, key, accountID, ttl).Err()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 23:07:00 +08:00
|
|
|
|
func (c *gatewayCache) RefreshSessionTTL(ctx context.Context, groupID int64, sessionHash string, ttl time.Duration) error {
|
|
|
|
|
|
key := buildSessionKey(groupID, sessionHash)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return c.rdb.Expire(ctx, key, ttl).Err()
|
|
|
|
|
|
}
|
2026-01-20 11:19:32 +08:00
|
|
|
|
|
|
|
|
|
|
// DeleteSessionAccountID 删除粘性会话与账号的绑定关系。
|
|
|
|
|
|
// 当检测到绑定的账号不可用(如状态错误、禁用、不可调度等)时调用,
|
|
|
|
|
|
// 以便下次请求能够重新选择可用账号。
|
|
|
|
|
|
//
|
|
|
|
|
|
// DeleteSessionAccountID removes the sticky session binding for the given session.
|
|
|
|
|
|
// Called when the bound account becomes unavailable (e.g., error status, disabled,
|
|
|
|
|
|
// or unschedulable), allowing subsequent requests to select a new available account.
|
|
|
|
|
|
func (c *gatewayCache) DeleteSessionAccountID(ctx context.Context, groupID int64, sessionHash string) error {
|
|
|
|
|
|
key := buildSessionKey(groupID, sessionHash)
|
|
|
|
|
|
return c.rdb.Del(ctx, key).Err()
|
|
|
|
|
|
}
|