mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 18:49:23 +08:00
fix: 复用Client 优化网络性能
This commit is contained in:
@@ -10,45 +10,43 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var userAgent = "curl/8.0.1"
|
var (
|
||||||
|
ipv4HTTPClient = &http.Client{
|
||||||
func ipv4Transport() *http.Transport {
|
Transport: &http.Transport{
|
||||||
return &http.Transport{
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
d := net.Dialer{
|
||||||
d := net.Dialer{
|
Timeout: 15 * time.Second,
|
||||||
Timeout: 15 * time.Second,
|
KeepAlive: 30 * time.Second,
|
||||||
KeepAlive: 30 * time.Second,
|
}
|
||||||
}
|
return d.DialContext(ctx, "tcp4", addr) // 锁v4防止出现问题
|
||||||
return d.DialContext(ctx, "tcp4", addr) // 锁v4防止出现问题
|
},
|
||||||
|
MaxIdleConns: 10,
|
||||||
|
IdleConnTimeout: 30 * time.Second,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
},
|
},
|
||||||
MaxIdleConns: 10,
|
Timeout: 15 * time.Second,
|
||||||
IdleConnTimeout: 30 * time.Second,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
|
||||||
}
|
}
|
||||||
}
|
ipv6HTTPClient = &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
func ipv6Transport() *http.Transport {
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
return &http.Transport{
|
d := net.Dialer{
|
||||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
Timeout: 15 * time.Second,
|
||||||
d := net.Dialer{
|
KeepAlive: 30 * time.Second,
|
||||||
Timeout: 15 * time.Second,
|
}
|
||||||
KeepAlive: 30 * time.Second,
|
return d.DialContext(ctx, "tcp6", addr) // 锁v6防止出现问题
|
||||||
}
|
},
|
||||||
return d.DialContext(ctx, "tcp6", addr) // 锁v6防止出现问题
|
MaxIdleConns: 10,
|
||||||
|
IdleConnTimeout: 30 * time.Second,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
},
|
},
|
||||||
MaxIdleConns: 10,
|
Timeout: 15 * time.Second,
|
||||||
IdleConnTimeout: 30 * time.Second,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
|
||||||
}
|
}
|
||||||
}
|
userAgent = "curl/8.0.1"
|
||||||
|
)
|
||||||
|
|
||||||
func GetIPv4Address() (string, error) {
|
func GetIPv4Address() (string, error) {
|
||||||
client := &http.Client{
|
|
||||||
Transport: ipv4Transport(),
|
|
||||||
Timeout: 15 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
webAPIs := []string{
|
webAPIs := []string{
|
||||||
"https://www.visa.cn/cdn-cgi/trace",
|
"https://www.visa.cn/cdn-cgi/trace",
|
||||||
@@ -67,7 +65,7 @@ func GetIPv4Address() (string, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", userAgent)
|
req.Header.Set("User-Agent", userAgent)
|
||||||
resp, err := client.Do(req)
|
resp, err := ipv4HTTPClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -87,10 +85,6 @@ func GetIPv4Address() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetIPv6Address() (string, error) {
|
func GetIPv6Address() (string, error) {
|
||||||
client := &http.Client{
|
|
||||||
Transport: ipv6Transport(), // 对于v6请求只使用v6
|
|
||||||
Timeout: 15 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
webAPIs := []string{
|
webAPIs := []string{
|
||||||
"https://v6.ip.zxinc.org/info.php?type=json",
|
"https://v6.ip.zxinc.org/info.php?type=json",
|
||||||
@@ -106,7 +100,7 @@ func GetIPv6Address() (string, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", userAgent)
|
req.Header.Set("User-Agent", userAgent)
|
||||||
resp, err := client.Do(req)
|
resp, err := ipv6HTTPClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user