Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support timeout when Dial #665

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"strconv"
"syscall"
"time"

"golang.org/x/sync/errgroup"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -148,6 +149,11 @@ func (cli *Client) Dial(network, address string) (Conn, error) {
return cli.DialContext(network, address, nil)
}

// DialTimeout is like net.DialTimeout().
func (cli *Client) DialTimeout(network, address string, timeout time.Duration) (Conn, error) {
return cli.DialContextTimeout(network, address, nil, timeout)
}

// DialContext is like Dial but also accepts an empty interface ctx that can be obtained later via Conn.Context.
func (cli *Client) DialContext(network, address string, ctx any) (Conn, error) {
c, err := net.Dial(network, address)
Expand All @@ -157,6 +163,15 @@ func (cli *Client) DialContext(network, address string, ctx any) (Conn, error) {
return cli.EnrollContext(c, ctx)
}

// DialContextTimeout is like DialContext but also accepts a timeout.
func (cli *Client) DialContextTimeout(network, address string, ctx any, timeout time.Duration) (Conn, error) {
c, err := net.DialTimeout(network, address, timeout)
if err != nil {
return nil, err
}
return cli.EnrollContext(c, ctx)
}

// Enroll converts a net.Conn to gnet.Conn and then adds it into Client.
func (cli *Client) Enroll(c net.Conn) (Conn, error) {
return cli.EnrollContext(c, nil)
Expand Down
26 changes: 26 additions & 0 deletions client_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"sync"
"time"

"golang.org/x/sync/errgroup"

Expand Down Expand Up @@ -118,6 +119,10 @@ func (cli *Client) Dial(network, addr string) (Conn, error) {
return cli.DialContext(network, addr, nil)
}

func (cli *Client) DialTimeout(network, addr string, timeout time.Duration) (Conn, error) {
return cli.DialContextTimeout(network, addr, nil, timeout)
}

func (cli *Client) DialContext(network, addr string, ctx any) (Conn, error) {
var (
c net.Conn
Expand All @@ -139,6 +144,27 @@ func (cli *Client) DialContext(network, addr string, ctx any) (Conn, error) {
return cli.EnrollContext(c, ctx)
}

func (cli *Client) DialContextTimeout(network, addr string, ctx any, timeout time.Duration) (Conn, error) {
var (
c net.Conn
err error
)
if network == "unix" {
laddr, _ := net.ResolveUnixAddr(network, unixAddr(addr))
raddr, _ := net.ResolveUnixAddr(network, addr)
c, err = net.DialUnix(network, laddr, raddr)
if err != nil {
return nil, err
}
} else {
c, err = net.DialTimeout(network, addr, timeout)
if err != nil {
return nil, err
}
}
return cli.EnrollContext(c, ctx)
}

func (cli *Client) Enroll(nc net.Conn) (gc Conn, err error) {
return cli.EnrollContext(nc, nil)
}
Expand Down