推进P1账号生命周期与自动化补偿迁移

This commit is contained in:
Christ committed 2026-08-16 01:44:11 +08:00
1 parent a0ce23e9cf
commit 19a7d5f924
17 files changed
+731 -74

No files matched your search

@@ -0,0 +1,77 @@
package account
import (
"context"
"errors"
"time"
)
// ErrDeleteConflict 表示账号运行时尚未收束,删除操作不会覆盖仍存活的实例。
var ErrDeleteConflict = errors.New("账号正在停止,请稍后重试")
// DeleteRepository 定义账号删除用例所需的最小非敏感持久化端口。
type DeleteRepository interface {
// GetOwnedSummary 按用户和账号联合查询非敏感摘要,用于删除前归属确认。
GetOwnedSummary(context.Context, int64, string) (Summary, error)
// DeleteOwned 在基础设施边界再次确认归属后删除账号及其关联数据。
DeleteOwned(context.Context, int64, string) error
}
// DeleteRuntime 定义账号删除期间的运行时 fencing 与收束端口。
type DeleteRuntime interface {
// BeginStopping 建立账号级 fencing,阻止删除期间新的运行实例启动。
BeginStopping(string) bool
// StopContext 在调用方 Context 限制内停止运行实例并等待其退出。
StopContext(context.Context, string) error
// EndStopping 释放账号级 fencing,供失败或成功后的收束路径调用。
EndStopping(string)
}
// DeleteService 编排账号归属校验、运行时 fencing 和关联数据删除。
type DeleteService struct {
// repository 提供非敏感账号查询和按归属删除能力。
repository DeleteRepository
// runtime 提供可选的账号运行时停止能力;为空时仅执行持久化删除。
runtime DeleteRuntime
}
// NewDeleteService 构造账号删除应用服务并校验必需的持久化端口。
func NewDeleteService(repository DeleteRepository, runtime DeleteRuntime) (*DeleteService, error) {
if repository == nil {
return nil, errors.New("账号删除 repository 未初始化")
}
return &DeleteService{repository: repository, runtime: runtime}, nil
}
// Delete 删除指定用户拥有的账号;运行时停止超时或 fencing 冲突时保留账号不变。
func (s *DeleteService) Delete(ctx context.Context, userID int64, accountID string) error {
if s == nil || s.repository == nil {
return errors.New("账号删除服务未初始化")
}
if accountID == "" {
return errors.New("缺少账号 ID")
}
// summary 保存删除前通过归属校验的非敏感账号摘要;凭证字段不会被读取。
if _, err := s.repository.GetOwnedSummary(ctx, userID, accountID); err != nil {
return err
}
if s.runtime != nil {
if !s.runtime.BeginStopping(accountID) {
return ErrDeleteConflict
}
defer s.runtime.EndStopping(accountID)
// stopErr 保存运行时在调用方 Context 内收束账号实例的结果。
// stopCtx、stopCancel 将删除等待限制在最多五秒,避免无截止请求永久占用 fencing。
stopCtx, stopCancel := context.WithTimeout(ctx, 5*time.Second)
defer stopCancel()
// stopErr 保存运行时停止结果;任何停止失败都阻止后续删除。
if err := s.runtime.StopContext(stopCtx, accountID); err != nil {
return ErrDeleteConflict
}
}
// deleteErr 在基础设施边界重新确认归属并删除关联数据,避免停止期间账号被替换后误删。
if deleteErr := s.repository.DeleteOwned(ctx, userID, accountID); deleteErr != nil {
return deleteErr
}
return nil
}
@@ -0,0 +1,174 @@
package account
import (
"context"
"errors"
"testing"
)
// deleteRepositoryStub 是账号删除服务测试使用的非敏感存储替身。
type deleteRepositoryStub struct {
summary Summary
getErr error
deleteErr error
deleted bool
}
// GetOwnedSummary 返回预置摘要或读取错误,模拟按用户过滤的账号查询。
func (r *deleteRepositoryStub) GetOwnedSummary(context.Context, int64, string) (Summary, error) {
if r.getErr != nil {
return Summary{}, r.getErr
}
return r.summary, nil
}
// DeleteOwned 记录删除调用并返回预置的持久化错误。
func (r *deleteRepositoryStub) DeleteOwned(context.Context, int64, string) error {
if r.deleteErr != nil {
return r.deleteErr
}
r.deleted = true
return nil
}
// deleteRuntimeStub 记录账号 fencing 与停止调用,便于验证收束顺序。
type deleteRuntimeStub struct {
beginOK bool
stopErr error
needDeadline bool
begin int
stop int
end int
stopOpen chan struct{}
}
// BeginStopping 返回预置结果并记录建立 fencing 的次数。
func (r *deleteRuntimeStub) BeginStopping(string) bool {
r.begin++
return r.beginOK
}
// StopContext 记录停止调用并可阻塞到测试显式释放。
func (r *deleteRuntimeStub) StopContext(ctx context.Context, _ string) error {
r.stop++
if r.needDeadline {
// ok 表示应用服务是否为停止操作注入了截止时间。
if _, ok := ctx.Deadline(); !ok {
return errors.New("停止上下文缺少截止时间")
}
return r.stopErr
}
if r.stopOpen != nil {
select {
case <-r.stopOpen:
case <-ctx.Done():
return ctx.Err()
}
}
return r.stopErr
}
// TestDeleteServiceBoundsRuntimeStop 验证删除服务为运行时停止提供最多五秒的截止时间。
func TestDeleteServiceBoundsRuntimeStop(t *testing.T) {
// repository 是账号删除用例的非敏感存储替身。
repository := &deleteRepositoryStub{summary: Summary{ID: "acc-1", UserID: 7}}
// runtime 是要求收到带截止时间 Context 的运行时替身。
runtime := &deleteRuntimeStub{beginOK: true, needDeadline: true}
// service 是待验证的账号删除应用服务。
service, err := NewDeleteService(repository, runtime)
if err != nil {
t.Fatal(err)
}
// deleteErr 保存带截止时间的停止结果。
if deleteErr := service.Delete(context.Background(), 7, "acc-1"); deleteErr != nil {
t.Fatalf("带截止时间的停止不应失败: %v", deleteErr)
}
}
// EndStopping 记录释放 fencing 的次数。
func (r *deleteRuntimeStub) EndStopping(string) {
r.end++
}
// TestDeleteServiceStopsBeforeOwnedDelete 验证账号先停止运行时再执行持久化删除。
func TestDeleteServiceStopsBeforeOwnedDelete(t *testing.T) {
// repository 是账号删除用例的非敏感存储替身。
repository := &deleteRepositoryStub{summary: Summary{ID: "acc-1", UserID: 7}}
// runtime 是记录 fencing 顺序的运行时替身。
runtime := &deleteRuntimeStub{beginOK: true}
// service 是待验证的账号删除应用服务。
service, err := NewDeleteService(repository, runtime)
if err != nil {
t.Fatal(err)
}
// deleteErr 保存账号停止并删除用例的执行结果。
if err := service.Delete(context.Background(), 7, "acc-1"); err != nil {
t.Fatalf("Delete error=%v", err)
}
if !repository.deleted || runtime.begin != 1 || runtime.stop != 1 || runtime.end != 1 {
t.Fatalf("删除收束不完整: deleted=%v begin=%d stop=%d end=%d", repository.deleted, runtime.begin, runtime.stop, runtime.end)
}
}
// TestDeleteServiceKeepsFencingOnStopFailure 验证停止失败时不会删除账号且一定释放 fencing。
func TestDeleteServiceKeepsFencingOnStopFailure(t *testing.T) {
// repository 是用于确认删除未发生的存储替身。
repository := &deleteRepositoryStub{summary: Summary{ID: "acc-1", UserID: 7}}
// runtime 是返回停止错误的运行时替身。
runtime := &deleteRuntimeStub{beginOK: true, stopErr: errors.New("stop failed")}
// service 是待验证的账号删除应用服务。
service, err := NewDeleteService(repository, runtime)
if err != nil {
t.Fatal(err)
}
// deleteErr 保存停止失败后的应用错误。
if err := service.Delete(context.Background(), 7, "acc-1"); !errors.Is(err, ErrDeleteConflict) {
t.Fatalf("停止失败应转换为删除冲突: %v", err)
}
if repository.deleted || runtime.end != 1 {
t.Fatalf("停止失败后仍删除或未释放 fencing: deleted=%v end=%d", repository.deleted, runtime.end)
}
}
// TestDeleteServiceRejectsFencingConflict 验证已有删除流程时不会进入停止或删除路径。
func TestDeleteServiceRejectsFencingConflict(t *testing.T) {
// repository 是用于确认冲突短路的存储替身。
repository := &deleteRepositoryStub{summary: Summary{ID: "acc-1", UserID: 7}}
// runtime 是拒绝重复 fencing 的运行时替身。
runtime := &deleteRuntimeStub{beginOK: false}
// service 是待验证的账号删除应用服务。
service, err := NewDeleteService(repository, runtime)
if err != nil {
t.Fatal(err)
}
// deleteErr 保存 fencing 冲突后的应用错误。
if err := service.Delete(context.Background(), 7, "acc-1"); !errors.Is(err, ErrDeleteConflict) {
t.Fatalf("重复 fencing 应返回冲突: %v", err)
}
if repository.deleted || runtime.stop != 0 || runtime.end != 0 {
t.Fatalf("fencing 冲突未短路: deleted=%v stop=%d end=%d", repository.deleted, runtime.stop, runtime.end)
}
}
// TestDeleteServiceHonorsCancellation 验证停止等待受调用方 Context 限制且不删除账号。
func TestDeleteServiceHonorsCancellation(t *testing.T) {
// repository 是用于确认取消后不删除的存储替身。
repository := &deleteRepositoryStub{summary: Summary{ID: "acc-1", UserID: 7}}
// runtime 是等待关闭信号或 Context 取消的运行时替身。
runtime := &deleteRuntimeStub{beginOK: true, stopOpen: make(chan struct{})}
// service 是待验证的账号删除应用服务。
service, err := NewDeleteService(repository, runtime)
if err != nil {
t.Fatal(err)
}
// ctx、cancel 将停止等待限制在确定的短超时内。
ctx, cancel := context.WithCancel(context.Background())
cancel()
// deleteErr 保存取消停止等待后的应用错误。
if err := service.Delete(ctx, 7, "acc-1"); !errors.Is(err, ErrDeleteConflict) {
t.Fatalf("取消停止应转换为删除冲突: %v", err)
}
if repository.deleted || runtime.end != 1 {
t.Fatalf("取消后删除或 fencing 收束错误: deleted=%v end=%d", repository.deleted, runtime.end)
}
}
@@ -0,0 +1,56 @@
package account
import (
"context"
"errors"
)
// ErrPasswordLoginDisabled 表示当前 Go 客户端不允许使用浏览器密码登录账号。
var ErrPasswordLoginDisabled = errors.New("密码登录已禁用")
// PasswordLoginStartInput 是密码登录启动请求的非敏感身份输入;不接收用户名、密码或 Cookie。
type PasswordLoginStartInput struct {
// UserID 是已经通过 HTTP 鉴权的本地用户标识,供未来启用登录会话时执行归属隔离。
UserID int64
}
// PasswordLoginSessionInput 是密码登录会话操作的非敏感输入;会话标识不包含平台凭证。
type PasswordLoginSessionInput struct {
// UserID 是已经通过 HTTP 鉴权的本地用户标识。
UserID int64
// SessionID 是待查询或取消的进程内登录会话标识。
SessionID string
}
// PasswordLoginService 编排密码登录能力策略;当前产品明确关闭浏览器密码登录。
// 浏览器 PasswordLogin 实现仍由 internal/browser 独立保留,避免绕过冻结的验证码行为。
type PasswordLoginService struct{}
// NewPasswordLoginService 构造不保存凭证和会话状态的密码登录应用服务。
func NewPasswordLoginService() *PasswordLoginService {
return &PasswordLoginService{}
}
// Start 拒绝启动密码登录;调用方不应把请求体中的密码传入应用层。
func (s *PasswordLoginService) Start(ctx context.Context, input PasswordLoginStartInput) error {
// ctx 和 input 预留给未来启用会话时的取消与归属校验;当前关闭策略不读取敏感请求体。
_ = ctx
_ = input
return ErrPasswordLoginDisabled
}
// Check 拒绝查询密码登录会话;当前不存在可供 Go 客户端轮询的密码登录会话。
func (s *PasswordLoginService) Check(ctx context.Context, input PasswordLoginSessionInput) error {
// ctx 和 input 仅表达调用边界,当前关闭策略不会创建或读取会话状态。
_ = ctx
_ = input
return ErrPasswordLoginDisabled
}
// Cancel 拒绝取消密码登录会话;关闭策略确保不会启动浏览器或留下后台任务。
func (s *PasswordLoginService) Cancel(ctx context.Context, input PasswordLoginSessionInput) error {
// ctx 和 input 仅表达调用边界,当前没有需要释放的密码登录资源。
_ = ctx
_ = input
return ErrPasswordLoginDisabled
}
@@ -0,0 +1,39 @@
package account
import (
"context"
"errors"
"testing"
)
// TestPasswordLoginServiceKeepsDisabledPolicy 验证密码登录的启动、查询和取消均由应用层统一拒绝。
func TestPasswordLoginServiceKeepsDisabledPolicy(t *testing.T) {
// service 是当前密码登录关闭策略的应用服务实例。
service := NewPasswordLoginService()
// startErr 保存启动密码登录的策略结果。
startErr := service.Start(context.Background(), PasswordLoginStartInput{UserID: 7})
// checkErr 保存查询密码登录会话的策略结果。
checkErr := service.Check(context.Background(), PasswordLoginSessionInput{UserID: 7, SessionID: "session-1"})
// cancelErr 保存取消密码登录会话的策略结果。
cancelErr := service.Cancel(context.Background(), PasswordLoginSessionInput{UserID: 7, SessionID: "session-1"})
if !errors.Is(startErr, ErrPasswordLoginDisabled) {
t.Fatalf("启动操作应返回密码登录关闭错误,got %v", startErr)
}
if !errors.Is(checkErr, ErrPasswordLoginDisabled) {
t.Fatalf("查询操作应返回密码登录关闭错误,got %v", checkErr)
}
if !errors.Is(cancelErr, ErrPasswordLoginDisabled) {
t.Fatalf("取消操作应返回密码登录关闭错误,got %v", cancelErr)
}
}
// TestPasswordLoginServiceDoesNotRequireCredentialInput 验证应用服务接口不存在密码、Cookie 或平台秘密参数。
func TestPasswordLoginServiceDoesNotRequireCredentialInput(t *testing.T) {
// service 是不保存任何密码登录状态的应用服务实例。
service := NewPasswordLoginService()
// emptyErr 保存空身份请求的结果;关闭策略应先于任何凭证读取返回。
emptyErr := service.Start(context.Background(), PasswordLoginStartInput{})
if !errors.Is(emptyErr, ErrPasswordLoginDisabled) {
t.Fatalf("空身份请求仍应保持关闭策略,got %v", emptyErr)
}
}