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: cached addr #345

Closed
wants to merge 3 commits 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
60 changes: 33 additions & 27 deletions net_sock.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,29 @@ func internetSocket(ctx context.Context, net string, laddr, raddr sockaddr, soty
// address family, both AF_INET and AF_INET6, and a wildcard address
// like the following:
//
// - A listen for a wildcard communication domain, "tcp" or
// "udp", with a wildcard address: If the platform supports
// both IPv6 and IPv4-mapped IPv6 communication capabilities,
// or does not support IPv4, we use a dual stack, AF_INET6 and
// IPV6_V6ONLY=0, wildcard address listen. The dual stack
// wildcard address listen may fall back to an IPv6-only,
// AF_INET6 and IPV6_V6ONLY=1, wildcard address listen.
// Otherwise we prefer an IPv4-only, AF_INET, wildcard address
// listen.
// - A listen for a wildcard communication domain, "tcp" or
// "udp", with a wildcard address: If the platform supports
// both IPv6 and IPv4-mapped IPv6 communication capabilities,
// or does not support IPv4, we use a dual stack, AF_INET6 and
// IPV6_V6ONLY=0, wildcard address listen. The dual stack
// wildcard address listen may fall back to an IPv6-only,
// AF_INET6 and IPV6_V6ONLY=1, wildcard address listen.
// Otherwise we prefer an IPv4-only, AF_INET, wildcard address
// listen.
//
// - A listen for a wildcard communication domain, "tcp" or
// "udp", with an IPv4 wildcard address: same as above.
// - A listen for a wildcard communication domain, "tcp" or
// "udp", with an IPv4 wildcard address: same as above.
//
// - A listen for a wildcard communication domain, "tcp" or
// "udp", with an IPv6 wildcard address: same as above.
// - A listen for a wildcard communication domain, "tcp" or
// "udp", with an IPv6 wildcard address: same as above.
//
// - A listen for an IPv4 communication domain, "tcp4" or "udp4",
// with an IPv4 wildcard address: We use an IPv4-only, AF_INET,
// wildcard address listen.
// - A listen for an IPv4 communication domain, "tcp4" or "udp4",
// with an IPv4 wildcard address: We use an IPv4-only, AF_INET,
// wildcard address listen.
//
// - A listen for an IPv6 communication domain, "tcp6" or "udp6",
// with an IPv6 wildcard address: We use an IPv6-only, AF_INET6
// and IPV6_V6ONLY=1, wildcard address listen.
// - A listen for an IPv6 communication domain, "tcp6" or "udp6",
// with an IPv6 wildcard address: We use an IPv6-only, AF_INET6
// and IPV6_V6ONLY=1, wildcard address listen.
//
// Otherwise guess: If the addresses are IPv4 then returns AF_INET,
// or else returns AF_INET6. It also returns a boolean value what
Expand Down Expand Up @@ -129,9 +129,11 @@ func sockaddrToAddr(sa syscall.Sockaddr) net.Addr {
var a net.Addr
switch sa := sa.(type) {
case *syscall.SockaddrInet4:
a = &net.TCPAddr{
IP: sa.Addr[0:],
Port: sa.Port,
a = &TCPAddr{
TCPAddr: net.TCPAddr{
IP: sa.Addr[0:],
Port: sa.Port,
},
}
case *syscall.SockaddrInet6:
var zone string
Expand All @@ -142,13 +144,17 @@ func sockaddrToAddr(sa syscall.Sockaddr) net.Addr {
}
// if zone == "" && sa.ZoneId != 0 {
// }
a = &net.TCPAddr{
IP: sa.Addr[0:],
Port: sa.Port,
Zone: zone,
a = &TCPAddr{
TCPAddr: net.TCPAddr{
IP: sa.Addr[0:],
Port: sa.Port,
Zone: zone,
},
}
case *syscall.SockaddrUnix:
a = &net.UnixAddr{Net: "unix", Name: sa.Name}
a = &UnixAddr{
UnixAddr: net.UnixAddr{Net: "unix", Name: sa.Name},
}
}
return a
}
14 changes: 11 additions & 3 deletions net_tcpsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ import (
// TCPAddr represents the address of a TCP end point.
type TCPAddr struct {
net.TCPAddr
cachedAddr string
}

func (a *TCPAddr) String() string {
if a.cachedAddr == "" {
a.cachedAddr = a.TCPAddr.String()
}
return a.cachedAddr
}

func (a *TCPAddr) isWildcard() bool {
Expand Down Expand Up @@ -129,7 +137,7 @@ func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
if err != nil {
return nil, err
}
return &TCPAddr{*addr}, nil
return &TCPAddr{TCPAddr: *addr}, nil
}

// TCPConnection implements Connection.
Expand Down Expand Up @@ -231,8 +239,8 @@ func selfConnect(conn *netFD, err error) bool {
if conn.localAddr == nil || conn.remoteAddr == nil {
return true
}
l := conn.localAddr.(*net.TCPAddr)
r := conn.remoteAddr.(*net.TCPAddr)
l := conn.localAddr.(*TCPAddr).TCPAddr
r := conn.remoteAddr.(*TCPAddr).TCPAddr
return l.Port == r.Port && l.IP.Equal(r.IP)
}

Expand Down
10 changes: 9 additions & 1 deletion net_unixsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ import (
// UnixAddr represents the address of a Unix domain socket end point.
type UnixAddr struct {
net.UnixAddr
cachedAddr string
}

func (a *UnixAddr) String() string {
if a.cachedAddr == "" {
a.cachedAddr = a.UnixAddr.String()
}
return a.cachedAddr
}

func (a *UnixAddr) isWildcard() bool {
Expand Down Expand Up @@ -65,7 +73,7 @@ func ResolveUnixAddr(network, address string) (*UnixAddr, error) {
if err != nil {
return nil, err
}
return &UnixAddr{*addr}, nil
return &UnixAddr{UnixAddr: *addr}, nil
}

// UnixConnection implements Connection.
Expand Down
Loading