You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

88 lines
3.0 KiB

  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package transport/grpc supports network connections to GRPC servers.
  15. // This package is not intended for use by end developers. Use the
  16. // google.golang.org/api/option package to configure API clients.
  17. package grpc
  18. import (
  19. "errors"
  20. "golang.org/x/net/context"
  21. "google.golang.org/api/internal"
  22. "google.golang.org/api/option"
  23. "google.golang.org/grpc"
  24. "google.golang.org/grpc/credentials"
  25. "google.golang.org/grpc/credentials/oauth"
  26. )
  27. // Set at init time by dial_appengine.go. If nil, we're not on App Engine.
  28. var appengineDialerHook func(context.Context) grpc.DialOption
  29. // Dial returns a GRPC connection for use communicating with a Google cloud
  30. // service, configured with the given ClientOptions.
  31. func Dial(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, error) {
  32. return dial(ctx, false, opts)
  33. }
  34. // DialInsecure returns an insecure GRPC connection for use communicating
  35. // with fake or mock Google cloud service implementations, such as emulators.
  36. // The connection is configured with the given ClientOptions.
  37. func DialInsecure(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, error) {
  38. return dial(ctx, true, opts)
  39. }
  40. func dial(ctx context.Context, insecure bool, opts []option.ClientOption) (*grpc.ClientConn, error) {
  41. var o internal.DialSettings
  42. for _, opt := range opts {
  43. opt.Apply(&o)
  44. }
  45. if err := o.Validate(); err != nil {
  46. return nil, err
  47. }
  48. if o.HTTPClient != nil {
  49. return nil, errors.New("unsupported HTTP client specified")
  50. }
  51. if o.GRPCConn != nil {
  52. return o.GRPCConn, nil
  53. }
  54. var grpcOpts []grpc.DialOption
  55. if insecure {
  56. grpcOpts = []grpc.DialOption{grpc.WithInsecure()}
  57. } else if !o.NoAuth {
  58. creds, err := internal.Creds(ctx, &o)
  59. if err != nil {
  60. return nil, err
  61. }
  62. grpcOpts = []grpc.DialOption{
  63. grpc.WithPerRPCCredentials(oauth.TokenSource{creds.TokenSource}),
  64. grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
  65. }
  66. }
  67. if appengineDialerHook != nil {
  68. // Use the Socket API on App Engine.
  69. grpcOpts = append(grpcOpts, appengineDialerHook(ctx))
  70. }
  71. // Add tracing, but before the other options, so that clients can override the
  72. // gRPC stats handler.
  73. // This assumes that gRPC options are processed in order, left to right.
  74. grpcOpts = addOCStatsHandler(grpcOpts)
  75. grpcOpts = append(grpcOpts, o.GRPCDialOpts...)
  76. if o.UserAgent != "" {
  77. grpcOpts = append(grpcOpts, grpc.WithUserAgent(o.UserAgent))
  78. }
  79. return grpc.DialContext(ctx, o.Endpoint, grpcOpts...)
  80. }