Skip to content

Commit 9eeded4

Browse files
committed
fix(extension): guard directory/customizer/shutdown/name mapping state
Fixes: #3247
1 parent 878966d commit 9eeded4

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

common/extension/graceful_shutdown.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package extension
1919

2020
import (
2121
"container/list"
22+
"sync"
2223
)
2324

2425
var customShutdownCallbacks = list.New()
26+
var customShutdownCallbacksLock sync.Mutex
2527

2628
/**
2729
* AddCustomShutdownCallback
@@ -44,10 +46,20 @@ var customShutdownCallbacks = list.New()
4446
* And it may introduce much complication for another users.
4547
*/
4648
func AddCustomShutdownCallback(callback func()) {
49+
customShutdownCallbacksLock.Lock()
50+
defer customShutdownCallbacksLock.Unlock()
51+
4752
customShutdownCallbacks.PushBack(callback)
4853
}
4954

5055
// GetAllCustomShutdownCallbacks gets all custom shutdown callbacks
5156
func GetAllCustomShutdownCallbacks() *list.List {
52-
return customShutdownCallbacks
57+
customShutdownCallbacksLock.Lock()
58+
defer customShutdownCallbacksLock.Unlock()
59+
60+
ret := list.New()
61+
for e := customShutdownCallbacks.Front(); e != nil; e = e.Next() {
62+
ret.PushBack(e.Value)
63+
}
64+
return ret
5365
}

common/extension/registry_directory.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,35 @@ import (
2222
)
2323

2424
import (
25+
"sync/atomic"
26+
2527
"dubbo.apache.org/dubbo-go/v3/cluster/directory"
2628
"dubbo.apache.org/dubbo-go/v3/common"
2729
"dubbo.apache.org/dubbo-go/v3/registry"
2830
)
2931

3032
type registryDirectory func(url *common.URL, registry registry.Registry) (directory.Directory, error)
3133

32-
var directories = make(map[string]registryDirectory)
33-
var defaultDirectory registryDirectory
34+
var directories = NewRegistry[registryDirectory]("registry directory")
35+
var defaultDirectory atomic.Value
3436

3537
// SetDefaultRegistryDirectory sets the default registryDirectory
3638
func SetDefaultRegistryDirectory(v registryDirectory) {
37-
defaultDirectory = v
39+
defaultDirectory.Store(v)
3840
}
3941

4042
// SetDirectory sets the default registryDirectory
4143
func SetDirectory(key string, v registryDirectory) {
42-
directories[key] = v
44+
directories.Register(key, v)
4345
}
4446

4547
// GetDefaultRegistryDirectory finds the registryDirectory with url and registry
4648
func GetDefaultRegistryDirectory(url *common.URL, registry registry.Registry) (directory.Directory, error) {
47-
if defaultDirectory == nil {
49+
v := defaultDirectory.Load()
50+
if v == nil {
4851
panic("registry directory is not existing, make sure you have import the package.")
4952
}
50-
return defaultDirectory(url, registry)
53+
return v.(registryDirectory)(url, registry)
5154
}
5255

5356
// GetDirectoryInstance finds the registryDirectory with url and registry
@@ -56,9 +59,10 @@ func GetDirectoryInstance(url *common.URL, registry registry.Registry) (director
5659
if key == "" {
5760
return GetDefaultRegistryDirectory(url, registry)
5861
}
59-
if directories[key] == nil {
62+
v, ok := directories.Get(key)
63+
if !ok {
6064
logger.Warn("registry directory " + key + " does not exist, make sure you have import the package, will use the default directory type.")
6165
return GetDefaultRegistryDirectory(url, registry)
6266
}
63-
return directories[key](url, registry)
67+
return v(url, registry)
6468
}

common/extension/service_instance_customizer.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,35 @@ package extension
1919

2020
import (
2121
"sort"
22+
"sync"
2223
)
2324

2425
import (
2526
"dubbo.apache.org/dubbo-go/v3/registry"
2627
)
2728

2829
var customizers = make([]registry.ServiceInstanceCustomizer, 0, 8)
30+
var customizersLock sync.RWMutex
2931

3032
// AddCustomizers will put the customizer into slices and then sort them;
3133
// this method will be invoked several time, so we sort them here.
3234
func AddCustomizers(cus registry.ServiceInstanceCustomizer) {
35+
customizersLock.Lock()
36+
defer customizersLock.Unlock()
37+
3338
customizers = append(customizers, cus)
3439
sort.Stable(customizerSlice(customizers))
3540
}
3641

3742
// GetCustomizers will return the sorted customizer
3843
// the result won't be nil
3944
func GetCustomizers() []registry.ServiceInstanceCustomizer {
40-
return customizers
45+
customizersLock.RLock()
46+
defer customizersLock.RUnlock()
47+
48+
ret := make([]registry.ServiceInstanceCustomizer, len(customizers))
49+
copy(ret, customizers)
50+
return ret
4151
}
4252

4353
type customizerSlice []registry.ServiceInstanceCustomizer

common/extension/service_name_mapping.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@
1818
package extension
1919

2020
import (
21+
"sync/atomic"
22+
2123
"dubbo.apache.org/dubbo-go/v3/metadata/mapping"
2224
)
2325

2426
type ServiceNameMappingCreator func() mapping.ServiceNameMapping
2527

26-
var globalNameMappingCreator ServiceNameMappingCreator
28+
var globalNameMappingCreator atomic.Value
2729

2830
func SetGlobalServiceNameMapping(nameMappingCreator ServiceNameMappingCreator) {
29-
globalNameMappingCreator = nameMappingCreator
31+
globalNameMappingCreator.Store(nameMappingCreator)
3032
}
3133

3234
func GetGlobalServiceNameMapping() mapping.ServiceNameMapping {
33-
return globalNameMappingCreator()
35+
v := globalNameMappingCreator.Load()
36+
if v == nil {
37+
panic("global service name mapping creator is not existing")
38+
}
39+
return v.(ServiceNameMappingCreator)()
3440
}

0 commit comments

Comments
 (0)