Skip to content

Commit 8514bc3

Browse files
committed
Use client cache to reduce the number of client connections
1 parent 596ad4f commit 8514bc3

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pkg/cloud/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type SecretConfig struct {
6666
StringData Config `yaml:"stringData"`
6767
}
6868

69+
var clientCache = map[Config]*client{}
70+
6971
// UnmarshalAllSecretConfigs parses a yaml document for each secret.
7072
func UnmarshalAllSecretConfigs(in []byte, out *[]SecretConfig) error {
7173
r := bytes.NewReader(in)
@@ -134,6 +136,10 @@ func NewClientFromYamlPath(confPath string, secretName string) (Client, error) {
134136

135137
// Creates a new Cloud Client form a map of strings to strings.
136138
func NewClientFromConf(conf Config) (Client, error) {
139+
if client, exists := clientCache[conf]; exists {
140+
return client, nil
141+
}
142+
137143
verifySSL := true
138144
if conf.VerifySSL == "false" {
139145
verifySSL = false
@@ -146,6 +152,8 @@ func NewClientFromConf(conf Config) (Client, error) {
146152
c.cs = cloudstack.NewAsyncClient(conf.APIUrl, conf.APIKey, conf.SecretKey, verifySSL)
147153
c.csAsync = cloudstack.NewClient(conf.APIUrl, conf.APIKey, conf.SecretKey, verifySSL)
148154
c.customMetrics = metrics.NewCustomMetrics()
155+
clientCache[conf] = c
156+
149157
return c, nil
150158
}
151159

0 commit comments

Comments
 (0)