Skip to content

Commit

Permalink
chore(spanner): source default opts from gapic client (#3572)
Browse files Browse the repository at this point in the history
Sources the default client options from the generated client instead of the hand-written client. This makes sure that new default options that are introduced in the future will automatically be picked up by the Spanner client.

Fixes #3532
  • Loading branch information
olavloite committed Jan 19, 2021
1 parent e0887c7 commit 549c351
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
25 changes: 25 additions & 0 deletions spanner/apiv1/spanner_client_options.go
@@ -0,0 +1,25 @@
// Copyright 2021 Google LLC
//
// 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
//
// https://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 spanner

import "google.golang.org/api/option"

// Returns the default client options used by the generated Spanner client.
//
// This function is only intended for use by the client library, and may be
// removed at any time without any warning.
func DefaultClientOptions() []option.ClientOption {
return defaultClientOptions()
}
50 changes: 15 additions & 35 deletions spanner/client.go
Expand Up @@ -36,9 +36,6 @@ import (
)

const (
endpoint = "spanner.googleapis.com:443"
mtlsEndpoint = "spanner.mtls.googleapis.com:443"

// resourcePrefixHeader is the name of the metadata header used to indicate
// the resource being operated on.
resourcePrefixHeader = "google-cloud-resource-prefix"
Expand All @@ -47,14 +44,6 @@ const (
numChannels = 4
)

const (
// Scope is the scope for Cloud Spanner Data API.
Scope = "https://www.googleapis.com/auth/spanner.data"

// AdminScope is the scope for Cloud Spanner Admin APIs.
AdminScope = "https://www.googleapis.com/auth/spanner.admin"
)

var (
validDBPattern = regexp.MustCompile("^projects/(?P<project>[^/]+)/instances/(?P<instance>[^/]+)/databases/(?P<database>[^/]+)$")
)
Expand Down Expand Up @@ -116,13 +105,6 @@ type ClientConfig struct {
logger *log.Logger
}

// errDial returns error for dialing to Cloud Spanner.
func errDial(ci int, err error) error {
e := ToSpannerError(err).(*Error)
e.decorate(fmt.Sprintf("dialing fails for channel[%v]", ci))
return e
}

func contextWithOutgoingMetadata(ctx context.Context, md metadata.MD) context.Context {
existing, ok := metadata.FromOutgoingContext(ctx)
if ok {
Expand Down Expand Up @@ -166,23 +148,7 @@ func NewClientWithConfig(ctx context.Context, database string, config ClientConf
config.NumChannels = numChannels
}
// gRPC options.
allOpts := []option.ClientOption{
internaloption.WithDefaultEndpoint(endpoint),
internaloption.WithDefaultMTLSEndpoint(mtlsEndpoint),
option.WithScopes(Scope),
option.WithGRPCDialOption(
grpc.WithDefaultCallOptions(
grpc.MaxCallSendMsgSize(100<<20),
grpc.MaxCallRecvMsgSize(100<<20),
),
),
option.WithGRPCConnectionPool(config.NumChannels),
option.WithUserAgent(clientUserAgent),
internaloption.EnableDirectPath(true),
}
// opts will take precedence above allOpts, as the values in opts will be
// applied after the values in allOpts.
allOpts = append(allOpts, opts...)
allOpts := allClientOpts(config.NumChannels, opts...)
pool, err := gtransport.DialPool(ctx, allOpts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -228,6 +194,20 @@ func NewClientWithConfig(ctx context.Context, database string, config ClientConf
return c, nil
}

// Combines the default options from the generated client, the default options
// of the hand-written client and the user options to one list of options.
// Precedence: userOpts > clientDefaultOpts > generatedDefaultOpts
func allClientOpts(numChannels int, userOpts ...option.ClientOption) []option.ClientOption {
generatedDefaultOpts := vkit.DefaultClientOptions()
clientDefaultOpts := []option.ClientOption{
option.WithGRPCConnectionPool(numChannels),
option.WithUserAgent(clientUserAgent),
internaloption.EnableDirectPath(true),
}
allDefaultOpts := append(generatedDefaultOpts, clientDefaultOpts...)
return append(allDefaultOpts, userOpts...)
}

// getQueryOptions returns the query options overwritten by the environment
// variables if exist. The input parameter is the query options set by users
// via application-level configuration. If the environment variables are set,
Expand Down

0 comments on commit 549c351

Please sign in to comment.