client: add a WithNoProxy dialoption (#3411)

This commit is contained in:
Patrick Bogen 2020-03-26 15:29:40 -07:00 коммит произвёл GitHub
Родитель 5dbd4c232c
Коммит e965f2a60b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 19 добавлений и 6 удалений

Просмотреть файл

@ -194,12 +194,13 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
cc.mkp = cc.dopts.copts.KeepaliveParams cc.mkp = cc.dopts.copts.KeepaliveParams
if cc.dopts.copts.Dialer == nil { if cc.dopts.copts.Dialer == nil {
cc.dopts.copts.Dialer = newProxyDialer( cc.dopts.copts.Dialer = func(ctx context.Context, addr string) (net.Conn, error) {
func(ctx context.Context, addr string) (net.Conn, error) {
network, addr := parseDialTarget(addr) network, addr := parseDialTarget(addr)
return (&net.Dialer{}).DialContext(ctx, network, addr) return (&net.Dialer{}).DialContext(ctx, network, addr)
}, }
) if cc.dopts.withProxy {
cc.dopts.copts.Dialer = newProxyDialer(cc.dopts.copts.Dialer)
}
} }
if cc.dopts.copts.UserAgent != "" { if cc.dopts.copts.UserAgent != "" {

Просмотреть файл

@ -72,6 +72,7 @@ type dialOptions struct {
// we need to be able to configure this in tests. // we need to be able to configure this in tests.
resolveNowBackoff func(int) time.Duration resolveNowBackoff func(int) time.Duration
resolvers []resolver.Builder resolvers []resolver.Builder
withProxy bool
} }
// DialOption configures how we set up the connection. // DialOption configures how we set up the connection.
@ -307,6 +308,16 @@ func WithInsecure() DialOption {
}) })
} }
// WithNoProxy returns a DialOption which disables the use of proxies for this
// ClientConn. This is ignored if WithDialer or WithContextDialer are used.
//
// This API is EXPERIMENTAL.
func WithNoProxy() DialOption {
return newFuncDialOption(func(o *dialOptions) {
o.withProxy = false
})
}
// WithTransportCredentials returns a DialOption which configures a connection // WithTransportCredentials returns a DialOption which configures a connection
// level security credentials (e.g., TLS/SSL). This should not be used together // level security credentials (e.g., TLS/SSL). This should not be used together
// with WithCredentialsBundle. // with WithCredentialsBundle.
@ -557,6 +568,7 @@ func defaultDialOptions() dialOptions {
ReadBufferSize: defaultReadBufSize, ReadBufferSize: defaultReadBufSize,
}, },
resolveNowBackoff: internalbackoff.DefaultExponential.Backoff, resolveNowBackoff: internalbackoff.DefaultExponential.Backoff,
withProxy: true,
} }
} }