@@ -19,13 +19,14 @@ package lock
1919
2020import (
2121 "context"
22+ "gorm.io/gorm"
23+ "math"
2224 "time"
2325
2426 "github.com/pkg/errors"
2527
2628 "github.com/apache/dubbo-admin/pkg/core/logger"
2729 "github.com/apache/dubbo-admin/pkg/core/runtime"
28- "github.com/apache/dubbo-admin/pkg/store/dbcommon"
2930)
3031
3132const (
@@ -49,48 +50,47 @@ func (c *Component) Type() runtime.ComponentType {
4950}
5051
5152// Order indicates the initialization order
52- // Lock should be initialized after Store (Order 100) but before other services
53+ // Lock should be initialized after Store (Order math.MaxInt - 1)
54+ // Higher order values are initialized first, so we use math.MaxInt - 2
5355func (c * Component ) Order () int {
54- return 90 // After Store, before Console
56+ return math . MaxInt - 2 // After Store, before other services
5557}
5658
5759// Init initializes the distributed lock component
5860func (c * Component ) Init (ctx runtime.BuilderContext ) error {
59- // Get the store component to access connection pool
61+ // Get the store component to access database connection
6062 storeComp , err := ctx .GetActivatedComponent (runtime .ResourceStore )
6163 if err != nil {
6264 return err
6365 }
6466
65- // Try to extract connection pool from store component
66- // We need to use type assertion with the proper interface
67- type ConnectionPoolProvider interface {
68- GetConnectionPool () * dbcommon. ConnectionPool
67+ // Try to extract database connection from store component
68+ // We use GetDB() interface to avoid circular dependency with dbcommon package
69+ type DBProvider interface {
70+ GetDB () * gorm. DB
6971 }
7072
71- storeWithPool , ok := storeComp .(ConnectionPoolProvider )
73+ storeWithDB , ok := storeComp .(DBProvider )
7274 if ! ok {
73- // For memory store or other stores without connection pool
74- logger .Warnf ("Store component does not provide connection pool , distributed lock will not be available" )
75+ // For memory store or other stores without database
76+ logger .Warnf ("Store component does not provide database connection , distributed lock will not be available" )
7577 return nil
7678 }
7779
78- pool := storeWithPool . GetConnectionPool ()
79- if pool == nil {
80- logger .Warnf ("Connection pool is nil, distributed lock will not be available" )
80+ db := storeWithDB . GetDB ()
81+ if db == nil {
82+ logger .Warnf ("Database connection is nil, distributed lock will not be available" )
8183 return nil
8284 }
8385
84- // Create GORM-based lock implementation using NewGormLock
85- c .lock = NewGormLock ( pool )
86+ // Create GORM-based lock implementation using NewGormLockFromDB
87+ c .lock = NewGormLockFromDB ( db )
8688
8789 // Initialize the lock table
88- db := pool .GetDB ()
8990 if err := db .AutoMigrate (& LockRecord {}); err != nil {
9091 return errors .Wrap (err , "failed to migrate lock table" )
9192 }
9293
93- logger .Info ("Distributed lock component initialized successfully" )
9494 return nil
9595}
9696
@@ -102,20 +102,17 @@ func (c *Component) Start(rt runtime.Runtime, stop <-chan struct{}) error {
102102 }
103103
104104 // Start background cleanup task
105- ticker := time .NewTicker (5 * time . Minute ) // Cleanup every 5 minutes
105+ ticker := time .NewTicker (DefaultCleanupInterval ) // Cleanup every 5 minutes
106106 defer ticker .Stop ()
107107
108- logger .Info ("Distributed lock cleanup task started" )
109-
110108 for {
111109 select {
112110 case <- stop :
113- logger .Info ("Distributed lock cleanup task stopped" )
114111 return nil
115112 case <- ticker .C :
116- ctx , cancel := context .WithTimeout (context .Background (), 30 * time . Second )
113+ ctx , cancel := context .WithTimeout (context .Background (), DefaultCleanupTimeout )
117114 if err := c .lock .CleanupExpiredLocks (ctx ); err != nil {
118- logger .Errorf ("Failed to cleanup expired locks: %v" , err )
115+ logger .Errorf ("Failed to cleanup expired locks: %v" , err )
119116 }
120117 cancel ()
121118 }
@@ -136,7 +133,6 @@ func GetLockFromRuntime(rt runtime.Runtime) (Lock, error) {
136133
137134 lockComp , ok := comp .(* Component )
138135 if ! ok {
139- // 修正:使用标准错误处理
140136 return nil , errors .Errorf ("component %s is not a valid lock component" , DistributedLockComponent )
141137 }
142138
0 commit comments