Skip to content

Commit

Permalink
fix: add option to disable dns resolve
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <[email protected]>
  • Loading branch information
hlts2 committed Sep 19, 2024
1 parent bcebbdc commit 41f84ae
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
1 change: 1 addition & 0 deletions apis/grpc/v1/payload/payload.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apis/grpc/v1/rpc/errdetails/error_details.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 34 additions & 20 deletions internal/net/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,32 @@ type Client interface {
GetDialOption() []DialOption
GetCallOption() []CallOption
GetBackoff() backoff.Backoff
SetDisableResolveDNSAddr(addr string, disabled bool)
ConnectedAddrs() []string
Close(ctx context.Context) error
}

type gRPCClient struct {
addrs map[string]struct{}
poolSize uint64
clientCount uint64
conns sync.Map[string, pool.Conn]
hcDur time.Duration
prDur time.Duration
dialer net.Dialer
enablePoolRebalance bool
resolveDNS bool
dopts []DialOption
copts []CallOption
roccd string // reconnection old connection closing duration
eg errgroup.Group
bo backoff.Backoff
cb circuitbreaker.CircuitBreaker
gbo gbackoff.Config // grpc's original backoff configuration
mcd time.Duration // minimum connection timeout duration
group singleflight.Group[pool.Conn]
crl sync.Map[string, bool] // connection request list
addrs map[string]struct{}
poolSize uint64
clientCount uint64
conns sync.Map[string, pool.Conn]
hcDur time.Duration
prDur time.Duration
dialer net.Dialer
enablePoolRebalance bool
disableResolveDNSAddrs sync.Map[string, bool]
resolveDNS bool
dopts []DialOption
copts []CallOption
roccd string // reconnection old connection closing duration
eg errgroup.Group
bo backoff.Backoff
cb circuitbreaker.CircuitBreaker
gbo gbackoff.Config // grpc's original backoff configuration
mcd time.Duration // minimum connection timeout duration
group singleflight.Group[pool.Conn]
crl sync.Map[string, bool] // connection request list

ech <-chan error
monitorRunning atomic.Bool
Expand Down Expand Up @@ -946,6 +948,12 @@ func (g *gRPCClient) GetBackoff() backoff.Backoff {
return g.bo
}

func (g *gRPCClient) SetDisableResolveDNSAddr(addr string, disabled bool) {

Check warning on line 951 in internal/net/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

internal/net/grpc/client.go#L951

Added line #L951 was not covered by tests
// NOTE: When connecting to multiple locations, it was necessary to switch dynamically, so implementation was added.
// There is no setting for disable on the helm chart side, so I used this implementation.
g.disableResolveDNSAddrs.Store(addr, disabled)

Check warning on line 954 in internal/net/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

internal/net/grpc/client.go#L954

Added line #L954 was not covered by tests
}

func (g *gRPCClient) Connect(
ctx context.Context, addr string, dopts ...DialOption,
) (conn pool.Conn, err error) {
Expand Down Expand Up @@ -975,7 +983,13 @@ func (g *gRPCClient) Connect(
pool.WithAddr(addr),
pool.WithSize(g.poolSize),
pool.WithDialOptions(append(g.dopts, dopts...)...),
pool.WithResolveDNS(g.resolveDNS),
pool.WithResolveDNS(func() bool {
disabled, ok := g.disableResolveDNSAddrs.Load(addr)
if ok && disabled {
return false

Check warning on line 989 in internal/net/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

internal/net/grpc/client.go#L986-L989

Added lines #L986 - L989 were not covered by tests
}
return g.resolveDNS

Check warning on line 991 in internal/net/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

internal/net/grpc/client.go#L991

Added line #L991 was not covered by tests
}()),
}
if g.bo != nil {
opts = append(opts, pool.WithBackoff(g.bo))
Expand Down
13 changes: 12 additions & 1 deletion pkg/gateway/mirror/service/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package service
import (
"context"
"reflect"
"strings"

"github.com/vdaas/vald/internal/client/v1/client/mirror"
"github.com/vdaas/vald/internal/errors"
Expand Down Expand Up @@ -143,11 +144,21 @@ func (g *gateway) Do(
if target == "" {
return nil, errors.ErrTargetNotFound
}
return g.client.GRPCClient().Do(g.ForwardedContext(ctx, g.podName), target,
fctx := g.ForwardedContext(ctx, g.podName)
res, err = g.client.GRPCClient().Do(fctx, target,
func(ictx context.Context, conn *grpc.ClientConn, copts ...grpc.CallOption) (any, error) {
return f(ictx, target, NewMirrorClient(conn), copts...)
},
)
if err != nil {
return g.client.GRPCClient().RoundRobin(fctx, func(ictx context.Context, conn *grpc.ClientConn, copts ...grpc.CallOption) (any, error) {
if strings.EqualFold(conn.Target(), target) {
return nil, errors.ErrTargetNotFound
}
return f(ictx, conn.Target(), NewMirrorClient(conn), copts...)
})
}
return res, nil
}

// DoMulti performs a gRPC operation on multiple targets using the provided function.
Expand Down
1 change: 1 addition & 0 deletions pkg/gateway/mirror/service/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func (m *mirr) Connect(ctx context.Context, targets ...*payload.Mirror_Target) e
if !m.isSelfMirrorAddr(addr) && !m.isGatewayAddr(addr) {
_, ok := m.addrs.Load(addr)
if !ok || !m.IsConnected(ctx, addr) {
m.gateway.GRPCClient().SetDisableResolveDNSAddr(addr, true)
_, err := m.gateway.GRPCClient().Connect(ctx, addr)
if err != nil {
m.addrs.Delete(addr)
Expand Down

0 comments on commit 41f84ae

Please sign in to comment.