@@ -36,8 +36,8 @@ import (
3636
3737//errors
3838var (
39- ErrKeyNotExist = errors .New ("key does not exist" )
40- ErrIgnoreChange = errors .New ("ignore key changed" )
39+ ErrKeyNotExist = errors .New ("key does not exist" )
40+ ErrIgnoreChange = errors .New ("ignore key changed" )
4141 ErrWriterInvalid = errors .New ("writer is invalid" )
4242)
4343
@@ -55,6 +55,8 @@ type Manager struct {
5555 sourceMapMux sync.RWMutex
5656 Sources map [string ]ConfigSource
5757
58+ ConfigValueCache sync.Map
59+
5860 ConfigurationMap sync.Map
5961
6062 dispatcher * event.Dispatcher
@@ -79,6 +81,9 @@ func (m *Manager) Cleanup() error {
7981 return err
8082 }
8183 }
84+
85+ m .ConfigValueCache = sync.Map {}
86+
8287 return nil
8388}
8489
@@ -139,8 +144,8 @@ func (m *Manager) Marshal(w io.Writer) error {
139144 }
140145 allConfig := make (map [string ]map [string ]interface {})
141146 for name , source := range m .Sources {
142- config , err := source .GetConfigurations ()
143- if err != nil {
147+ config , err := source .GetConfigurations ()
148+ if err != nil {
144149 openlog .Error ("get source " + name + " error " + err .Error ())
145150 continue
146151 }
@@ -215,12 +220,11 @@ func (m *Manager) pullSourceConfigs(source string) error {
215220func (m * Manager ) Configs () map [string ]interface {} {
216221 config := make (map [string ]interface {}, 0 )
217222
218- m .ConfigurationMap .Range (func (key , value interface {}) bool {
219- sValue := m .configValueBySource (key .(string ), value .(string ))
220- if sValue == nil {
223+ m .ConfigValueCache .Range (func (key , value interface {}) bool {
224+ if value == nil {
221225 return true
222226 }
223- config [key .(string )] = sValue
227+ config [key .(string )] = value
224228 return true
225229 })
226230
@@ -236,8 +240,8 @@ func (m *Manager) ConfigsWithSourceNames() map[string]interface{} {
236240 config := make (map [string ]interface {}, 0 )
237241
238242 m .ConfigurationMap .Range (func (key , value interface {}) bool {
239- sValue := m .configValueBySource ( key .( string ), value .(string ))
240- if sValue == nil {
243+ sValue , ok := m .ConfigValueCache . Load ( key .(string ))
244+ if ! ok || sValue == nil {
241245 return true
242246 }
243247 // each key stores its value and source name
@@ -317,43 +321,23 @@ func (m *Manager) IsKeyExist(key string) bool {
317321
318322// GetConfig returns the value for a particular key from cache
319323func (m * Manager ) GetConfig (key string ) interface {} {
320- sourceName , ok := m .ConfigurationMap .Load (key )
321- if ! ok {
322- return nil
323- }
324- return m .configValueBySource (key , sourceName .(string ))
324+ val , _ := m .ConfigValueCache .Load (key )
325+ return val
325326}
326327
327328func (m * Manager ) updateConfigurationMap (source ConfigSource , configs map [string ]interface {}) error {
328329 for key := range configs {
329- sourceName , ok := m .ConfigurationMap .Load (key )
330- if ! ok { // if key do not exist then add source
331- m .ConfigurationMap .Store (key , source .GetSourceName ())
332- continue
333- }
334-
335- m .sourceMapMux .RLock ()
336- currentSource , ok := m .Sources [sourceName .(string )]
337- m .sourceMapMux .RUnlock ()
338- if ! ok {
339- m .ConfigurationMap .Store (key , source .GetSourceName ())
340- continue
341- }
342330
343- currentSrcPriority := currentSource . GetPriority ( )
344- if currentSrcPriority > source . GetPriority () { // lesser value has high priority
345- m . ConfigurationMap . Store ( key , source . GetSourceName ())
331+ val , err := source . GetConfigurationByKey ( key )
332+ if err != nil {
333+ return err
346334 }
347- }
348-
349- return nil
350- }
351335
352- func (m * Manager ) updateConfigurationMapByDI (source ConfigSource , configs map [string ]interface {}) error {
353- for key := range configs {
354336 sourceName , ok := m .ConfigurationMap .Load (key )
355337 if ! ok { // if key do not exist then add source
356338 m .ConfigurationMap .Store (key , source .GetSourceName ())
339+
340+ m .ConfigValueCache .Store (key , val )
357341 continue
358342 }
359343
@@ -362,12 +346,15 @@ func (m *Manager) updateConfigurationMapByDI(source ConfigSource, configs map[st
362346 m .sourceMapMux .RUnlock ()
363347 if ! ok {
364348 m .ConfigurationMap .Store (key , source .GetSourceName ())
349+
350+ m .ConfigValueCache .Store (key , val )
365351 continue
366352 }
367353
368354 currentSrcPriority := currentSource .GetPriority ()
369355 if currentSrcPriority > source .GetPriority () { // lesser value has high priority
370356 m .ConfigurationMap .Store (key , source .GetSourceName ())
357+ m .ConfigValueCache .Store (key , val )
371358 }
372359 }
373360
@@ -409,23 +396,39 @@ func (m *Manager) updateEvent(e *event.Event) error {
409396 return nil
410397 }
411398 openlog .Info ("config update event received" )
399+
412400 switch e .EventType {
413401 case event .Create , event .Update :
402+
414403 sourceName , ok := m .ConfigurationMap .Load (e .Key )
404+
405+ val := m .configValueBySource (e .Key , e .EventSource )
406+
415407 if ! ok {
416408 m .ConfigurationMap .Store (e .Key , e .EventSource )
409+
410+ m .ConfigValueCache .Store (e .Key , val )
411+
417412 e .EventType = event .Create
418413 } else if sourceName == e .EventSource {
414+
415+ m .ConfigValueCache .Store (e .Key , val )
416+
419417 e .EventType = event .Update
420418 } else if sourceName != e .EventSource {
419+
421420 prioritySrc := m .getHighPrioritySource (sourceName .(string ), e .EventSource )
421+
422422 if prioritySrc != nil && prioritySrc .GetSourceName () == sourceName {
423423 // if event generated from less priority source then ignore
424424 openlog .Info (fmt .Sprintf ("the event source %s's priority is less then %s's, ignore" ,
425425 e .EventSource , sourceName ))
426426 return ErrIgnoreChange
427427 }
428428 m .ConfigurationMap .Store (e .Key , e .EventSource )
429+
430+ m .ConfigValueCache .Store (e .Key , val )
431+
429432 e .EventType = event .Update
430433 }
431434
@@ -441,8 +444,12 @@ func (m *Manager) updateEvent(e *event.Event) error {
441444 source := m .findNextBestSource (e .Key , sourceName .(string ))
442445 if source == nil {
443446 m .ConfigurationMap .Delete (e .Key )
447+
448+ m .ConfigValueCache .Delete (e .Key )
444449 } else {
445450 m .ConfigurationMap .Store (e .Key , source .GetSourceName ())
451+
452+ m .ConfigValueCache .Store (e .Key , m .configValueBySource (e .Key , source .GetSourceName ()))
446453 }
447454 }
448455
0 commit comments