Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
fcc5c73b
authored
Sep 27, 2025
by
RedwindA
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 添加代理客户端缓存和重置功能,优化 HTTP 客户端管理
parent
1d4d243f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
5 deletions
+37
-5
service/http_client.go
+37
-5
No files found.
service/http_client.go
View file @
fcc5c73b
...
@@ -7,12 +7,17 @@ import (
...
@@ -7,12 +7,17 @@ import (
"net/http"
"net/http"
"net/url"
"net/url"
"one-api/common"
"one-api/common"
"sync"
"time"
"time"
"golang.org/x/net/proxy"
"golang.org/x/net/proxy"
)
)
var
httpClient
*
http
.
Client
var
(
httpClient
*
http
.
Client
proxyClientLock
sync
.
Mutex
proxyClients
=
make
(
map
[
string
]
*
http
.
Client
)
)
func
InitHttpClient
()
{
func
InitHttpClient
()
{
if
common
.
RelayTimeout
==
0
{
if
common
.
RelayTimeout
==
0
{
...
@@ -28,12 +33,31 @@ func GetHttpClient() *http.Client {
...
@@ -28,12 +33,31 @@ func GetHttpClient() *http.Client {
return
httpClient
return
httpClient
}
}
// ResetProxyClientCache 清空代理客户端缓存,确保下次使用时重新初始化
func
ResetProxyClientCache
()
{
proxyClientLock
.
Lock
()
defer
proxyClientLock
.
Unlock
()
for
_
,
client
:=
range
proxyClients
{
if
transport
,
ok
:=
client
.
Transport
.
(
*
http
.
Transport
);
ok
&&
transport
!=
nil
{
transport
.
CloseIdleConnections
()
}
}
proxyClients
=
make
(
map
[
string
]
*
http
.
Client
)
}
// NewProxyHttpClient 创建支持代理的 HTTP 客户端
// NewProxyHttpClient 创建支持代理的 HTTP 客户端
func
NewProxyHttpClient
(
proxyURL
string
)
(
*
http
.
Client
,
error
)
{
func
NewProxyHttpClient
(
proxyURL
string
)
(
*
http
.
Client
,
error
)
{
if
proxyURL
==
""
{
if
proxyURL
==
""
{
return
http
.
DefaultClient
,
nil
return
http
.
DefaultClient
,
nil
}
}
proxyClientLock
.
Lock
()
if
client
,
ok
:=
proxyClients
[
proxyURL
];
ok
{
proxyClientLock
.
Unlock
()
return
client
,
nil
}
proxyClientLock
.
Unlock
()
parsedURL
,
err
:=
url
.
Parse
(
proxyURL
)
parsedURL
,
err
:=
url
.
Parse
(
proxyURL
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -41,11 +65,15 @@ func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
...
@@ -41,11 +65,15 @@ func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
switch
parsedURL
.
Scheme
{
switch
parsedURL
.
Scheme
{
case
"http"
,
"https"
:
case
"http"
,
"https"
:
return
&
http
.
Client
{
client
:=
&
http
.
Client
{
Transport
:
&
http
.
Transport
{
Transport
:
&
http
.
Transport
{
Proxy
:
http
.
ProxyURL
(
parsedURL
),
Proxy
:
http
.
ProxyURL
(
parsedURL
),
},
},
},
nil
}
proxyClientLock
.
Lock
()
proxyClients
[
proxyURL
]
=
client
proxyClientLock
.
Unlock
()
return
client
,
nil
case
"socks5"
,
"socks5h"
:
case
"socks5"
,
"socks5h"
:
// 获取认证信息
// 获取认证信息
...
@@ -67,13 +95,17 @@ func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
...
@@ -67,13 +95,17 @@ func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
return
nil
,
err
return
nil
,
err
}
}
return
&
http
.
Client
{
client
:=
&
http
.
Client
{
Transport
:
&
http
.
Transport
{
Transport
:
&
http
.
Transport
{
DialContext
:
func
(
ctx
context
.
Context
,
network
,
addr
string
)
(
net
.
Conn
,
error
)
{
DialContext
:
func
(
ctx
context
.
Context
,
network
,
addr
string
)
(
net
.
Conn
,
error
)
{
return
dialer
.
Dial
(
network
,
addr
)
return
dialer
.
Dial
(
network
,
addr
)
},
},
},
},
},
nil
}
proxyClientLock
.
Lock
()
proxyClients
[
proxyURL
]
=
client
proxyClientLock
.
Unlock
()
return
client
,
nil
default
:
default
:
return
nil
,
fmt
.
Errorf
(
"unsupported proxy scheme: %s"
,
parsedURL
.
Scheme
)
return
nil
,
fmt
.
Errorf
(
"unsupported proxy scheme: %s"
,
parsedURL
.
Scheme
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment