-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The global config support set the global config once per web server
- Loading branch information
Showing
5 changed files
with
128 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,52 @@ | ||
package conf | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
var ( | ||
globalConfigSet = &configSet{ | ||
m: make(map[string]*Config), | ||
} | ||
) | ||
|
||
var ( | ||
// GlobalConfig the global config of the program, initial by flags or config file | ||
GlobalConfig *Config | ||
errConfigIsNil = errors.New("the config is nil") | ||
errConfigExist = errors.New("the config exists") | ||
) | ||
|
||
type configSet struct { | ||
m map[string]*Config | ||
mu sync.RWMutex | ||
} | ||
|
||
func (cs *configSet) setGlobalConfig(c *Config) error { | ||
if c == nil { | ||
return errConfigIsNil | ||
} | ||
addr := c.FileServerAddr | ||
cs.mu.Lock() | ||
defer cs.mu.Unlock() | ||
if _, ok := cs.m[addr]; ok { | ||
return errConfigExist | ||
} | ||
cs.m[addr] = c | ||
return nil | ||
} | ||
|
||
func (cs *configSet) getGlobalConfig(addr string) *Config { | ||
cs.mu.RLock() | ||
defer cs.mu.RUnlock() | ||
return cs.m[addr] | ||
} | ||
|
||
// SetGlobalConfig set the global config once per web server | ||
func SetGlobalConfig(c *Config) error { | ||
return globalConfigSet.setGlobalConfig(c) | ||
} | ||
|
||
// GetGlobalConfig get the global config by web server address | ||
func GetGlobalConfig(addr string) *Config { | ||
return globalConfigSet.getGlobalConfig(addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package conf | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSetGlobalConfig(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
config *Config | ||
err error | ||
}{ | ||
{"normal address", &Config{FileServerAddr: ":8080"}, nil}, | ||
{"empty address", &Config{FileServerAddr: ""}, nil}, | ||
{"nil config", nil, errConfigIsNil}, | ||
{"config exists", &Config{FileServerAddr: ":8080"}, errConfigExist}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := SetGlobalConfig(tc.config) | ||
if err != tc.err { | ||
t.Errorf("expect to get error %v, but get %v", tc.err, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetGlobalConfig(t *testing.T) { | ||
if err := SetGlobalConfig(&Config{FileServerAddr: ":8088"}); err != nil { | ||
t.Errorf("call SetGlobalConfig error") | ||
return | ||
} | ||
testCases := []struct { | ||
name string | ||
address string | ||
exist bool | ||
}{ | ||
{"normal address", ":8088", true}, | ||
{"not exist config", ":8000", false}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
config := GetGlobalConfig(tc.address) | ||
exist := config != nil | ||
if exist != tc.exist { | ||
t.Errorf("expect to get config %v, but get %v", tc.exist, exist) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters