Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53b414d

Browse files
authored
Merge pull request edgexfoundry#819 from jackchenjc/issue-468
feat: Passing callback func to get message client in WatchForChanges
2 parents a0a2198 + b6bb7df commit 53b414d

File tree

3 files changed

+33
-66
lines changed

3 files changed

+33
-66
lines changed

bootstrap/config/config.go

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*******************************************************************************
22
* Copyright 2019 Dell Inc.
33
* Copyright 2023 Intel Corporation
4-
* Copyright 2024 IOTech Ltd
4+
* Copyright 2024-2025 IOTech Ltd
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
77
* in compliance with the License. You may obtain a copy of the License at
@@ -65,8 +65,6 @@ const (
6565
deviceServicesKey = "device-services"
6666

6767
SecurityModeKey = "Mode"
68-
69-
configProviderTypeKeeper = "keeper"
7068
)
7169

7270
var invalidRemoteHostsError = errors.New("-rsh/--remoteServiceHosts must contain 3 and only 3 comma seperated host names")
@@ -270,16 +268,16 @@ func (cp *Processor) Process(
270268

271269
// listen for changes on Writable
272270
if useProvider {
273-
cp.listenForPrivateChanges(serviceConfig, privateConfigClient, utils.BuildBaseKey(configStem, serviceKey), configProviderInfo.ServiceConfig().Type)
271+
cp.listenForPrivateChanges(serviceConfig, privateConfigClient, utils.BuildBaseKey(configStem, serviceKey))
274272
cp.lc.Infof("listening for private config changes")
275-
cp.listenForCommonChanges(serviceConfig, cp.commonConfigClient, privateConfigClient, utils.BuildBaseKey(configStem, common.CoreCommonConfigServiceKey, allServicesKey), configProviderInfo.ServiceConfig().Type)
273+
cp.listenForCommonChanges(serviceConfig, cp.commonConfigClient, privateConfigClient, utils.BuildBaseKey(configStem, common.CoreCommonConfigServiceKey, allServicesKey))
276274
cp.lc.Infof("listening for all services common config changes")
277275
if cp.appConfigClient != nil {
278-
cp.listenForCommonChanges(serviceConfig, cp.appConfigClient, privateConfigClient, utils.BuildBaseKey(configStem, common.CoreCommonConfigServiceKey, appServicesKey), configProviderInfo.ServiceConfig().Type)
276+
cp.listenForCommonChanges(serviceConfig, cp.appConfigClient, privateConfigClient, utils.BuildBaseKey(configStem, common.CoreCommonConfigServiceKey, appServicesKey))
279277
cp.lc.Infof("listening for application service common config changes")
280278
}
281279
if cp.deviceConfigClient != nil {
282-
cp.listenForCommonChanges(serviceConfig, cp.deviceConfigClient, privateConfigClient, utils.BuildBaseKey(configStem, common.CoreCommonConfigServiceKey, deviceServicesKey), configProviderInfo.ServiceConfig().Type)
280+
cp.listenForCommonChanges(serviceConfig, cp.deviceConfigClient, privateConfigClient, utils.BuildBaseKey(configStem, common.CoreCommonConfigServiceKey, deviceServicesKey))
283281
cp.lc.Infof("listening for device service common config changes")
284282
}
285283
}
@@ -652,28 +650,7 @@ func (cp *Processor) ListenForCustomConfigChanges(
652650
updateStream := make(chan any)
653651
defer close(updateStream)
654652

655-
var messageBus messaging.MessageClient
656-
configProviderUrl := cp.flags.ConfigProviderUrl()
657-
// check if the config provider type is keeper
658-
if strings.HasPrefix(configProviderUrl, configProviderTypeKeeper) {
659-
// there's no startupTimer for cp created by NewProcessorForCustomConfig
660-
// add a new startupTimer here
661-
if !cp.startupTimer.HasNotElapsed() {
662-
cp.startupTimer = startup.NewStartUpTimer("")
663-
}
664-
for cp.startupTimer.HasNotElapsed() {
665-
if msgClient := container.MessagingClientFrom(cp.dic.Get); msgClient != nil {
666-
messageBus = msgClient
667-
break
668-
}
669-
cp.startupTimer.SleepForInterval()
670-
}
671-
if messageBus == nil {
672-
cp.lc.Error("unable to use MessageClient to watch for custom configuration changes")
673-
return
674-
}
675-
}
676-
go configClient.WatchForChanges(updateStream, errorStream, configToWatch, sectionName, messageBus)
653+
go configClient.WatchForChanges(updateStream, errorStream, configToWatch, sectionName, cp.getMessageClient)
677654

678655
isFirstUpdate := true
679656

@@ -772,7 +749,7 @@ func GetConfigFileLocation(lc logger.LoggingClient, flags flags.Common) string {
772749
// service's configuration writable sub-struct. It's assumed the log level is universally part of the
773750
// writable struct and this function explicitly updates the loggingClient's log level when new configuration changes
774751
// are received.
775-
func (cp *Processor) listenForPrivateChanges(serviceConfig interfaces.Configuration, configClient configuration.Client, baseKey string, configProviderType string) {
752+
func (cp *Processor) listenForPrivateChanges(serviceConfig interfaces.Configuration, configClient configuration.Client, baseKey string) {
776753
lc := cp.lc
777754
isFirstUpdate := true
778755

@@ -786,22 +763,7 @@ func (cp *Processor) listenForPrivateChanges(serviceConfig interfaces.Configurat
786763
updateStream := make(chan any)
787764
defer close(updateStream)
788765

789-
// get the MessageClient to be used in Keeper WatchForChanges method
790-
var messageBus messaging.MessageClient
791-
if configProviderType == configProviderTypeKeeper {
792-
for cp.startupTimer.HasNotElapsed() {
793-
if msgClient := container.MessagingClientFrom(cp.dic.Get); msgClient != nil {
794-
messageBus = msgClient
795-
break
796-
}
797-
cp.startupTimer.SleepForInterval()
798-
}
799-
if messageBus == nil {
800-
lc.Error("unable to use MessageClient to watch for configuration changes")
801-
return
802-
}
803-
}
804-
go configClient.WatchForChanges(updateStream, errorStream, serviceConfig.EmptyWritablePtr(), writableKey, messageBus)
766+
go configClient.WatchForChanges(updateStream, errorStream, serviceConfig.EmptyWritablePtr(), writableKey, cp.getMessageClient)
805767

806768
for {
807769
select {
@@ -844,7 +806,7 @@ func (cp *Processor) listenForPrivateChanges(serviceConfig interfaces.Configurat
844806
// listenForCommonChanges leverages the Configuration Provider client's WatchForChanges() method to receive changes to and update the
845807
// service's common configuration writable sub-struct.
846808
func (cp *Processor) listenForCommonChanges(fullServiceConfig interfaces.Configuration, configClient configuration.Client,
847-
privateConfigClient configuration.Client, baseKey string, configProviderType string) {
809+
privateConfigClient configuration.Client, baseKey string) {
848810
lc := cp.lc
849811
isFirstUpdate := true
850812
baseKey = utils.BuildBaseKey(baseKey, writableKey)
@@ -863,22 +825,7 @@ func (cp *Processor) listenForCommonChanges(fullServiceConfig interfaces.Configu
863825
updateStream := make(chan any)
864826
defer close(updateStream)
865827

866-
// get the MessageClient to be used in Keeper WatchForChanges method
867-
var messageBus messaging.MessageClient
868-
if configProviderType == configProviderTypeKeeper {
869-
for cp.startupTimer.HasNotElapsed() {
870-
if msgClient := container.MessagingClientFrom(cp.dic.Get); msgClient != nil {
871-
messageBus = msgClient
872-
break
873-
}
874-
cp.startupTimer.SleepForInterval()
875-
}
876-
if messageBus == nil {
877-
lc.Error("unable to use MessageClient to watch for configuration changes")
878-
return
879-
}
880-
}
881-
go commonConfigClient.WatchForChanges(updateStream, errorStream, fullServiceConfig.EmptyWritablePtr(), writableKey, messageBus)
828+
go commonConfigClient.WatchForChanges(updateStream, errorStream, fullServiceConfig.EmptyWritablePtr(), writableKey, cp.getMessageClient)
882829

883830
for {
884831
select {
@@ -1129,6 +1076,26 @@ func (cp *Processor) loadConfigFromProvider(serviceConfig interfaces.Configurati
11291076
return nil
11301077
}
11311078

1079+
// getMessageClient waits and gets the message client
1080+
func (cp *Processor) getMessageClient() (msgClient messaging.MessageClient) {
1081+
// there's no startupTimer for cp created by NewProcessorForCustomConfig
1082+
// add a new startupTimer here
1083+
if !cp.startupTimer.HasNotElapsed() {
1084+
cp.startupTimer = startup.NewStartUpTimer("")
1085+
}
1086+
for cp.startupTimer.HasNotElapsed() {
1087+
if msgClient = container.MessagingClientFrom(cp.dic.Get); msgClient != nil {
1088+
break
1089+
}
1090+
cp.startupTimer.SleepForInterval()
1091+
}
1092+
if msgClient == nil {
1093+
cp.lc.Error("unable to use MessageClient to watch for configuration changes")
1094+
return nil
1095+
}
1096+
return msgClient
1097+
}
1098+
11321099
// getSecretNamesChanged returns a slice of secretNames that have changed secrets or are new.
11331100
func getSecretNamesChanged(prevVals config.InsecureSecrets, curVals config.InsecureSecrets) []string {
11341101
var updatedNames []string

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.23
44

55
require (
66
github.com/eclipse/paho.mqtt.golang v1.5.0
7-
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.10
7+
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.11
88
github.com/edgexfoundry/go-mod-core-contracts/v4 v4.0.0-dev.22
99
github.com/edgexfoundry/go-mod-messaging/v4 v4.0.0-dev.10
1010
github.com/edgexfoundry/go-mod-registry/v4 v4.0.0-dev.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
6868
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6969
github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o=
7070
github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk=
71-
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.10 h1:DMv5LZDxcqUeb1dREMd/vK+reXmZYlpafgtm8XhYdHQ=
72-
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.10/go.mod h1:ltUpMcOpJSzmabBtZox5qg1AK2wEikvZJyIBXtJ7mUQ=
71+
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.11 h1:VCDeyEwhSb01dhJ3Rw0DbtV8pc18TILAoUGSFn7NR64=
72+
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.11/go.mod h1:ltUpMcOpJSzmabBtZox5qg1AK2wEikvZJyIBXtJ7mUQ=
7373
github.com/edgexfoundry/go-mod-core-contracts/v4 v4.0.0-dev.22 h1:XzDtbAmvp/v+DZlFoSgttnUQsIXGhlf+H8xgGxBClUA=
7474
github.com/edgexfoundry/go-mod-core-contracts/v4 v4.0.0-dev.22/go.mod h1:D35HIMZkFFy82shKtPYaEL3Nn+ZNEjUjZI1RLn1j23E=
7575
github.com/edgexfoundry/go-mod-messaging/v4 v4.0.0-dev.10 h1:xvDQDIJtmj/ZCmKzbAzg3h1F2ZdWz1MPoJSNfYZANGc=

0 commit comments

Comments
 (0)