完善自动续期策略与管理配置
This commit is contained in:
1 parent
8158cefc05
commit
4ac123798d
8 files changed
+323
-28
No files matched your search
@@ -18,33 +18,52 @@ type CertificateIssuer interface {
|
||||
Issue(ctx context.Context, domainID string) (IssueResult, error)
|
||||
}
|
||||
|
||||
type RenewalPolicy struct {
|
||||
CheckInterval time.Duration
|
||||
NearExpiryThreshold time.Duration
|
||||
RenewalThreshold time.Duration
|
||||
UrgentThreshold time.Duration
|
||||
MaxRetries int
|
||||
RetryInterval time.Duration
|
||||
}
|
||||
|
||||
type RenewalScheduler struct {
|
||||
lister DomainLister
|
||||
issuer CertificateIssuer
|
||||
threshold time.Duration
|
||||
now func() time.Time
|
||||
interval time.Duration
|
||||
mu sync.Mutex
|
||||
runMu sync.Mutex
|
||||
running bool
|
||||
lister DomainLister
|
||||
issuer CertificateIssuer
|
||||
policy RenewalPolicy
|
||||
now func() time.Time
|
||||
mu sync.Mutex
|
||||
runMu sync.Mutex
|
||||
running bool
|
||||
}
|
||||
|
||||
type RenewalReport struct {
|
||||
Checked int `json:"checked"`
|
||||
Selected int `json:"selected"`
|
||||
Succeeded int `json:"succeeded"`
|
||||
Failed int `json:"failed"`
|
||||
Errors []string `json:"errors,omitempty"`
|
||||
Checked int `json:"checked"`
|
||||
NearExpiry int `json:"near_expiry"`
|
||||
Selected int `json:"selected"`
|
||||
Urgent int `json:"urgent"`
|
||||
Succeeded int `json:"succeeded"`
|
||||
Failed int `json:"failed"`
|
||||
Errors []string `json:"errors,omitempty"`
|
||||
}
|
||||
|
||||
func NewRenewalScheduler(lister DomainLister, issuer CertificateIssuer, thresholdDays int) *RenewalScheduler {
|
||||
if thresholdDays < 1 {
|
||||
thresholdDays = 6
|
||||
}
|
||||
return &RenewalScheduler{
|
||||
lister: lister, issuer: issuer, threshold: time.Duration(thresholdDays) * 24 * time.Hour,
|
||||
now: time.Now, interval: 24 * time.Hour,
|
||||
}
|
||||
return NewRenewalSchedulerWithPolicy(lister, issuer, RenewalPolicy{
|
||||
CheckInterval: 3 * 24 * time.Hour,
|
||||
NearExpiryThreshold: 10 * 24 * time.Hour,
|
||||
RenewalThreshold: time.Duration(thresholdDays) * 24 * time.Hour,
|
||||
UrgentThreshold: 3 * 24 * time.Hour,
|
||||
MaxRetries: 3,
|
||||
RetryInterval: 30 * time.Minute,
|
||||
})
|
||||
}
|
||||
|
||||
func NewRenewalSchedulerWithPolicy(lister DomainLister, issuer CertificateIssuer, policy RenewalPolicy) *RenewalScheduler {
|
||||
policy = normalizeRenewalPolicy(policy)
|
||||
return &RenewalScheduler{lister: lister, issuer: issuer, policy: policy, now: time.Now}
|
||||
}
|
||||
|
||||
func (s *RenewalScheduler) RunOnce(ctx context.Context) (RenewalReport, error) {
|
||||
@@ -65,11 +84,19 @@ func (s *RenewalScheduler) RunOnce(ctx context.Context) (RenewalReport, error) {
|
||||
if !item.AutoRenew || item.Status == "issuing" {
|
||||
continue
|
||||
}
|
||||
if item.ExpiresAt != nil && item.ExpiresAt.Sub(now) > s.threshold {
|
||||
remaining := renewalRemaining(item.ExpiresAt, now)
|
||||
if item.ExpiresAt != nil && remaining <= s.policy.NearExpiryThreshold {
|
||||
report.NearExpiry++
|
||||
}
|
||||
if item.ExpiresAt != nil && remaining > s.policy.RenewalThreshold {
|
||||
continue
|
||||
}
|
||||
report.Selected++
|
||||
if _, err := s.issuer.Issue(ctx, item.ID); err != nil {
|
||||
urgent := item.ExpiresAt != nil && remaining <= s.policy.UrgentThreshold
|
||||
if urgent {
|
||||
report.Urgent++
|
||||
}
|
||||
if err := s.issueWithRetry(ctx, item.ID, urgent); err != nil {
|
||||
report.Failed++
|
||||
report.Errors = append(report.Errors, fmt.Sprintf("%s:%v", item.Name, err))
|
||||
continue
|
||||
@@ -96,16 +123,84 @@ func (s *RenewalScheduler) Start(ctx context.Context) {
|
||||
s.running = false
|
||||
s.mu.Unlock()
|
||||
}()
|
||||
_, _ = s.RunOnce(ctx)
|
||||
ticker := time.NewTicker(s.interval)
|
||||
defer ticker.Stop()
|
||||
report, err := s.RunOnce(ctx)
|
||||
for {
|
||||
interval := s.policy.CheckInterval
|
||||
if err == nil && report.NearExpiry > 0 {
|
||||
interval = 24 * time.Hour
|
||||
}
|
||||
timer := time.NewTimer(interval)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
return
|
||||
case <-ticker.C:
|
||||
_, _ = s.RunOnce(ctx)
|
||||
case <-timer.C:
|
||||
}
|
||||
report, err = s.RunOnce(ctx)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *RenewalScheduler) issueWithRetry(ctx context.Context, domainID string, urgent bool) error {
|
||||
attempts := 1
|
||||
if urgent {
|
||||
attempts = s.policy.MaxRetries
|
||||
}
|
||||
var lastErr error
|
||||
for attempt := 1; attempt <= attempts; attempt++ {
|
||||
if _, err := s.issuer.Issue(ctx, domainID); err == nil {
|
||||
return nil
|
||||
} else {
|
||||
lastErr = err
|
||||
}
|
||||
if attempt == attempts {
|
||||
break
|
||||
}
|
||||
if err := waitForRenewalRetry(ctx, s.policy.RetryInterval); err != nil {
|
||||
return fmt.Errorf("第 %d 次续期失败,等待重试被取消:%w", attempt, err)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("续期失败(已尝试 %d 次):%w", attempts, lastErr)
|
||||
}
|
||||
|
||||
func waitForRenewalRetry(ctx context.Context, interval time.Duration) error {
|
||||
timer := time.NewTimer(interval)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func renewalRemaining(expiresAt *time.Time, now time.Time) time.Duration {
|
||||
if expiresAt == nil {
|
||||
return 0
|
||||
}
|
||||
return expiresAt.Sub(now)
|
||||
}
|
||||
|
||||
func normalizeRenewalPolicy(policy RenewalPolicy) RenewalPolicy {
|
||||
if policy.CheckInterval <= 0 {
|
||||
policy.CheckInterval = 3 * 24 * time.Hour
|
||||
}
|
||||
if policy.NearExpiryThreshold <= 0 {
|
||||
policy.NearExpiryThreshold = 10 * 24 * time.Hour
|
||||
}
|
||||
if policy.RenewalThreshold <= 0 {
|
||||
policy.RenewalThreshold = 6 * 24 * time.Hour
|
||||
}
|
||||
if policy.UrgentThreshold <= 0 {
|
||||
policy.UrgentThreshold = 3 * 24 * time.Hour
|
||||
}
|
||||
if policy.MaxRetries <= 0 {
|
||||
policy.MaxRetries = 3
|
||||
}
|
||||
if policy.RetryInterval <= 0 {
|
||||
policy.RetryInterval = 30 * time.Minute
|
||||
}
|
||||
return policy
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestRenewalSchedulerSelectsOnlyExpiringAutoRenewDomains(t *testing.T) {
|
||||
|
||||
func TestRenewalSchedulerReportsFailures(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
expires := now.Add(time.Hour)
|
||||
expires := now.Add(4 * 24 * time.Hour)
|
||||
item := domain.Domain{ID: "one", Name: "one.example.com", AutoRenew: true, ExpiresAt: &expires}
|
||||
scheduler := NewRenewalScheduler(renewalLister{items: []domain.Domain{item}}, &renewalService{err: errors.New("模拟续期失败")}, 6)
|
||||
report, err := scheduler.RunOnce(context.Background())
|
||||
@@ -64,3 +64,54 @@ func TestRenewalSchedulerReportsFailures(t *testing.T) {
|
||||
t.Fatalf("续期失败结果错误:%+v", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenewalSchedulerRetriesUrgentRenewal(t *testing.T) {
|
||||
now := time.Date(2026, 8, 26, 0, 0, 0, 0, time.UTC)
|
||||
expires := now.Add(2 * 24 * time.Hour)
|
||||
service := &renewalService{err: errors.New("模拟紧急续期失败")}
|
||||
scheduler := NewRenewalSchedulerWithPolicy(renewalLister{items: []domain.Domain{{
|
||||
ID: "urgent", Name: "urgent.example.com", AutoRenew: true, ExpiresAt: &expires,
|
||||
}}}, service, RenewalPolicy{
|
||||
CheckInterval: time.Hour,
|
||||
NearExpiryThreshold: 10 * 24 * time.Hour,
|
||||
RenewalThreshold: 6 * 24 * time.Hour,
|
||||
UrgentThreshold: 3 * 24 * time.Hour,
|
||||
MaxRetries: 3,
|
||||
RetryInterval: time.Nanosecond,
|
||||
})
|
||||
scheduler.now = func() time.Time { return now }
|
||||
|
||||
report, err := scheduler.RunOnce(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if report.Selected != 1 || report.Urgent != 1 || report.Failed != 1 || report.Succeeded != 0 {
|
||||
t.Fatalf("紧急续期报告错误:%+v", report)
|
||||
}
|
||||
if len(service.ids) != 3 {
|
||||
t.Fatalf("紧急续期应按最大重试次数执行:%v", service.ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenewalSchedulerStopsRetryWhenContextCanceled(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
expires := now.Add(2 * 24 * time.Hour)
|
||||
service := &renewalService{err: errors.New("模拟紧急续期失败")}
|
||||
scheduler := NewRenewalSchedulerWithPolicy(renewalLister{items: []domain.Domain{{
|
||||
ID: "urgent", Name: "urgent.example.com", AutoRenew: true, ExpiresAt: &expires,
|
||||
}}}, service, RenewalPolicy{MaxRetries: 3, RetryInterval: time.Hour})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
cancel()
|
||||
}()
|
||||
report, err := scheduler.RunOnce(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if report.Failed != 1 || len(service.ids) != 1 {
|
||||
t.Fatalf("取消后的紧急续期重试报告错误:%+v calls=%d", report, len(service.ids))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user