Skip to content

Commit

Permalink
Merge branch 'develop' into chore/freeze_rpcinfo_comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ppzqh authored May 17, 2024
2 parents 3a6c343 + 3e9ff58 commit 15d06fe
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
40 changes: 36 additions & 4 deletions pkg/http/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,48 @@ import (
"strconv"
)

const (
tcp = "tcp"
tcp4 = "tcp4"
tcp6 = "tcp6"
)

// Resolver resolves url to address.
type Resolver interface {
Resolve(string) (string, error)
}

type defaultResolver struct{}
type ResolverOption func(cfg *resolverConfig)

// WithIPv4 configures the resolver to resolve ipv4 address only.
func WithIPv4() ResolverOption {
return func(cfg *resolverConfig) {
cfg.network = tcp4
}
}

// WithIPv6 configures the resolver to resolve ipv6 address only.
func WithIPv6() ResolverOption {
return func(cfg *resolverConfig) {
cfg.network = tcp6
}
}

type resolverConfig struct {
network string
}

type defaultResolver struct {
*resolverConfig
}

// NewDefaultResolver creates a default resolver.
func NewDefaultResolver() Resolver {
return &defaultResolver{}
func NewDefaultResolver(options ...ResolverOption) Resolver {
cfg := &resolverConfig{tcp}
for _, option := range options {
option(cfg)
}
return &defaultResolver{cfg}
}

// Resolve implements the Resolver interface.
Expand All @@ -49,7 +81,7 @@ func (p *defaultResolver) Resolve(URL string) (string, error) {
port = "80"
}
}
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, port))
addr, err := net.ResolveTCPAddr(p.network, net.JoinHostPort(host, port))
if err != nil {
return "", err
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/http/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package http

import (
"net"
"testing"

"github.com/cloudwego/kitex/internal/test"
Expand All @@ -34,3 +35,57 @@ func TestResolve(t *testing.T) {
_, err = r.Resolve(irresolvableURL)
test.Assert(t, err != nil)
}

func TestResolverWithIPPolicy(t *testing.T) {
// resolve ipv6 only
v6Resolver := NewDefaultResolver(WithIPv6())
test.Assert(t, v6Resolver.(*defaultResolver).network == tcp6)
resolvableURL := "http://www.google.com"
ipPort, err := v6Resolver.Resolve(resolvableURL)
test.Assert(t, err == nil)
ip, err := parseIPPort(ipPort)
test.Assert(t, err == nil)
test.Assert(t, isV6(ip))

invalidURL := "http://www.)(*&^%$#@!adomainshouldnotexists.com"
_, err = v6Resolver.Resolve(invalidURL)
test.Assert(t, err != nil)
irresolvableURL := "http://www.adomainshouldnotexists.com"
_, err = v6Resolver.Resolve(irresolvableURL)
test.Assert(t, err != nil)

// resolve ipv4 only
v4Resolver := NewDefaultResolver(WithIPv4())
test.Assert(t, v4Resolver.(*defaultResolver).network == tcp4)
ipPort, err = v4Resolver.Resolve(resolvableURL)
test.Assert(t, err == nil)
ip, err = parseIPPort(ipPort)
test.Assert(t, err == nil)
test.Assert(t, isV4(ip))

// dual
dualResolver := NewDefaultResolver()
test.Assert(t, dualResolver.(*defaultResolver).network == tcp)
ipPort, err = dualResolver.Resolve(resolvableURL)
test.Assert(t, err == nil)
ip, err = parseIPPort(ipPort)
test.Assert(t, err == nil)
test.Assert(t, ip != nil)
}

func parseIPPort(ipPort string) (net.IP, error) {
host, _, err := net.SplitHostPort(ipPort)
if err != nil {
return nil, err
}

return net.ParseIP(host), nil
}

func isV6(ip net.IP) bool {
return ip.To4() == nil && ip.To16() != nil
}

func isV4(ip net.IP) bool {
return ip.To4() != nil
}
4 changes: 2 additions & 2 deletions pkg/remote/trans/nphttp2/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ func (t *svrTransHandler) OnInactive(ctx context.Context, conn net.Conn) {
func (t *svrTransHandler) OnError(ctx context.Context, err error, conn net.Conn) {
var de *kerrors.DetailedError
if ok := errors.As(err, &de); ok && de.Stack() != "" {
klog.Errorf("KITEX: processing gRPC request error, remoteAddr=%s, error=%s\nstack=%s", conn.RemoteAddr(), err.Error(), de.Stack())
klog.CtxErrorf(ctx, "KITEX: processing gRPC request error, remoteAddr=%s, error=%s\nstack=%s", conn.RemoteAddr(), err.Error(), de.Stack())
} else {
klog.Errorf("KITEX: processing gRPC request error, remoteAddr=%s, error=%s", conn.RemoteAddr(), err.Error())
klog.CtxErrorf(ctx, "KITEX: processing gRPC request error, remoteAddr=%s, error=%s", conn.RemoteAddr(), err.Error())
}
}

Expand Down

0 comments on commit 15d06fe

Please sign in to comment.