Skip to content

Commit cc8dc71

Browse files
committed
fix: config global cache
Signed-off-by: jiuxia211 <2064166368@qq.com>
1 parent f9dcbe5 commit cc8dc71

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

config.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,40 @@ import (
2222
"net/http"
2323
"net/url"
2424
"os"
25+
"sync"
2526

2627
"github.com/west2-online/jwch/constants"
2728
"github.com/west2-online/jwch/errno"
2829
)
2930

31+
// 全局缓存,避免重复获取隧道地址
32+
var (
33+
globalConfig *Config
34+
globalConfigOnce sync.Once
35+
)
36+
3037
// LoadConfigFromEnv 从环境变量加载配置
3138
func LoadConfigFromEnv() *Config {
32-
config := &Config{
33-
Proxy: ProxyConfig{
34-
Enabled: false,
35-
},
36-
}
37-
38-
// 从环境变量读取代理配置
39-
if authKey := os.Getenv("QINGGUO_AUTH_KEY"); authKey != "" {
40-
config.Proxy.AuthKey = authKey
41-
}
42-
if authPwd := os.Getenv("QINGGUO_AUTH_PWD"); authPwd != "" {
43-
config.Proxy.AuthPwd = authPwd
44-
}
45-
if enabled := os.Getenv("QINGGUO_PROXY_ENABLED"); enabled == "true" {
46-
config.Proxy.Enabled = true
47-
}
48-
49-
return config
39+
globalConfigOnce.Do(func() {
40+
globalConfig = &Config{
41+
Proxy: ProxyConfig{
42+
Enabled: false,
43+
},
44+
}
45+
46+
// 从环境变量读取代理配置
47+
if authKey := os.Getenv("QINGGUO_AUTH_KEY"); authKey != "" {
48+
globalConfig.Proxy.AuthKey = authKey
49+
}
50+
if authPwd := os.Getenv("QINGGUO_AUTH_PWD"); authPwd != "" {
51+
globalConfig.Proxy.AuthPwd = authPwd
52+
}
53+
if enabled := os.Getenv("QINGGUO_PROXY_ENABLED"); enabled == "true" {
54+
globalConfig.Proxy.Enabled = true
55+
}
56+
})
57+
58+
return globalConfig
5059
}
5160

5261
// GetTunnelAddress 获取青果网络隧道地址

jwch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"crypto/tls"
2222
"encoding/json"
23+
"fmt"
2324
"net/http"
2425
"strings"
2526

@@ -46,7 +47,7 @@ func NewStudent() *Student {
4647
if config.Proxy.Enabled {
4748
_, err := config.GetTunnelAddress()
4849
if err == nil && config.Proxy.ProxyServer != "" {
49-
// fmt.Println("proxy server", config.Proxy.ProxyServer)
50+
fmt.Println("proxy server", config.Proxy.ProxyServer)
5051
proxyURL, err := config.GetProxyURL()
5152
if err == nil {
5253
transport.Proxy = http.ProxyURL(proxyURL)

0 commit comments

Comments
 (0)