1818package service
1919
2020import (
21+ "github.com/apache/dubbo-admin/pkg/common/constants"
22+ "github.com/apache/dubbo-admin/pkg/core/lock"
2123 "github.com/duke-git/lancet/v2/slice"
2224
2325 "github.com/apache/dubbo-admin/pkg/common/bizerror"
@@ -114,6 +116,17 @@ func GetConfigurator(ctx consolectx.Context, name string, mesh string) (*meshres
114116}
115117
116118func UpdateConfigurator (ctx consolectx.Context , res * meshresource.DynamicConfigResource ) error {
119+ lockMgr := ctx .LockManager ()
120+ if lockMgr == nil {
121+ return updateConfiguratorUnsafe (ctx , res )
122+ }
123+ lockKey := lock .BuildConfiguratorRuleLockKey (res .Mesh , res .Name )
124+ return lockMgr .WithLock (ctx .AppContext (), lockKey , constants .DefaultLockTimeout , func () error {
125+ return updateConfiguratorUnsafe (ctx , res )
126+ })
127+ }
128+
129+ func updateConfiguratorUnsafe (ctx consolectx.Context , res * meshresource.DynamicConfigResource ) error {
117130 if err := ctx .ResourceManager ().Update (res ); err != nil {
118131 logger .Warnf ("update %s configurator failed with error: %s" , res .Name , err .Error ())
119132 return err
@@ -122,6 +135,17 @@ func UpdateConfigurator(ctx consolectx.Context, res *meshresource.DynamicConfigR
122135}
123136
124137func CreateConfigurator (ctx consolectx.Context , res * meshresource.DynamicConfigResource ) error {
138+ lockMgr := ctx .LockManager ()
139+ if lockMgr == nil {
140+ return createConfiguratorUnsafe (ctx , res )
141+ }
142+ lockKey := lock .BuildConfiguratorRuleLockKey (res .Mesh , res .Name )
143+ return lockMgr .WithLock (ctx .AppContext (), lockKey , constants .DefaultLockTimeout , func () error {
144+ return createConfiguratorUnsafe (ctx , res )
145+ })
146+ }
147+
148+ func createConfiguratorUnsafe (ctx consolectx.Context , res * meshresource.DynamicConfigResource ) error {
125149 if err := ctx .ResourceManager ().Add (res ); err != nil {
126150 logger .Warnf ("create %s configurator failed with error: %s" , res .Name , err .Error ())
127151 return err
@@ -130,6 +154,17 @@ func CreateConfigurator(ctx consolectx.Context, res *meshresource.DynamicConfigR
130154}
131155
132156func DeleteConfigurator (ctx consolectx.Context , name string , mesh string ) error {
157+ lockMgr := ctx .LockManager ()
158+ if lockMgr == nil {
159+ return deleteConfiguratorUnsafe (ctx , name , mesh )
160+ }
161+ lockKey := lock .BuildConfiguratorRuleLockKey (mesh , name )
162+ return lockMgr .WithLock (ctx .AppContext (), lockKey , constants .DefaultLockTimeout , func () error {
163+ return deleteConfiguratorUnsafe (ctx , name , mesh )
164+ })
165+ }
166+
167+ func deleteConfiguratorUnsafe (ctx consolectx.Context , name string , mesh string ) error {
133168 if err := ctx .ResourceManager ().DeleteByKey (meshresource .DynamicConfigKind , mesh , coremodel .BuildResourceKey (mesh , name )); err != nil {
134169 logger .Warnf ("delete %s configurator failed with error: %s" , name , err .Error ())
135170 return err
0 commit comments