fix timeout for router

This commit is contained in:
ginuerzh
2024-06-25 20:37:08 +08:00
parent ca340b1bf1
commit 4e831b95e8
5 changed files with 25 additions and 39 deletions

View File

@ -36,7 +36,6 @@ func (*route) Dial(ctx context.Context, network, address string, opts ...DialOpt
}
netd := dialer.NetDialer{
Timeout: options.Timeout,
Interface: options.Interface,
Netns: options.Netns,
Logger: options.Logger,
@ -94,7 +93,6 @@ func (r *route) Nodes() []*Node {
}
type DialOptions struct {
Timeout time.Duration
Interface string
Netns string
SockOpts *SockOpts
@ -103,12 +101,6 @@ type DialOptions struct {
type DialOption func(opts *DialOptions)
func TimeoutDialOption(d time.Duration) DialOption {
return func(opts *DialOptions) {
opts.Timeout = d
}
}
func InterfaceDialOption(ifName string) DialOption {
return func(opts *DialOptions) {
opts.Interface = ifName

View File

@ -103,6 +103,10 @@ func NewRouter(opts ...RouterOption) *Router {
opt(&r.options)
}
}
if r.options.Timeout == 0 {
r.options.Timeout = 15 * time.Second
}
if r.options.Logger == nil {
r.options.Logger = logger.Default().WithFields(map[string]any{"kind": "router"})
}
@ -117,6 +121,12 @@ func (r *Router) Options() *RouterOptions {
}
func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Conn, err error) {
if r.options.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.options.Timeout)
defer cancel()
}
host := address
if h, _, _ := net.SplitHostPort(address); h != "" {
host = h
@ -191,7 +201,6 @@ func (r *Router) dial(ctx context.Context, network, address string) (conn net.Co
NetnsDialOption(r.options.Netns),
SockOptsDialOption(r.options.SockOpts),
LoggerDialOption(r.options.Logger),
TimeoutDialOption(r.options.Timeout),
)
if err == nil {
break
@ -203,6 +212,12 @@ func (r *Router) dial(ctx context.Context, network, address string) (conn net.Co
}
func (r *Router) Bind(ctx context.Context, network, address string, opts ...BindOption) (ln net.Listener, err error) {
if r.options.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.options.Timeout)
defer cancel()
}
count := r.options.Retries + 1
if count <= 0 {
count = 1

View File

@ -3,7 +3,6 @@ package chain
import (
"context"
"net"
"time"
net_dialer "github.com/go-gost/core/common/net/dialer"
"github.com/go-gost/core/connector"
@ -16,7 +15,6 @@ type TransportOptions struct {
Netns string
SockOpts *SockOpts
Route Route
Timeout time.Duration
}
type TransportOption func(*TransportOptions)
@ -51,12 +49,6 @@ func RouteTransportOption(route Route) TransportOption {
}
}
func TimeoutTransportOption(timeout time.Duration) TransportOption {
return func(o *TransportOptions) {
o.Timeout = timeout
}
}
type Transport struct {
dialer dialer.Dialer
connector connector.Connector
@ -81,7 +73,6 @@ func (tr *Transport) Dial(ctx context.Context, addr string) (net.Conn, error) {
netd := &net_dialer.NetDialer{
Interface: tr.options.IfceName,
Netns: tr.options.Netns,
Timeout: tr.options.Timeout,
}
if tr.options.SockOpts != nil {
netd.Mark = tr.options.SockOpts.Mark
@ -117,7 +108,6 @@ func (tr *Transport) Connect(ctx context.Context, conn net.Conn, network, addres
netd := &net_dialer.NetDialer{
Interface: tr.options.IfceName,
Netns: tr.options.Netns,
Timeout: tr.options.Timeout,
}
if tr.options.SockOpts != nil {
netd.Mark = tr.options.SockOpts.Mark