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

feat: add a DialTimeout function #92

Merged
merged 1 commit into from
Jun 2, 2022
Merged
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
11 changes: 10 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"net"
"time"
)

// Available returns whether or not SO_REUSEPORT or equivalent behaviour is
Expand All @@ -47,17 +48,25 @@ func ListenPacket(network, address string) (net.PacketConn, error) {
return listenConfig.ListenPacket(context.Background(), network, address)
}

// Dial dials the given network and address. see net.Dialer.Dial
// Dial dials the given network and address. see net.Dial
// Returns a net.Conn created from a file descriptor for a socket
// with SO_REUSEPORT and SO_REUSEADDR option set.
func Dial(network, laddr, raddr string) (net.Conn, error) {
return DialTimeout(network, laddr, raddr, time.Duration(0))
}

// Dial dials the given network and address, with the given timeout. see
// net.DialTimeout Returns a net.Conn created from a file descriptor for
// a socket with SO_REUSEPORT and SO_REUSEADDR option set.
func DialTimeout(network, laddr, raddr string, timeout time.Duration) (net.Conn, error) {
nla, err := ResolveAddr(network, laddr)
if err != nil {
return nil, fmt.Errorf("failed to resolve local addr: %w", err)
}
d := net.Dialer{
Control: Control,
LocalAddr: nla,
Timeout: timeout,
}
return d.Dial(network, raddr)
}