Skip to content

Commit 95cc3f2

Browse files
committed
Add timeout to request, using context
1 parent 15db043 commit 95cc3f2

4 files changed

Lines changed: 28 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ server = http://<user>:<pass>@<server host>:<server port>
294294
# 服务器的API地址,可以设置https或http
295295
heartbeat_interval = 5 # [可选] 心跳间隔,保持到服务器的连接,默认为30s。
296296
skip_verify = false # [可选] 跳过TLS检测,如果服务器使用的是自签证书,可以使用该项。
297+
request_timeout = 5 # [可选] 访问API的时间限制,默认为5s。
297298
298299
[ssh] # 一个section为一个对外暴露的服务
299300
type = tcp # 服务类型,目前支持tcp/http/https

client.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewServiceClient(svc Service, clientCfg *ClientCommonConf) *ServiceClient {
3333
svc: svc,
3434
cfg: clientCfg,
3535
client: &http.Client{
36-
Timeout: 5 * time.Second,
36+
Timeout: time.Duration(clientCfg.RequestTimeout * int64(time.Second)),
3737
},
3838
stopCh: make(chan struct{}),
3939
}
@@ -51,7 +51,7 @@ func NewServiceClient(svc Service, clientCfg *ClientCommonConf) *ServiceClient {
5151
}
5252
if c.listenProxy() {
5353
c.pClient = &http.Client{
54-
Timeout: 5 * time.Second,
54+
Timeout: time.Duration(clientCfg.RequestTimeout * int64(time.Second)),
5555
Transport: &http.Transport{
5656
Proxy: nil,
5757
DialContext: c.DialProxyAddr,
@@ -297,13 +297,29 @@ func (c *ServiceClient) refreshAPI(ctx context.Context) error {
297297
}
298298

299299
// Use the binded address to dial
300-
func (c *ServiceClient) DialBindAddr(_ context.Context, network string, addr string) (net.Conn, error) {
301-
return reuse.Dial("tcp", c.listener.Addr().String(), addr)
300+
func (c *ServiceClient) DialBindAddr(ctx context.Context, network string, addr string) (net.Conn, error) {
301+
nla, err := reuse.ResolveAddr(network, c.listener.Addr().String())
302+
if err != nil {
303+
return nil, fmt.Errorf("failed to resolve local addr: %w", err)
304+
}
305+
d := net.Dialer{
306+
Control: reuse.Control,
307+
LocalAddr: nla,
308+
}
309+
return d.DialContext(ctx, network, addr)
302310
}
303311

304312
// Use the binded address to dial
305-
func (c *ServiceClient) DialProxyAddr(_ context.Context, network string, addr string) (net.Conn, error) {
306-
return reuse.Dial("tcp", c.pListener.Addr().String(), addr)
313+
func (c *ServiceClient) DialProxyAddr(ctx context.Context, network string, addr string) (net.Conn, error) {
314+
nla, err := reuse.ResolveAddr(network, c.pListener.Addr().String())
315+
if err != nil {
316+
return nil, fmt.Errorf("failed to resolve local addr: %w", err)
317+
}
318+
d := net.Dialer{
319+
Control: reuse.Control,
320+
LocalAddr: nla,
321+
}
322+
return d.DialContext(ctx, network, addr)
307323
}
308324

309325
func (c *ServiceClient) Start(force bool) error {

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type ClientCommonConf struct {
1414
// before the connection is terminated, in seconds. It is not recommended
1515
// to change this value. By default, this value is 90.
1616
HeartbeatTimeout int64 `ini:"heartbeat_timeout" json:"heartbeat_timeout"`
17+
// RequestTimeout specifies the timeout for any API request in seconds.
18+
// By default, this value is 5.
19+
RequestTimeout int64 `ini:"request_timeout" json:"request_timeout"`
1720
}
1821

1922
func GetDefaultClientConf() ClientCommonConf {
@@ -22,6 +25,7 @@ func GetDefaultClientConf() ClientCommonConf {
2225
SkipTLSVerify: false,
2326
HeartbeatInterval: 30,
2427
HeartbeatTimeout: 90,
28+
RequestTimeout: 5,
2529
}
2630
}
2731

server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ type Server struct {
3939
httpMux map[string]*ServiceReflector
4040
httpsMux map[string]*ServiceReflector
4141
connGroup sync.WaitGroup
42+
listenerGroup sync.WaitGroup
4243
conns map[*net.Conn]struct{}
4344
listeners map[*net.Listener]struct{}
44-
listenerGroup sync.WaitGroup
4545
}
4646

4747
func NewServer(user, pass string, tlsConf *tls.Config) *Server {

0 commit comments

Comments
 (0)