From 59a2cfbdf9272f6334fe4856b6e7c8d212845b3d Mon Sep 17 00:00:00 2001 From: Doug Fawley Date: Thu, 1 Nov 2018 15:43:42 -0700 Subject: [PATCH] Remove support for Go1.6-1.8 (#2428) --- README.md | 2 +- benchmark/benchmark16_test.go | 112 --------------- ...{benchmark17_test.go => benchmark_test.go} | 2 - benchmark/primitives/primitives_test.go | 2 - channelz/service/func_linux.go | 2 +- channelz/service/func_nonlinux.go | 2 +- channelz/service/service_sktopt_test.go | 2 +- channelz/service/service_test.go | 1 + channelz/service/util_sktopt_386_test.go | 2 +- channelz/service/util_sktopt_amd64_test.go | 2 +- clientconn.go | 4 +- credentials/credentials.go | 20 +++ credentials/go16.go | 57 -------- credentials/go17.go | 59 -------- credentials/go18.go | 46 ------ credentials/{go19.go => syscall.go} | 2 +- encoding/proto/proto_benchmark_test.go | 2 - go16.go | 71 --------- go17.go | 72 ---------- internal/binarylog/binarylog_end2end_test.go | 2 - internal/channelz/types_linux.go | 2 +- internal/channelz/types_nonlinux.go | 2 +- .../{util_linux_go19.go => util_linux.go} | 2 +- ..._nonlinux_pre_go19.go => util_nonlinux.go} | 2 +- internal/syscall/syscall_linux.go | 2 +- internal/syscall/syscall_nonlinux.go | 2 +- internal/transport/go16.go | 52 ------- internal/transport/go17.go | 53 ------- internal/transport/handler_server.go | 2 +- internal/transport/http2_client.go | 2 +- internal/transport/transport.go | 13 +- naming/dns_resolver.go | 3 + naming/dns_resolver_test.go | 16 +++ naming/go17.go | 34 ----- naming/go17_test.go | 42 ------ naming/go18.go | 28 ---- naming/go18_test.go | 41 ------ proxy.go | 11 +- resolver/dns/dns_resolver.go | 32 ++++- resolver/dns/dns_resolver_go19_test.go | 135 ------------------ resolver/dns/dns_resolver_test.go | 106 +++++++++++++- resolver/dns/go18_test.go | 27 ---- resolver/dns/go19.go | 54 ------- resolver/dns/pre_go18_test.go | 27 ---- resolver/dns/pre_go19.go | 51 ------- rpc_util.go | 27 +++- stats/stats_test.go | 2 - status/go16.go | 42 ------ status/go17.go | 44 ------ status/go17_test.go | 44 ------ status/status.go | 17 +++ status/status_test.go | 2 +- 52 files changed, 256 insertions(+), 1127 deletions(-) delete mode 100644 benchmark/benchmark16_test.go rename benchmark/{benchmark17_test.go => benchmark_test.go} (99%) delete mode 100644 credentials/go16.go delete mode 100644 credentials/go17.go delete mode 100644 credentials/go18.go rename credentials/{go19.go => syscall.go} (97%) delete mode 100644 go16.go delete mode 100644 go17.go rename internal/channelz/{util_linux_go19.go => util_linux.go} (96%) rename internal/channelz/{util_nonlinux_pre_go19.go => util_nonlinux.go} (95%) delete mode 100644 internal/transport/go16.go delete mode 100644 internal/transport/go17.go delete mode 100644 naming/go17.go delete mode 100644 naming/go17_test.go delete mode 100644 naming/go18.go delete mode 100644 naming/go18_test.go delete mode 100644 resolver/dns/dns_resolver_go19_test.go delete mode 100644 resolver/dns/go18_test.go delete mode 100644 resolver/dns/go19.go delete mode 100644 resolver/dns/pre_go18_test.go delete mode 100644 resolver/dns/pre_go19.go delete mode 100644 status/go16.go delete mode 100644 status/go17.go delete mode 100644 status/go17_test.go diff --git a/README.md b/README.md index 29ffb00d3aa8..e3fb3c75ae3c 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ $ go get -u google.golang.org/grpc Prerequisites ------------- -This requires Go 1.6 or later. Go 1.7 will be required soon. +gRPC-Go requires Go 1.9 or later. Constraints ----------- diff --git a/benchmark/benchmark16_test.go b/benchmark/benchmark16_test.go deleted file mode 100644 index a036b63cbafb..000000000000 --- a/benchmark/benchmark16_test.go +++ /dev/null @@ -1,112 +0,0 @@ -// +build go1.6,!go1.7 - -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package benchmark - -import ( - "os" - "testing" - - "google.golang.org/grpc" - "google.golang.org/grpc/benchmark/stats" -) - -func BenchmarkClientStreamc1(b *testing.B) { - grpc.EnableTracing = true - runStream(b, stats.Features{"", true, 0, 0, 0, 1, 1, 1, false, false}) -} - -func BenchmarkClientStreamc8(b *testing.B) { - grpc.EnableTracing = true - runStream(b, stats.Features{"", true, 0, 0, 0, 8, 1, 1, false, false}) -} - -func BenchmarkClientStreamc64(b *testing.B) { - grpc.EnableTracing = true - runStream(b, stats.Features{"", true, 0, 0, 0, 64, 1, 1, false, false}) -} - -func BenchmarkClientStreamc512(b *testing.B) { - grpc.EnableTracing = true - runStream(b, stats.Features{"", true, 0, 0, 0, 512, 1, 1, false, false}) -} -func BenchmarkClientUnaryc1(b *testing.B) { - grpc.EnableTracing = true - runStream(b, stats.Features{"", true, 0, 0, 0, 1, 1, 1, false, false}) -} - -func BenchmarkClientUnaryc8(b *testing.B) { - grpc.EnableTracing = true - runStream(b, stats.Features{"", true, 0, 0, 0, 8, 1, 1, false, false}) -} - -func BenchmarkClientUnaryc64(b *testing.B) { - grpc.EnableTracing = true - runStream(b, stats.Features{"", true, 0, 0, 0, 64, 1, 1, false, false}) -} - -func BenchmarkClientUnaryc512(b *testing.B) { - grpc.EnableTracing = true - runStream(b, stats.Features{"", true, 0, 0, 0, 512, 1, 1, false, false}) -} - -func BenchmarkClientStreamNoTracec1(b *testing.B) { - grpc.EnableTracing = false - runStream(b, stats.Features{"", false, 0, 0, 0, 1, 1, 1, false, false}) -} - -func BenchmarkClientStreamNoTracec8(b *testing.B) { - grpc.EnableTracing = false - runStream(b, stats.Features{"", false, 0, 0, 0, 8, 1, 1, false, false}) -} - -func BenchmarkClientStreamNoTracec64(b *testing.B) { - grpc.EnableTracing = false - runStream(b, stats.Features{"", false, 0, 0, 0, 64, 1, 1, false, false}) -} - -func BenchmarkClientStreamNoTracec512(b *testing.B) { - grpc.EnableTracing = false - runStream(b, stats.Features{"", false, 0, 0, 0, 512, 1, 1, false, false}) -} -func BenchmarkClientUnaryNoTracec1(b *testing.B) { - grpc.EnableTracing = false - runStream(b, stats.Features{"", false, 0, 0, 0, 1, 1, 1, false, false}) -} - -func BenchmarkClientUnaryNoTracec8(b *testing.B) { - grpc.EnableTracing = false - runStream(b, stats.Features{"", false, 0, 0, 0, 8, 1, 1, false, false}) -} - -func BenchmarkClientUnaryNoTracec64(b *testing.B) { - grpc.EnableTracing = false - runStream(b, stats.Features{"", false, 0, 0, 0, 64, 1, 1, false, false}) -} - -func BenchmarkClientUnaryNoTracec512(b *testing.B) { - grpc.EnableTracing = false - runStream(b, stats.Features{"", false, 0, 0, 0, 512, 1, 1, false, false}) - runStream(b, stats.Features{"", false, 0, 0, 0, 512, 1, 1, false, false}) -} - -func TestMain(m *testing.M) { - os.Exit(stats.RunTestMain(m)) -} diff --git a/benchmark/benchmark17_test.go b/benchmark/benchmark_test.go similarity index 99% rename from benchmark/benchmark17_test.go rename to benchmark/benchmark_test.go index 8dc7d3c55889..dd4984a26f60 100644 --- a/benchmark/benchmark17_test.go +++ b/benchmark/benchmark_test.go @@ -1,5 +1,3 @@ -// +build go1.7 - /* * * Copyright 2017 gRPC authors. diff --git a/benchmark/primitives/primitives_test.go b/benchmark/primitives/primitives_test.go index 846813d8d174..71fc26e29418 100644 --- a/benchmark/primitives/primitives_test.go +++ b/benchmark/primitives/primitives_test.go @@ -1,5 +1,3 @@ -// +build go1.7 - /* * * Copyright 2017 gRPC authors. diff --git a/channelz/service/func_linux.go b/channelz/service/func_linux.go index fc5bbf361236..192c3a1fa5a3 100644 --- a/channelz/service/func_linux.go +++ b/channelz/service/func_linux.go @@ -1,4 +1,4 @@ -// +build !appengine,go1.7 +// +build !appengine /* * diff --git a/channelz/service/func_nonlinux.go b/channelz/service/func_nonlinux.go index 44a9f195106c..eb53334ed0d1 100644 --- a/channelz/service/func_nonlinux.go +++ b/channelz/service/func_nonlinux.go @@ -1,4 +1,4 @@ -// +build !linux appengine !go1.7 +// +build !linux appengine /* * diff --git a/channelz/service/service_sktopt_test.go b/channelz/service/service_sktopt_test.go index e7e1c7eac672..894f57d4b175 100644 --- a/channelz/service/service_sktopt_test.go +++ b/channelz/service/service_sktopt_test.go @@ -1,4 +1,4 @@ -// +build linux,!appengine,go1.7 +// +build linux,!appengine // +build 386 amd64 /* diff --git a/channelz/service/service_test.go b/channelz/service/service_test.go index d455ca90578b..4ec1133cc5e8 100644 --- a/channelz/service/service_test.go +++ b/channelz/service/service_test.go @@ -51,6 +51,7 @@ var protoToSocketOpt protoToSocketOptFunc // with &utcLoc. However zero value of a time.Time type value loc field is nil. // This behavior will make reflect.DeepEqual fail upon unset time.Time field, // and cause false positive fatal error. +// TODO: Go1.7 is no longer supported - does this need a change? var emptyTime time.Time type dummyChannel struct { diff --git a/channelz/service/util_sktopt_386_test.go b/channelz/service/util_sktopt_386_test.go index e713f16d3320..cdc2fda4028d 100644 --- a/channelz/service/util_sktopt_386_test.go +++ b/channelz/service/util_sktopt_386_test.go @@ -1,4 +1,4 @@ -// +build 386,linux,!appengine,go1.7 +// +build 386,linux,!appengine /* * diff --git a/channelz/service/util_sktopt_amd64_test.go b/channelz/service/util_sktopt_amd64_test.go index 0c1db69ba90e..7ebe9c70eb68 100644 --- a/channelz/service/util_sktopt_amd64_test.go +++ b/channelz/service/util_sktopt_amd64_test.go @@ -1,4 +1,4 @@ -// +build amd64,linux,!appengine,go1.7 +// +build amd64,linux,!appengine /* * diff --git a/clientconn.go b/clientconn.go index 5d43cf465dff..fb4e04467592 100644 --- a/clientconn.go +++ b/clientconn.go @@ -19,6 +19,7 @@ package grpc import ( + "context" "errors" "fmt" "math" @@ -29,7 +30,6 @@ import ( "sync/atomic" "time" - "golang.org/x/net/context" "google.golang.org/grpc/balancer" _ "google.golang.org/grpc/balancer/roundrobin" // To register roundrobin. "google.golang.org/grpc/codes" @@ -183,7 +183,7 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * cc.dopts.copts.Dialer = newProxyDialer( func(ctx context.Context, addr string) (net.Conn, error) { network, addr := parseDialTarget(addr) - return dialContext(ctx, network, addr) + return (&net.Dialer{}).DialContext(ctx, network, addr) }, ) } diff --git a/credentials/credentials.go b/credentials/credentials.go index ae1bc3027e07..aff58bee1ab6 100644 --- a/credentials/credentials.go +++ b/credentials/credentials.go @@ -318,4 +318,24 @@ var cipherSuiteLookup = map[uint16]string{ tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", tls.TLS_FALLBACK_SCSV: "TLS_FALLBACK_SCSV", + tls.TLS_RSA_WITH_AES_128_CBC_SHA256: "TLS_RSA_WITH_AES_128_CBC_SHA256", + tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", + tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305", + tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305", +} + +// cloneTLSConfig returns a shallow clone of the exported +// fields of cfg, ignoring the unexported sync.Once, which +// contains a mutex and must not be copied. +// +// If cfg is nil, a new zero tls.Config is returned. +// +// TODO: inline this function if possible. +func cloneTLSConfig(cfg *tls.Config) *tls.Config { + if cfg == nil { + return &tls.Config{} + } + + return cfg.Clone() } diff --git a/credentials/go16.go b/credentials/go16.go deleted file mode 100644 index d6bbcc9fdd95..000000000000 --- a/credentials/go16.go +++ /dev/null @@ -1,57 +0,0 @@ -// +build !go1.7 - -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package credentials - -import ( - "crypto/tls" -) - -// cloneTLSConfig returns a shallow clone of the exported -// fields of cfg, ignoring the unexported sync.Once, which -// contains a mutex and must not be copied. -// -// If cfg is nil, a new zero tls.Config is returned. -func cloneTLSConfig(cfg *tls.Config) *tls.Config { - if cfg == nil { - return &tls.Config{} - } - return &tls.Config{ - Rand: cfg.Rand, - Time: cfg.Time, - Certificates: cfg.Certificates, - NameToCertificate: cfg.NameToCertificate, - GetCertificate: cfg.GetCertificate, - RootCAs: cfg.RootCAs, - NextProtos: cfg.NextProtos, - ServerName: cfg.ServerName, - ClientAuth: cfg.ClientAuth, - ClientCAs: cfg.ClientCAs, - InsecureSkipVerify: cfg.InsecureSkipVerify, - CipherSuites: cfg.CipherSuites, - PreferServerCipherSuites: cfg.PreferServerCipherSuites, - SessionTicketsDisabled: cfg.SessionTicketsDisabled, - SessionTicketKey: cfg.SessionTicketKey, - ClientSessionCache: cfg.ClientSessionCache, - MinVersion: cfg.MinVersion, - MaxVersion: cfg.MaxVersion, - CurvePreferences: cfg.CurvePreferences, - } -} diff --git a/credentials/go17.go b/credentials/go17.go deleted file mode 100644 index fbd500002008..000000000000 --- a/credentials/go17.go +++ /dev/null @@ -1,59 +0,0 @@ -// +build go1.7,!go1.8 - -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package credentials - -import ( - "crypto/tls" -) - -// cloneTLSConfig returns a shallow clone of the exported -// fields of cfg, ignoring the unexported sync.Once, which -// contains a mutex and must not be copied. -// -// If cfg is nil, a new zero tls.Config is returned. -func cloneTLSConfig(cfg *tls.Config) *tls.Config { - if cfg == nil { - return &tls.Config{} - } - return &tls.Config{ - Rand: cfg.Rand, - Time: cfg.Time, - Certificates: cfg.Certificates, - NameToCertificate: cfg.NameToCertificate, - GetCertificate: cfg.GetCertificate, - RootCAs: cfg.RootCAs, - NextProtos: cfg.NextProtos, - ServerName: cfg.ServerName, - ClientAuth: cfg.ClientAuth, - ClientCAs: cfg.ClientCAs, - InsecureSkipVerify: cfg.InsecureSkipVerify, - CipherSuites: cfg.CipherSuites, - PreferServerCipherSuites: cfg.PreferServerCipherSuites, - SessionTicketsDisabled: cfg.SessionTicketsDisabled, - SessionTicketKey: cfg.SessionTicketKey, - ClientSessionCache: cfg.ClientSessionCache, - MinVersion: cfg.MinVersion, - MaxVersion: cfg.MaxVersion, - CurvePreferences: cfg.CurvePreferences, - DynamicRecordSizingDisabled: cfg.DynamicRecordSizingDisabled, - Renegotiation: cfg.Renegotiation, - } -} diff --git a/credentials/go18.go b/credentials/go18.go deleted file mode 100644 index db30d46cc57d..000000000000 --- a/credentials/go18.go +++ /dev/null @@ -1,46 +0,0 @@ -// +build go1.8 - -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package credentials - -import ( - "crypto/tls" -) - -func init() { - cipherSuiteLookup[tls.TLS_RSA_WITH_AES_128_CBC_SHA256] = "TLS_RSA_WITH_AES_128_CBC_SHA256" - cipherSuiteLookup[tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256] = "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" - cipherSuiteLookup[tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256] = "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" - cipherSuiteLookup[tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305] = "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305" - cipherSuiteLookup[tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305] = "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305" -} - -// cloneTLSConfig returns a shallow clone of the exported -// fields of cfg, ignoring the unexported sync.Once, which -// contains a mutex and must not be copied. -// -// If cfg is nil, a new zero tls.Config is returned. -func cloneTLSConfig(cfg *tls.Config) *tls.Config { - if cfg == nil { - return &tls.Config{} - } - - return cfg.Clone() -} diff --git a/credentials/go19.go b/credentials/syscall.go similarity index 97% rename from credentials/go19.go rename to credentials/syscall.go index 2a4ca1a57d66..9102b22ba95b 100644 --- a/credentials/go19.go +++ b/credentials/syscall.go @@ -1,4 +1,4 @@ -// +build go1.9,!appengine +// +build !appengine /* * diff --git a/encoding/proto/proto_benchmark_test.go b/encoding/proto/proto_benchmark_test.go index 63ea57de2edb..7d4819859fde 100644 --- a/encoding/proto/proto_benchmark_test.go +++ b/encoding/proto/proto_benchmark_test.go @@ -1,5 +1,3 @@ -// +build go1.7 - /* * * Copyright 2014 gRPC authors. diff --git a/go16.go b/go16.go deleted file mode 100644 index b1db21af6f4d..000000000000 --- a/go16.go +++ /dev/null @@ -1,71 +0,0 @@ -// +build go1.6,!go1.7 - -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package grpc - -import ( - "fmt" - "io" - "net" - "net/http" - - "golang.org/x/net/context" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/internal/transport" - "google.golang.org/grpc/status" -) - -// dialContext connects to the address on the named network. -func dialContext(ctx context.Context, network, address string) (net.Conn, error) { - return (&net.Dialer{Cancel: ctx.Done()}).Dial(network, address) -} - -func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error { - req.Cancel = ctx.Done() - if err := req.Write(conn); err != nil { - return fmt.Errorf("failed to write the HTTP request: %v", err) - } - return nil -} - -// toRPCErr converts an error into an error from the status package. -func toRPCErr(err error) error { - if err == nil || err == io.EOF { - return err - } - if err == io.ErrUnexpectedEOF { - return status.Error(codes.Internal, err.Error()) - } - if _, ok := status.FromError(err); ok { - return err - } - switch e := err.(type) { - case transport.ConnectionError: - return status.Error(codes.Unavailable, e.Desc) - default: - switch err { - case context.DeadlineExceeded: - return status.Error(codes.DeadlineExceeded, err.Error()) - case context.Canceled: - return status.Error(codes.Canceled, err.Error()) - } - } - return status.Error(codes.Unknown, err.Error()) -} diff --git a/go17.go b/go17.go deleted file mode 100644 index 71a72e8fe15e..000000000000 --- a/go17.go +++ /dev/null @@ -1,72 +0,0 @@ -// +build go1.7 - -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package grpc - -import ( - "context" - "fmt" - "io" - "net" - "net/http" - - netctx "golang.org/x/net/context" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/internal/transport" - "google.golang.org/grpc/status" -) - -// dialContext connects to the address on the named network. -func dialContext(ctx context.Context, network, address string) (net.Conn, error) { - return (&net.Dialer{}).DialContext(ctx, network, address) -} - -func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error { - req = req.WithContext(ctx) - if err := req.Write(conn); err != nil { - return fmt.Errorf("failed to write the HTTP request: %v", err) - } - return nil -} - -// toRPCErr converts an error into an error from the status package. -func toRPCErr(err error) error { - if err == nil || err == io.EOF { - return err - } - if err == io.ErrUnexpectedEOF { - return status.Error(codes.Internal, err.Error()) - } - if _, ok := status.FromError(err); ok { - return err - } - switch e := err.(type) { - case transport.ConnectionError: - return status.Error(codes.Unavailable, e.Desc) - default: - switch err { - case context.DeadlineExceeded, netctx.DeadlineExceeded: - return status.Error(codes.DeadlineExceeded, err.Error()) - case context.Canceled, netctx.Canceled: - return status.Error(codes.Canceled, err.Error()) - } - } - return status.Error(codes.Unknown, err.Error()) -} diff --git a/internal/binarylog/binarylog_end2end_test.go b/internal/binarylog/binarylog_end2end_test.go index a5a5dec61559..51d009dce734 100644 --- a/internal/binarylog/binarylog_end2end_test.go +++ b/internal/binarylog/binarylog_end2end_test.go @@ -1,5 +1,3 @@ -// +build go1.8 - /* * * Copyright 2018 gRPC authors. diff --git a/internal/channelz/types_linux.go b/internal/channelz/types_linux.go index 07215396d64a..692dd6181778 100644 --- a/internal/channelz/types_linux.go +++ b/internal/channelz/types_linux.go @@ -1,4 +1,4 @@ -// +build !appengine,go1.7 +// +build !appengine /* * diff --git a/internal/channelz/types_nonlinux.go b/internal/channelz/types_nonlinux.go index b24600480c34..79edbefc4331 100644 --- a/internal/channelz/types_nonlinux.go +++ b/internal/channelz/types_nonlinux.go @@ -1,4 +1,4 @@ -// +build !linux appengine !go1.7 +// +build !linux appengine /* * diff --git a/internal/channelz/util_linux_go19.go b/internal/channelz/util_linux.go similarity index 96% rename from internal/channelz/util_linux_go19.go rename to internal/channelz/util_linux.go index e1e9e32d7397..fdf409d55de3 100644 --- a/internal/channelz/util_linux_go19.go +++ b/internal/channelz/util_linux.go @@ -1,4 +1,4 @@ -// +build linux,go1.9,!appengine +// +build linux,!appengine /* * diff --git a/internal/channelz/util_nonlinux_pre_go19.go b/internal/channelz/util_nonlinux.go similarity index 95% rename from internal/channelz/util_nonlinux_pre_go19.go rename to internal/channelz/util_nonlinux.go index 1d4da952d85e..8864a0811164 100644 --- a/internal/channelz/util_nonlinux_pre_go19.go +++ b/internal/channelz/util_nonlinux.go @@ -1,4 +1,4 @@ -// +build !linux !go1.9 appengine +// +build !linux appengine /* * diff --git a/internal/syscall/syscall_linux.go b/internal/syscall/syscall_linux.go index 87bc65a19674..1c7cef610396 100644 --- a/internal/syscall/syscall_linux.go +++ b/internal/syscall/syscall_linux.go @@ -1,4 +1,4 @@ -// +build !appengine,go1.7 +// +build !appengine /* * diff --git a/internal/syscall/syscall_nonlinux.go b/internal/syscall/syscall_nonlinux.go index 16a5c3fe45cb..887c38bbd04e 100644 --- a/internal/syscall/syscall_nonlinux.go +++ b/internal/syscall/syscall_nonlinux.go @@ -1,4 +1,4 @@ -// +build !linux appengine !go1.7 +// +build !linux appengine /* * diff --git a/internal/transport/go16.go b/internal/transport/go16.go deleted file mode 100644 index e0d00115d8e4..000000000000 --- a/internal/transport/go16.go +++ /dev/null @@ -1,52 +0,0 @@ -// +build go1.6,!go1.7 - -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package transport - -import ( - "net" - "net/http" - - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - "golang.org/x/net/context" -) - -// dialContext connects to the address on the named network. -func dialContext(ctx context.Context, network, address string) (net.Conn, error) { - return (&net.Dialer{Cancel: ctx.Done()}).Dial(network, address) -} - -// ContextErr converts the error from context package into a status error. -func ContextErr(err error) error { - switch err { - case context.DeadlineExceeded: - return status.Error(codes.DeadlineExceeded, err.Error()) - case context.Canceled: - return status.Error(codes.Canceled, err.Error()) - } - return status.Errorf(codes.Internal, "Unexpected error from context packet: %v", err) -} - -// contextFromRequest returns a background context. -func contextFromRequest(r *http.Request) context.Context { - return context.Background() -} diff --git a/internal/transport/go17.go b/internal/transport/go17.go deleted file mode 100644 index 4d515b00dcf5..000000000000 --- a/internal/transport/go17.go +++ /dev/null @@ -1,53 +0,0 @@ -// +build go1.7 - -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package transport - -import ( - "context" - "net" - "net/http" - - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - netctx "golang.org/x/net/context" -) - -// dialContext connects to the address on the named network. -func dialContext(ctx context.Context, network, address string) (net.Conn, error) { - return (&net.Dialer{}).DialContext(ctx, network, address) -} - -// ContextErr converts the error from context package into a status error. -func ContextErr(err error) error { - switch err { - case context.DeadlineExceeded, netctx.DeadlineExceeded: - return status.Error(codes.DeadlineExceeded, err.Error()) - case context.Canceled, netctx.Canceled: - return status.Error(codes.Canceled, err.Error()) - } - return status.Errorf(codes.Internal, "Unexpected error from context packet: %v", err) -} - -// contextFromRequest returns a context from the HTTP Request. -func contextFromRequest(r *http.Request) context.Context { - return r.Context() -} diff --git a/internal/transport/handler_server.go b/internal/transport/handler_server.go index c6fb4b9c13b2..0b4a3c5e3528 100644 --- a/internal/transport/handler_server.go +++ b/internal/transport/handler_server.go @@ -307,7 +307,7 @@ func (ht *serverHandlerTransport) WriteHeader(s *Stream, md metadata.MD) error { func (ht *serverHandlerTransport) HandleStreams(startStream func(*Stream), traceCtx func(context.Context, string) context.Context) { // With this transport type there will be exactly 1 stream: this HTTP request. - ctx := contextFromRequest(ht.req) + ctx := ht.req.Context() var cancel context.CancelFunc if ht.timeoutSet { ctx, cancel = context.WithTimeout(ctx, ht.timeout) diff --git a/internal/transport/http2_client.go b/internal/transport/http2_client.go index 54a5f70e7a7c..a1e602658cdf 100644 --- a/internal/transport/http2_client.go +++ b/internal/transport/http2_client.go @@ -122,7 +122,7 @@ func dial(ctx context.Context, fn func(context.Context, string) (net.Conn, error if fn != nil { return fn(ctx, addr) } - return dialContext(ctx, "tcp", addr) + return (&net.Dialer{}).DialContext(ctx, "tcp", addr) } func isTemporary(err error) bool { diff --git a/internal/transport/transport.go b/internal/transport/transport.go index c2547322592c..4d7e89067c22 100644 --- a/internal/transport/transport.go +++ b/internal/transport/transport.go @@ -22,6 +22,7 @@ package transport import ( + "context" "errors" "fmt" "io" @@ -29,7 +30,6 @@ import ( "sync" "sync/atomic" - "golang.org/x/net/context" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" "google.golang.org/grpc/keepalive" @@ -723,3 +723,14 @@ type channelzData struct { lastMsgSentTime int64 lastMsgRecvTime int64 } + +// ContextErr converts the error from context package into a status error. +func ContextErr(err error) error { + switch err { + case context.DeadlineExceeded: + return status.Error(codes.DeadlineExceeded, err.Error()) + case context.Canceled: + return status.Error(codes.Canceled, err.Error()) + } + return status.Errorf(codes.Internal, "Unexpected error from context packet: %v", err) +} diff --git a/naming/dns_resolver.go b/naming/dns_resolver.go index 0f8a908ea9c1..11ac8dac4753 100644 --- a/naming/dns_resolver.go +++ b/naming/dns_resolver.go @@ -37,6 +37,9 @@ const ( var ( errMissingAddr = errors.New("missing address") errWatcherClose = errors.New("watcher has been closed") + + lookupHost = net.DefaultResolver.LookupHost + lookupSRV = net.DefaultResolver.LookupSRV ) // NewDNSResolverWithFreq creates a DNS Resolver that can resolve DNS names, and diff --git a/naming/dns_resolver_test.go b/naming/dns_resolver_test.go index be1ac1aeca71..42868a5d0bd4 100644 --- a/naming/dns_resolver_test.go +++ b/naming/dns_resolver_test.go @@ -19,6 +19,7 @@ package naming import ( + "context" "fmt" "net" "reflect" @@ -253,6 +254,21 @@ func testResolver(t *testing.T, freq time.Duration, slp time.Duration) { } } +func replaceNetFunc() func() { + oldLookupHost := lookupHost + oldLookupSRV := lookupSRV + lookupHost = func(ctx context.Context, host string) ([]string, error) { + return hostLookup(host) + } + lookupSRV = func(ctx context.Context, service, proto, name string) (string, []*net.SRV, error) { + return srvLookup(service, proto, name) + } + return func() { + lookupHost = oldLookupHost + lookupSRV = oldLookupSRV + } +} + func TestResolve(t *testing.T) { defer replaceNetFunc()() testResolver(t, time.Millisecond*5, time.Millisecond*10) diff --git a/naming/go17.go b/naming/go17.go deleted file mode 100644 index 57b65d7b8898..000000000000 --- a/naming/go17.go +++ /dev/null @@ -1,34 +0,0 @@ -// +build go1.6,!go1.8 - -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package naming - -import ( - "net" - - "golang.org/x/net/context" -) - -var ( - lookupHost = func(ctx context.Context, host string) ([]string, error) { return net.LookupHost(host) } - lookupSRV = func(ctx context.Context, service, proto, name string) (string, []*net.SRV, error) { - return net.LookupSRV(service, proto, name) - } -) diff --git a/naming/go17_test.go b/naming/go17_test.go deleted file mode 100644 index db39b9ab7841..000000000000 --- a/naming/go17_test.go +++ /dev/null @@ -1,42 +0,0 @@ -// +build go1.6, !go1.8 - -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package naming - -import ( - "net" - - "golang.org/x/net/context" -) - -func replaceNetFunc() func() { - oldLookupHost := lookupHost - oldLookupSRV := lookupSRV - lookupHost = func(ctx context.Context, host string) ([]string, error) { - return hostLookup(host) - } - lookupSRV = func(ctx context.Context, service, proto, name string) (string, []*net.SRV, error) { - return srvLookup(service, proto, name) - } - return func() { - lookupHost = oldLookupHost - lookupSRV = oldLookupSRV - } -} diff --git a/naming/go18.go b/naming/go18.go deleted file mode 100644 index b5a0f842748f..000000000000 --- a/naming/go18.go +++ /dev/null @@ -1,28 +0,0 @@ -// +build go1.8 - -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package naming - -import "net" - -var ( - lookupHost = net.DefaultResolver.LookupHost - lookupSRV = net.DefaultResolver.LookupSRV -) diff --git a/naming/go18_test.go b/naming/go18_test.go deleted file mode 100644 index 5e297539ba57..000000000000 --- a/naming/go18_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// +build go1.8 - -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package naming - -import ( - "context" - "net" -) - -func replaceNetFunc() func() { - oldLookupHost := lookupHost - oldLookupSRV := lookupSRV - lookupHost = func(ctx context.Context, host string) ([]string, error) { - return hostLookup(host) - } - lookupSRV = func(ctx context.Context, service, proto, name string) (string, []*net.SRV, error) { - return srvLookup(service, proto, name) - } - return func() { - lookupHost = oldLookupHost - lookupSRV = oldLookupSRV - } -} diff --git a/proxy.go b/proxy.go index 2d40236e2180..5547ea8d8f8c 100644 --- a/proxy.go +++ b/proxy.go @@ -20,6 +20,7 @@ package grpc import ( "bufio" + "context" "errors" "fmt" "io" @@ -27,8 +28,6 @@ import ( "net/http" "net/http/httputil" "net/url" - - "golang.org/x/net/context" ) var ( @@ -128,3 +127,11 @@ func newProxyDialer(dialer func(context.Context, string) (net.Conn, error)) func return } } + +func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error { + req = req.WithContext(ctx) + if err := req.Write(conn); err != nil { + return fmt.Errorf("failed to write the HTTP request: %v", err) + } + return nil +} diff --git a/resolver/dns/dns_resolver.go b/resolver/dns/dns_resolver.go index 4af67422c6b9..56f28a647da1 100644 --- a/resolver/dns/dns_resolver.go +++ b/resolver/dns/dns_resolver.go @@ -43,9 +43,10 @@ func init() { } const ( - defaultPort = "443" - defaultFreq = time.Minute * 30 - golang = "GO" + defaultPort = "443" + defaultFreq = time.Minute * 30 + defaultDNSSvrPort = "53" + golang = "GO" // In DNS, service config is encoded in a TXT record via the mechanism // described in RFC-1464 using the attribute name grpc_config. txtAttribute = "grpc_config=" @@ -61,6 +62,31 @@ var ( errEndsWithColon = errors.New("dns resolver: missing port after port-separator colon") ) +var ( + defaultResolver netResolver = net.DefaultResolver +) + +var customAuthorityDialler = func(authority string) func(ctx context.Context, network, address string) (net.Conn, error) { + return func(ctx context.Context, network, address string) (net.Conn, error) { + var dialer net.Dialer + return dialer.DialContext(ctx, network, authority) + } +} + +var customAuthorityResolver = func(authority string) (netResolver, error) { + host, port, err := parseTarget(authority, defaultDNSSvrPort) + if err != nil { + return nil, err + } + + authorityWithPort := net.JoinHostPort(host, port) + + return &net.Resolver{ + PreferGo: true, + Dial: customAuthorityDialler(authorityWithPort), + }, nil +} + // NewBuilder creates a dnsBuilder which is used to factory DNS resolvers. func NewBuilder() resolver.Builder { return &dnsBuilder{minFreq: defaultFreq} diff --git a/resolver/dns/dns_resolver_go19_test.go b/resolver/dns/dns_resolver_go19_test.go deleted file mode 100644 index 8b179381701b..000000000000 --- a/resolver/dns/dns_resolver_go19_test.go +++ /dev/null @@ -1,135 +0,0 @@ -// +build go1.9 - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package dns - -import ( - "errors" - "fmt" - "net" - "testing" - - "golang.org/x/net/context" - "google.golang.org/grpc/internal/leakcheck" - "google.golang.org/grpc/resolver" -) - -func TestCustomAuthority(t *testing.T) { - defer leakcheck.Check(t) - - tests := []struct { - authority string - authorityWant string - expectError bool - }{ - { - "4.3.2.1:" + defaultDNSSvrPort, - "4.3.2.1:" + defaultDNSSvrPort, - false, - }, - { - "4.3.2.1:123", - "4.3.2.1:123", - false, - }, - { - "4.3.2.1", - "4.3.2.1:" + defaultDNSSvrPort, - false, - }, - { - "::1", - "[::1]:" + defaultDNSSvrPort, - false, - }, - { - "[::1]", - "[::1]:" + defaultDNSSvrPort, - false, - }, - { - "[::1]:123", - "[::1]:123", - false, - }, - { - "dnsserver.com", - "dnsserver.com:" + defaultDNSSvrPort, - false, - }, - { - ":123", - "localhost:123", - false, - }, - { - ":", - "", - true, - }, - { - "[::1]:", - "", - true, - }, - { - "dnsserver.com:", - "", - true, - }, - } - oldCustomAuthorityDialler := customAuthorityDialler - defer func() { - customAuthorityDialler = oldCustomAuthorityDialler - }() - - for _, a := range tests { - errChan := make(chan error, 1) - customAuthorityDialler = func(authority string) func(ctx context.Context, network, address string) (net.Conn, error) { - if authority != a.authorityWant { - errChan <- fmt.Errorf("wrong custom authority passed to resolver. input: %s expected: %s actual: %s", a.authority, a.authorityWant, authority) - } else { - errChan <- nil - } - return func(ctx context.Context, network, address string) (net.Conn, error) { - return nil, errors.New("no need to dial") - } - } - - b := NewBuilder() - cc := &testClientConn{target: "foo.bar.com"} - r, err := b.Build(resolver.Target{Endpoint: "foo.bar.com", Authority: a.authority}, cc, resolver.BuildOption{}) - - if err == nil { - r.Close() - - err = <-errChan - if err != nil { - t.Errorf(err.Error()) - } - - if a.expectError { - t.Errorf("custom authority should have caused an error: %s", a.authority) - } - } else if !a.expectError { - t.Errorf("unexpected error using custom authority %s: %s", a.authority, err) - } - } -} diff --git a/resolver/dns/dns_resolver_test.go b/resolver/dns/dns_resolver_test.go index 222eb516fb4c..940558414e01 100644 --- a/resolver/dns/dns_resolver_test.go +++ b/resolver/dns/dns_resolver_test.go @@ -19,6 +19,7 @@ package dns import ( + "errors" "fmt" "net" "os" @@ -904,7 +905,7 @@ func TestResolveFunc(t *testing.T) { {"[2001:db8::1]:", errEndsWithColon}, {":", errEndsWithColon}, {"", errMissingAddr}, - {"[2001:db8:a0b:12f0::1", errForInvalidTarget}, + {"[2001:db8:a0b:12f0::1", fmt.Errorf("invalid target address [2001:db8:a0b:12f0::1, error info: address [2001:db8:a0b:12f0::1:443: missing ']' in address")}, } b := NewBuilder() @@ -1018,3 +1019,106 @@ func TestDNSResolverRetry(t *testing.T) { } r.Close() } + +func TestCustomAuthority(t *testing.T) { + defer leakcheck.Check(t) + + tests := []struct { + authority string + authorityWant string + expectError bool + }{ + { + "4.3.2.1:" + defaultDNSSvrPort, + "4.3.2.1:" + defaultDNSSvrPort, + false, + }, + { + "4.3.2.1:123", + "4.3.2.1:123", + false, + }, + { + "4.3.2.1", + "4.3.2.1:" + defaultDNSSvrPort, + false, + }, + { + "::1", + "[::1]:" + defaultDNSSvrPort, + false, + }, + { + "[::1]", + "[::1]:" + defaultDNSSvrPort, + false, + }, + { + "[::1]:123", + "[::1]:123", + false, + }, + { + "dnsserver.com", + "dnsserver.com:" + defaultDNSSvrPort, + false, + }, + { + ":123", + "localhost:123", + false, + }, + { + ":", + "", + true, + }, + { + "[::1]:", + "", + true, + }, + { + "dnsserver.com:", + "", + true, + }, + } + oldCustomAuthorityDialler := customAuthorityDialler + defer func() { + customAuthorityDialler = oldCustomAuthorityDialler + }() + + for _, a := range tests { + errChan := make(chan error, 1) + customAuthorityDialler = func(authority string) func(ctx context.Context, network, address string) (net.Conn, error) { + if authority != a.authorityWant { + errChan <- fmt.Errorf("wrong custom authority passed to resolver. input: %s expected: %s actual: %s", a.authority, a.authorityWant, authority) + } else { + errChan <- nil + } + return func(ctx context.Context, network, address string) (net.Conn, error) { + return nil, errors.New("no need to dial") + } + } + + b := NewBuilder() + cc := &testClientConn{target: "foo.bar.com"} + r, err := b.Build(resolver.Target{Endpoint: "foo.bar.com", Authority: a.authority}, cc, resolver.BuildOption{}) + + if err == nil { + r.Close() + + err = <-errChan + if err != nil { + t.Errorf(err.Error()) + } + + if a.expectError { + t.Errorf("custom authority should have caused an error: %s", a.authority) + } + } else if !a.expectError { + t.Errorf("unexpected error using custom authority %s: %s", a.authority, err) + } + } +} diff --git a/resolver/dns/go18_test.go b/resolver/dns/go18_test.go deleted file mode 100644 index 1743f3a23778..000000000000 --- a/resolver/dns/go18_test.go +++ /dev/null @@ -1,27 +0,0 @@ -// +build go1.8 - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package dns - -import ( - "fmt" -) - -var errForInvalidTarget = fmt.Errorf("invalid target address [2001:db8:a0b:12f0::1, error info: address [2001:db8:a0b:12f0::1:443: missing ']' in address") diff --git a/resolver/dns/go19.go b/resolver/dns/go19.go deleted file mode 100644 index 9886de27547f..000000000000 --- a/resolver/dns/go19.go +++ /dev/null @@ -1,54 +0,0 @@ -// +build go1.9 - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package dns - -import ( - "net" - - "golang.org/x/net/context" -) - -var ( - defaultResolver netResolver = net.DefaultResolver -) - -const defaultDNSSvrPort = "53" - -var customAuthorityDialler = func(authority string) func(ctx context.Context, network, address string) (net.Conn, error) { - return func(ctx context.Context, network, address string) (net.Conn, error) { - var dialer net.Dialer - return dialer.DialContext(ctx, network, authority) - } -} - -var customAuthorityResolver = func(authority string) (netResolver, error) { - host, port, err := parseTarget(authority, defaultDNSSvrPort) - if err != nil { - return nil, err - } - - authorityWithPort := net.JoinHostPort(host, port) - - return &net.Resolver{ - PreferGo: true, - Dial: customAuthorityDialler(authorityWithPort), - }, nil -} diff --git a/resolver/dns/pre_go18_test.go b/resolver/dns/pre_go18_test.go deleted file mode 100644 index 4bf305e26e15..000000000000 --- a/resolver/dns/pre_go18_test.go +++ /dev/null @@ -1,27 +0,0 @@ -// +build go1.6, !go1.8 - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package dns - -import ( - "fmt" -) - -var errForInvalidTarget = fmt.Errorf("invalid target address [2001:db8:a0b:12f0::1, error info: missing ']' in address [2001:db8:a0b:12f0::1:443") diff --git a/resolver/dns/pre_go19.go b/resolver/dns/pre_go19.go deleted file mode 100644 index 70428113b84c..000000000000 --- a/resolver/dns/pre_go19.go +++ /dev/null @@ -1,51 +0,0 @@ -// +build go1.6, !go1.9 - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package dns - -import ( - "fmt" - "net" - - "golang.org/x/net/context" -) - -var ( - defaultResolver netResolver = &preGo19Resolver{} -) - -type preGo19Resolver struct { -} - -func (*preGo19Resolver) LookupHost(ctx context.Context, host string) ([]string, error) { - return net.LookupHost(host) -} - -func (*preGo19Resolver) LookupSRV(ctx context.Context, service, proto, name string) (string, []*net.SRV, error) { - return net.LookupSRV(service, proto, name) -} - -func (*preGo19Resolver) LookupTXT(ctx context.Context, name string) ([]string, error) { - return net.LookupTXT(name) -} - -var customAuthorityResolver = func(authority string) (netResolver, error) { - return nil, fmt.Errorf("Default DNS resolver does not support custom DNS server with go < 1.9") -} diff --git a/rpc_util.go b/rpc_util.go index b645998e3ed4..86f00e5a2d1f 100644 --- a/rpc_util.go +++ b/rpc_util.go @@ -21,6 +21,7 @@ package grpc import ( "bytes" "compress/gzip" + "context" "encoding/binary" "fmt" "io" @@ -31,7 +32,6 @@ import ( "sync" "time" - "golang.org/x/net/context" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" "google.golang.org/grpc/encoding" @@ -705,6 +705,31 @@ func Errorf(c codes.Code, format string, a ...interface{}) error { return status.Errorf(c, format, a...) } +// toRPCErr converts an error into an error from the status package. +func toRPCErr(err error) error { + if err == nil || err == io.EOF { + return err + } + if err == io.ErrUnexpectedEOF { + return status.Error(codes.Internal, err.Error()) + } + if _, ok := status.FromError(err); ok { + return err + } + switch e := err.(type) { + case transport.ConnectionError: + return status.Error(codes.Unavailable, e.Desc) + default: + switch err { + case context.DeadlineExceeded: + return status.Error(codes.DeadlineExceeded, err.Error()) + case context.Canceled: + return status.Error(codes.Canceled, err.Error()) + } + } + return status.Error(codes.Unknown, err.Error()) +} + // setCallInfoCodec should only be called after CallOptions have been applied. func setCallInfoCodec(c *callInfo) error { if c.codec != nil { diff --git a/stats/stats_test.go b/stats/stats_test.go index c48143a39e33..c8a5f239776f 100644 --- a/stats/stats_test.go +++ b/stats/stats_test.go @@ -1,5 +1,3 @@ -// +build go1.7 - /* * * Copyright 2016 gRPC authors. diff --git a/status/go16.go b/status/go16.go deleted file mode 100644 index e59b53e82be7..000000000000 --- a/status/go16.go +++ /dev/null @@ -1,42 +0,0 @@ -// +build go1.6,!go1.7 - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package status - -import ( - "golang.org/x/net/context" - "google.golang.org/grpc/codes" -) - -// FromContextError converts a context error into a Status. It returns a -// Status with codes.OK if err is nil, or a Status with codes.Unknown if err is -// non-nil and not a context error. -func FromContextError(err error) *Status { - switch err { - case nil: - return New(codes.OK, "") - case context.DeadlineExceeded: - return New(codes.DeadlineExceeded, err.Error()) - case context.Canceled: - return New(codes.Canceled, err.Error()) - default: - return New(codes.Unknown, err.Error()) - } -} diff --git a/status/go17.go b/status/go17.go deleted file mode 100644 index 090215149cfb..000000000000 --- a/status/go17.go +++ /dev/null @@ -1,44 +0,0 @@ -// +build go1.7 - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package status - -import ( - "context" - - netctx "golang.org/x/net/context" - "google.golang.org/grpc/codes" -) - -// FromContextError converts a context error into a Status. It returns a -// Status with codes.OK if err is nil, or a Status with codes.Unknown if err is -// non-nil and not a context error. -func FromContextError(err error) *Status { - switch err { - case nil: - return New(codes.OK, "") - case context.DeadlineExceeded, netctx.DeadlineExceeded: - return New(codes.DeadlineExceeded, err.Error()) - case context.Canceled, netctx.Canceled: - return New(codes.Canceled, err.Error()) - default: - return New(codes.Unknown, err.Error()) - } -} diff --git a/status/go17_test.go b/status/go17_test.go deleted file mode 100644 index 2dc5add94046..000000000000 --- a/status/go17_test.go +++ /dev/null @@ -1,44 +0,0 @@ -// +build go1.7 - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package status - -import ( - "context" - "testing" - - "google.golang.org/grpc/codes" -) - -func TestFromStdContextError(t *testing.T) { - testCases := []struct { - in error - want *Status - }{ - {in: context.DeadlineExceeded, want: New(codes.DeadlineExceeded, context.DeadlineExceeded.Error())}, - {in: context.Canceled, want: New(codes.Canceled, context.Canceled.Error())}, - } - for _, tc := range testCases { - got := FromContextError(tc.in) - if got.Code() != tc.want.Code() || got.Message() != tc.want.Message() { - t.Errorf("FromContextError(%v) = %v; want %v", tc.in, got, tc.want) - } - } -} diff --git a/status/status.go b/status/status.go index 897321babffd..ed36681bb546 100644 --- a/status/status.go +++ b/status/status.go @@ -28,6 +28,7 @@ package status import ( + "context" "errors" "fmt" @@ -191,3 +192,19 @@ func Code(err error) codes.Code { } return codes.Unknown } + +// FromContextError converts a context error into a Status. It returns a +// Status with codes.OK if err is nil, or a Status with codes.Unknown if err is +// non-nil and not a context error. +func FromContextError(err error) *Status { + switch err { + case nil: + return New(codes.OK, "") + case context.DeadlineExceeded: + return New(codes.DeadlineExceeded, err.Error()) + case context.Canceled: + return New(codes.Canceled, err.Error()) + default: + return New(codes.Unknown, err.Error()) + } +} diff --git a/status/status_test.go b/status/status_test.go index 1eb132093209..011cb0aead73 100644 --- a/status/status_test.go +++ b/status/status_test.go @@ -19,6 +19,7 @@ package status import ( + "context" "errors" "fmt" "reflect" @@ -28,7 +29,6 @@ import ( "github.com/golang/protobuf/ptypes" apb "github.com/golang/protobuf/ptypes/any" dpb "github.com/golang/protobuf/ptypes/duration" - "golang.org/x/net/context" cpb "google.golang.org/genproto/googleapis/rpc/code" epb "google.golang.org/genproto/googleapis/rpc/errdetails" spb "google.golang.org/genproto/googleapis/rpc/status"