Skip to content

Commit

Permalink
改进锁机制
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Apr 25, 2022
1 parent 970766b commit 3824b8f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions abstractions/default_viper_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,18 @@ func (c *Configuration) GetConfigObject(configTag string, configObject interface
} else {
_ = copier.CopyWithOption(configObject, object, copier.Option{IgnoreEmpty: true, DeepCopy: true})
}

}

func (c *Configuration) RefreshAll() {
c.gRWLock.Lock()
defer c.gRWLock.Unlock()
c.configMap = make(map[string]interface{})
c.gRWLock.Unlock()
}

func (c *Configuration) RefreshBy(name string) {
c.gRWLock.Lock()
defer c.gRWLock.Unlock()
delete(c.configMap, name)
c.gRWLock.Unlock()
}

/**
Expand All @@ -218,17 +217,19 @@ func (c *Configuration) bindEnvDSL(dslKey string, originalKey string) (interface
}
envKey := envKeyDefaultValue[0] // ${ENV:DEFAULT} [0] is key of the env
c.gRWLock.Lock()
defer c.gRWLock.Unlock()
// written lock
_ = viper.BindEnv(envKey) // binding environment for the env key
_ = viper.BindEnv(envKey) // binding environment for the env key
c.gRWLock.Unlock()
envValue := viper.Get(envKey) // get value of env
dslValue := envValue // dsl value is env value by default
// if env value is nil and dsl parameters length > 1
if envValue == nil && len(envKeyDefaultValue) > 1 {
dslValue = envKeyDefaultValue[1]
}
if originalKey != "" {
c.gRWLock.Lock()
c.config.Set(originalKey, dslValue)
c.gRWLock.Unlock()
}
return dslValue, nil
}
Expand Down

0 comments on commit 3824b8f

Please sign in to comment.