diff --git a/attributes/attributes.go b/attributes/attributes.go new file mode 100644 index 000000000000..68ffc6201375 --- /dev/null +++ b/attributes/attributes.go @@ -0,0 +1,70 @@ +/* + * + * Copyright 2019 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 attributes defines a generic key/value store used in various gRPC +// components. +// +// All APIs in this package are EXPERIMENTAL. +package attributes + +import "fmt" + +// Attributes is an immutable struct for storing and retrieving generic +// key/value pairs. Keys must be hashable, and users should define their own +// types for keys. +type Attributes struct { + m map[interface{}]interface{} +} + +// New returns a new Attributes containing all key/value pairs in kvs. If the +// same key appears multiple times, the last value overwrites all previous +// values for that key. Panics if len(kvs) is not even. +func New(kvs ...interface{}) *Attributes { + if len(kvs)%2 != 0 { + panic(fmt.Sprintf("attributes.New called with unexpected input: len(kvs) = %v", len(kvs))) + } + a := &Attributes{m: make(map[interface{}]interface{}, len(kvs)/2)} + for i := 0; i < len(kvs)/2; i++ { + a.m[kvs[i*2]] = kvs[i*2+1] + } + return a +} + +// WithValues returns a new Attributes containing all key/value pairs in a and +// kvs. Panics if len(kvs) is not even. If the same key appears multiple +// times, the last value overwrites all previous values for that key. To +// remove an existing key, use a nil value. +func (a *Attributes) WithValues(kvs ...interface{}) *Attributes { + if len(kvs)%2 != 0 { + panic(fmt.Sprintf("attributes.New called with unexpected input: len(kvs) = %v", len(kvs))) + } + n := &Attributes{m: make(map[interface{}]interface{}, len(a.m)+len(kvs)/2)} + for k, v := range a.m { + n.m[k] = v + } + for i := 0; i < len(kvs)/2; i++ { + n.m[kvs[i*2]] = kvs[i*2+1] + } + return n +} + +// Value returns the value associated with these attributes for key, or nil if +// no value is associated with key. +func (a *Attributes) Value(key interface{}) interface{} { + return a.m[key] +} diff --git a/attributes/attributes_test.go b/attributes/attributes_test.go new file mode 100644 index 000000000000..4cca17b55e93 --- /dev/null +++ b/attributes/attributes_test.go @@ -0,0 +1,48 @@ +/* + * + * Copyright 2019 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 attributes_test + +import ( + "fmt" + + "google.golang.org/grpc/attributes" +) + +func ExampleAttributes() { + type keyOne struct{} + type keyTwo struct{} + a := attributes.New(keyOne{}, 1, keyTwo{}, "two") + fmt.Println("Key one:", a.Value(keyOne{})) + fmt.Println("Key two:", a.Value(keyTwo{})) + // Output: + // Key one: 1 + // Key two: two +} + +func ExampleAttributes_WithValues() { + type keyOne struct{} + type keyTwo struct{} + a := attributes.New(keyOne{}, 1) + a = a.WithValues(keyTwo{}, "two") + fmt.Println("Key one:", a.Value(keyOne{})) + fmt.Println("Key two:", a.Value(keyTwo{})) + // Output: + // Key one: 1 + // Key two: two +} diff --git a/resolver/resolver.go b/resolver/resolver.go index 4c5423ba0fb2..538a5a39021e 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -24,6 +24,7 @@ import ( "context" "net" + "google.golang.org/grpc/attributes" "google.golang.org/grpc/credentials" "google.golang.org/grpc/serviceconfig" ) @@ -87,8 +88,7 @@ const ( type Address struct { // Addr is the server address on which a connection will be established. Addr string - // Type is the type of this address. - Type AddressType + // ServerName is the name of this address. // If non-empty, the ServerName is used as the transport certification authority for // the address, instead of the hostname from the Dial target string. In most cases, @@ -101,8 +101,20 @@ type Address struct { // is insecure to populate it with data from untrusted inputs since untrusted // values could be used to bypass the authority checks performed by TLS. ServerName string + + // Attributes contains arbitrary data about this address intended for + // consumption by the load balancing policy. + Attributes *attributes.Attributes + + // Type is the type of this address. + // + // Deprecated: use Attributes instead. + Type AddressType + // Metadata is the information associated with Addr, which may be used // to make load balancing decision. + // + // Deprecated: use Attributes instead. Metadata interface{} } @@ -141,6 +153,10 @@ type State struct { // config. If it is nil, it indicates no service config is present or the // resolver does not provide service configs. ServiceConfig *serviceconfig.ParseResult + + // Attributes contains arbitrary data about the resolver intended for + // consumption by the load balancing policy. + Attributes *attributes.Attributes } // ClientConn contains the callbacks for resolver to notify any updates diff --git a/vet.sh b/vet.sh index f324be509a47..1032cdd13832 100755 --- a/vet.sh +++ b/vet.sh @@ -114,29 +114,34 @@ fi # TODO(dfawley): don't use deprecated functions in examples. staticcheck -go 1.9 -checks 'inherit,-ST1015' -ignore ' google.golang.org/grpc/balancer.go:SA1019 +google.golang.org/grpc/balancer/grpclb/grpclb.go:SA1019 google.golang.org/grpc/balancer/grpclb/grpclb_remote_balancer.go:SA1019 google.golang.org/grpc/balancer/grpclb/grpclb_test.go:SA1019 +google.golang.org/grpc/balancer/grpclb/grpclb_util.go:SA1019 google.golang.org/grpc/balancer/roundrobin/roundrobin_test.go:SA1019 -google.golang.org/grpc/xds/internal/balancer/edsbalancer/balancergroup.go:SA1019 -google.golang.org/grpc/xds/internal/resolver/xds_resolver.go:SA1019 -google.golang.org/grpc/xds/internal/balancer/xds.go:SA1019 -google.golang.org/grpc/xds/internal/balancer/xds_client.go:SA1019 google.golang.org/grpc/balancer_conn_wrappers.go:SA1019 google.golang.org/grpc/balancer_test.go:SA1019 google.golang.org/grpc/benchmark/benchmain/main.go:SA1019 google.golang.org/grpc/benchmark/worker/benchmark_client.go:SA1019 +google.golang.org/grpc/clientconn.go:SA1019 google.golang.org/grpc/clientconn.go:S1024 google.golang.org/grpc/clientconn_state_transition_test.go:SA1019 google.golang.org/grpc/clientconn_test.go:SA1019 google.golang.org/grpc/examples/features/debugging/client/main.go:SA1019 google.golang.org/grpc/examples/features/load_balancing/client/main.go:SA1019 +google.golang.org/grpc/internal/resolver/dns/dns_resolver.go:SA1019 google.golang.org/grpc/internal/transport/handler_server.go:SA1019 google.golang.org/grpc/internal/transport/handler_server_test.go:SA1019 -google.golang.org/grpc/internal/resolver/dns/dns_resolver.go:SA1019 google.golang.org/grpc/stats/stats_test.go:SA1019 google.golang.org/grpc/test/balancer_test.go:SA1019 google.golang.org/grpc/test/channelz_test.go:SA1019 google.golang.org/grpc/test/end2end_test.go:SA1019 google.golang.org/grpc/test/healthcheck_test.go:SA1019 +google.golang.org/grpc/xds/internal/balancer/edsbalancer/balancergroup.go:SA1019 +google.golang.org/grpc/xds/internal/balancer/edsbalancer/edsbalancer.go:SA1019 +google.golang.org/grpc/xds/internal/balancer/xds.go:SA1019 +google.golang.org/grpc/xds/internal/balancer/xds_client.go:SA1019 +google.golang.org/grpc/xds/internal/resolver/xds_resolver.go:SA1019 +google.golang.org/grpc/xds/internal/resolver/xds_resolver.go:SA1019 ' ./... misspell -error .