mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-20 17:16:37 +00:00
Caddy 式进程内模块系统(详见 docs/plugin-architecture/MODULE-AUTHOR-GUIDE.md): - internal/plugin:命名空间注册表(init 注册/重复 panic)、Provision→Validate→Start→Stop 生命周期(Start 半途逆序回滚、Stop 逆序+ctx deadline)、Host 四项 ports 能力面、 modules: 配置子树(enabled 三态;viper 含点 key 手工提取)、Runtime 状态机 - internal/plugin/plugintest:模块测试夹具(NewHost Options + RunLifecycle) - internal/modules:standard/imports.go 唯一插装清单 + job.hello 示例模块(默认 disabled) - Wire/main 接入:Build+Start 失败 fail-fast(显式 Cleanup 防 leader 锁泄漏)、 runtime.Stop 在 cleanup 并行组首位(先于 Redis/Ent) - admin 可观测:GET /api/v1/admin/modules(只读,错误文本脱敏) - 默认配置零行为变更(Phase-0 安全网 + bench allocs 零漂移验证) 含两轮对抗式审计修复(1×P1 启动失败泄漏 leader 锁 + 11×P2)与回归测试。
334 lines
8.1 KiB
Go
334 lines
8.1 KiB
Go
//go:build wireinject
|
|
// +build wireinject
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/ent"
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/Wei-Shaw/sub2api/internal/handler"
|
|
"github.com/Wei-Shaw/sub2api/internal/payment"
|
|
"github.com/Wei-Shaw/sub2api/internal/plugin"
|
|
"github.com/Wei-Shaw/sub2api/internal/repository"
|
|
"github.com/Wei-Shaw/sub2api/internal/server"
|
|
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
|
|
"github.com/google/wire"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Application struct {
|
|
Server *http.Server
|
|
// Runtime 是插件模块生命周期驱动器:main 在 HTTP server 启动前
|
|
// 调用 Build + Start,关闭时由 Cleanup 序列执行 Stop。
|
|
Runtime *plugin.Runtime
|
|
Cleanup func()
|
|
}
|
|
|
|
func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
wire.Build(
|
|
// Infrastructure layer ProviderSets
|
|
config.ProviderSet,
|
|
|
|
// Business layer ProviderSets
|
|
repository.ProviderSet,
|
|
service.ProviderSet,
|
|
payment.ProviderSet,
|
|
middleware.ProviderSet,
|
|
handler.ProviderSet,
|
|
|
|
// Server layer ProviderSet
|
|
server.ProviderSet,
|
|
|
|
// Plugin kernel ProviderSet (Host + module Runtime)
|
|
plugin.ProviderSet,
|
|
|
|
// Privacy client factory for OpenAI training opt-out
|
|
providePrivacyClientFactory,
|
|
|
|
// BuildInfo provider
|
|
provideServiceBuildInfo,
|
|
|
|
// Cleanup function provider
|
|
provideCleanup,
|
|
|
|
// Application struct
|
|
wire.Struct(new(Application), "Server", "Runtime", "Cleanup"),
|
|
)
|
|
return nil, nil
|
|
}
|
|
|
|
func providePrivacyClientFactory() service.PrivacyClientFactory {
|
|
return repository.CreatePrivacyReqClient
|
|
}
|
|
|
|
func provideServiceBuildInfo(buildInfo handler.BuildInfo) service.BuildInfo {
|
|
return service.BuildInfo{
|
|
Version: buildInfo.Version,
|
|
BuildType: buildInfo.BuildType,
|
|
}
|
|
}
|
|
|
|
func provideCleanup(
|
|
entClient *ent.Client,
|
|
rdb *redis.Client,
|
|
opsMetricsCollector *service.OpsMetricsCollector,
|
|
opsAggregation *service.OpsAggregationService,
|
|
opsAlertEvaluator *service.OpsAlertEvaluatorService,
|
|
opsCleanup *service.OpsCleanupService,
|
|
opsScheduledReport *service.OpsScheduledReportService,
|
|
opsSystemLogSink *service.OpsSystemLogSink,
|
|
schedulerSnapshot *service.SchedulerSnapshotService,
|
|
tokenRefresh *service.TokenRefreshService,
|
|
accountExpiry *service.AccountExpiryService,
|
|
proxyExpiry *service.ProxyExpiryService,
|
|
subscriptionExpiry *service.SubscriptionExpiryService,
|
|
usageCleanup *service.UsageCleanupService,
|
|
idempotencyCleanup *service.IdempotencyCleanupService,
|
|
pricing *service.PricingService,
|
|
emailQueue *service.EmailQueueService,
|
|
billingCache *service.BillingCacheService,
|
|
usageRecordWorkerPool *service.UsageRecordWorkerPool,
|
|
subscriptionService *service.SubscriptionService,
|
|
oauth *service.OAuthService,
|
|
openaiOAuth *service.OpenAIOAuthService,
|
|
geminiOAuth *service.GeminiOAuthService,
|
|
antigravityOAuth *service.AntigravityOAuthService,
|
|
openAIGateway *service.OpenAIGatewayService,
|
|
scheduledTestRunner *service.ScheduledTestRunnerService,
|
|
backupSvc *service.BackupService,
|
|
paymentOrderExpiry *service.PaymentOrderExpiryService,
|
|
channelMonitorRunner *service.ChannelMonitorRunner,
|
|
quotaFlusher *service.UserPlatformQuotaUsageFlusher,
|
|
moduleRuntime *plugin.Runtime,
|
|
) func() {
|
|
return func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
type cleanupStep struct {
|
|
name string
|
|
fn func() error
|
|
}
|
|
|
|
// 应用层清理步骤可并行执行,基础设施资源(Redis/Ent)最后按顺序关闭。
|
|
parallelSteps := []cleanupStep{
|
|
// 插件模块 Stop 属于业务服务关闭组:模块停止时可能仍需写 DB/Redis,
|
|
// 必须先于基础设施(Redis/Ent)关闭执行。
|
|
{"PluginModuleRuntime", func() error {
|
|
if moduleRuntime == nil {
|
|
return nil
|
|
}
|
|
return moduleRuntime.Stop(ctx)
|
|
}},
|
|
{"OpsScheduledReportService", func() error {
|
|
if opsScheduledReport != nil {
|
|
opsScheduledReport.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsCleanupService", func() error {
|
|
if opsCleanup != nil {
|
|
opsCleanup.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsSystemLogSink", func() error {
|
|
if opsSystemLogSink != nil {
|
|
opsSystemLogSink.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsAlertEvaluatorService", func() error {
|
|
if opsAlertEvaluator != nil {
|
|
opsAlertEvaluator.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsAggregationService", func() error {
|
|
if opsAggregation != nil {
|
|
opsAggregation.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsMetricsCollector", func() error {
|
|
if opsMetricsCollector != nil {
|
|
opsMetricsCollector.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"SchedulerSnapshotService", func() error {
|
|
if schedulerSnapshot != nil {
|
|
schedulerSnapshot.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"UsageCleanupService", func() error {
|
|
if usageCleanup != nil {
|
|
usageCleanup.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"IdempotencyCleanupService", func() error {
|
|
if idempotencyCleanup != nil {
|
|
idempotencyCleanup.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"TokenRefreshService", func() error {
|
|
tokenRefresh.Stop()
|
|
return nil
|
|
}},
|
|
{"AccountExpiryService", func() error {
|
|
accountExpiry.Stop()
|
|
return nil
|
|
}},
|
|
{"ProxyExpiryService", func() error {
|
|
proxyExpiry.Stop()
|
|
return nil
|
|
}},
|
|
{"SubscriptionExpiryService", func() error {
|
|
subscriptionExpiry.Stop()
|
|
return nil
|
|
}},
|
|
{"SubscriptionService", func() error {
|
|
if subscriptionService != nil {
|
|
subscriptionService.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"PricingService", func() error {
|
|
pricing.Stop()
|
|
return nil
|
|
}},
|
|
{"EmailQueueService", func() error {
|
|
emailQueue.Stop()
|
|
return nil
|
|
}},
|
|
{"BillingCacheService", func() error {
|
|
billingCache.Stop()
|
|
return nil
|
|
}},
|
|
{"UsageRecordWorkerPool", func() error {
|
|
if usageRecordWorkerPool != nil {
|
|
usageRecordWorkerPool.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OAuthService", func() error {
|
|
oauth.Stop()
|
|
return nil
|
|
}},
|
|
{"OpenAIOAuthService", func() error {
|
|
openaiOAuth.Stop()
|
|
return nil
|
|
}},
|
|
{"GeminiOAuthService", func() error {
|
|
geminiOAuth.Stop()
|
|
return nil
|
|
}},
|
|
{"AntigravityOAuthService", func() error {
|
|
antigravityOAuth.Stop()
|
|
return nil
|
|
}},
|
|
{"OpenAIWSPool", func() error {
|
|
if openAIGateway != nil {
|
|
openAIGateway.CloseOpenAIWSPool()
|
|
}
|
|
return nil
|
|
}},
|
|
{"ScheduledTestRunnerService", func() error {
|
|
if scheduledTestRunner != nil {
|
|
scheduledTestRunner.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"BackupService", func() error {
|
|
if backupSvc != nil {
|
|
backupSvc.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"PaymentOrderExpiryService", func() error {
|
|
if paymentOrderExpiry != nil {
|
|
paymentOrderExpiry.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"ChannelMonitorRunner", func() error {
|
|
if channelMonitorRunner != nil {
|
|
channelMonitorRunner.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"UserPlatformQuotaUsageFlusher", func() error {
|
|
if quotaFlusher != nil {
|
|
quotaFlusher.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
}
|
|
|
|
infraSteps := []cleanupStep{
|
|
{"Redis", func() error {
|
|
if rdb == nil {
|
|
return nil
|
|
}
|
|
return rdb.Close()
|
|
}},
|
|
{"Ent", func() error {
|
|
if entClient == nil {
|
|
return nil
|
|
}
|
|
return entClient.Close()
|
|
}},
|
|
}
|
|
|
|
runParallel := func(steps []cleanupStep) {
|
|
var wg sync.WaitGroup
|
|
for i := range steps {
|
|
step := steps[i]
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
if err := step.fn(); err != nil {
|
|
log.Printf("[Cleanup] %s failed: %v", step.name, err)
|
|
return
|
|
}
|
|
log.Printf("[Cleanup] %s succeeded", step.name)
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
runSequential := func(steps []cleanupStep) {
|
|
for i := range steps {
|
|
step := steps[i]
|
|
if err := step.fn(); err != nil {
|
|
log.Printf("[Cleanup] %s failed: %v", step.name, err)
|
|
continue
|
|
}
|
|
log.Printf("[Cleanup] %s succeeded", step.name)
|
|
}
|
|
}
|
|
|
|
runParallel(parallelSteps)
|
|
runSequential(infraSteps)
|
|
|
|
// Check if context timed out
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Printf("[Cleanup] Warning: cleanup timed out after 10 seconds")
|
|
default:
|
|
log.Printf("[Cleanup] All cleanup steps completed")
|
|
}
|
|
}
|
|
}
|