feat: gitea client
This commit is contained in:
538
vendor/google.golang.org/api/transport/grpc/dial.go
generated
vendored
Normal file
538
vendor/google.golang.org/api/transport/grpc/dial.go
generated
vendored
Normal file
@@ -0,0 +1,538 @@
|
||||
// Copyright 2015 Google LLC.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package grpc supports network connections to GRPC servers.
|
||||
// This package is not intended for use by end developers. Use the
|
||||
// google.golang.org/api/option package to configure API clients.
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"cloud.google.com/go/auth"
|
||||
"cloud.google.com/go/auth/credentials"
|
||||
"cloud.google.com/go/auth/grpctransport"
|
||||
"cloud.google.com/go/auth/oauth2adapt"
|
||||
"cloud.google.com/go/compute/metadata"
|
||||
"go.opencensus.io/plugin/ocgrpc"
|
||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/time/rate"
|
||||
"google.golang.org/api/internal"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/grpc"
|
||||
grpcgoogle "google.golang.org/grpc/credentials/google"
|
||||
grpcinsecure "google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/credentials/oauth"
|
||||
"google.golang.org/grpc/stats"
|
||||
|
||||
// Install grpclb, which is required for direct path.
|
||||
_ "google.golang.org/grpc/balancer/grpclb"
|
||||
)
|
||||
|
||||
// Check env to disable DirectPath traffic.
|
||||
const disableDirectPath = "GOOGLE_CLOUD_DISABLE_DIRECT_PATH"
|
||||
|
||||
// Check env to decide if using google-c2p resolver for DirectPath traffic.
|
||||
const enableDirectPathXds = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS"
|
||||
|
||||
// Set at init time by dial_socketopt.go. If nil, socketopt is not supported.
|
||||
var timeoutDialerOption grpc.DialOption
|
||||
|
||||
// Log rate limiter
|
||||
var logRateLimiter = rate.Sometimes{Interval: 1 * time.Second}
|
||||
|
||||
// Assign to var for unit test replacement
|
||||
var dialContext = grpc.DialContext
|
||||
|
||||
// otelStatsHandler is a singleton otelgrpc.clientHandler to be used across
|
||||
// all dial connections to avoid the memory leak documented in
|
||||
// https://github.com/open-telemetry/opentelemetry-go-contrib/issues/4226
|
||||
//
|
||||
// TODO: If 4226 has been fixed in opentelemetry-go-contrib, replace this
|
||||
// singleton with inline usage for simplicity.
|
||||
var (
|
||||
initOtelStatsHandlerOnce sync.Once
|
||||
otelStatsHandler stats.Handler
|
||||
)
|
||||
|
||||
// otelGRPCStatsHandler returns singleton otelStatsHandler for reuse across all
|
||||
// dial connections.
|
||||
func otelGRPCStatsHandler() stats.Handler {
|
||||
initOtelStatsHandlerOnce.Do(func() {
|
||||
otelStatsHandler = otelgrpc.NewClientHandler()
|
||||
})
|
||||
return otelStatsHandler
|
||||
}
|
||||
|
||||
// Dial returns a GRPC connection for use communicating with a Google cloud
|
||||
// service, configured with the given ClientOptions.
|
||||
func Dial(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, error) {
|
||||
o, err := processAndValidateOpts(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o.GRPCConnPool != nil {
|
||||
return o.GRPCConnPool.Conn(), nil
|
||||
}
|
||||
if o.IsNewAuthLibraryEnabled() {
|
||||
pool, err := dialPoolNewAuth(ctx, true, 1, o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pool.Connection(), nil
|
||||
}
|
||||
// NOTE(cbro): We removed support for option.WithGRPCConnPool (GRPCConnPoolSize)
|
||||
// on 2020-02-12 because RoundRobin and WithBalancer are deprecated and we need to remove usages of it.
|
||||
//
|
||||
// Connection pooling is only done via DialPool.
|
||||
return dial(ctx, false, o)
|
||||
}
|
||||
|
||||
// DialInsecure returns an insecure GRPC connection for use communicating
|
||||
// with fake or mock Google cloud service implementations, such as emulators.
|
||||
// The connection is configured with the given ClientOptions.
|
||||
func DialInsecure(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, error) {
|
||||
o, err := processAndValidateOpts(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o.IsNewAuthLibraryEnabled() {
|
||||
pool, err := dialPoolNewAuth(ctx, false, 1, o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pool.Connection(), nil
|
||||
}
|
||||
return dial(ctx, true, o)
|
||||
}
|
||||
|
||||
// DialPool returns a pool of GRPC connections for the given service.
|
||||
// This differs from the connection pooling implementation used by Dial, which uses a custom GRPC load balancer.
|
||||
// DialPool should be used instead of Dial when a pool is used by default or a different custom GRPC load balancer is needed.
|
||||
// The context and options are shared between each Conn in the pool.
|
||||
// The pool size is configured using the WithGRPCConnectionPool option.
|
||||
//
|
||||
// This API is subject to change as we further refine requirements. It will go away if gRPC stubs accept an interface instead of the concrete ClientConn type. See https://github.com/grpc/grpc-go/issues/1287.
|
||||
func DialPool(ctx context.Context, opts ...option.ClientOption) (ConnPool, error) {
|
||||
o, err := processAndValidateOpts(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o.GRPCConnPool != nil {
|
||||
return o.GRPCConnPool, nil
|
||||
}
|
||||
|
||||
if o.IsNewAuthLibraryEnabled() {
|
||||
if o.GRPCConn != nil {
|
||||
return &singleConnPool{o.GRPCConn}, nil
|
||||
}
|
||||
pool, err := dialPoolNewAuth(ctx, true, o.GRPCConnPoolSize, o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &poolAdapter{pool}, nil
|
||||
}
|
||||
|
||||
poolSize := o.GRPCConnPoolSize
|
||||
if o.GRPCConn != nil {
|
||||
// WithGRPCConn is technically incompatible with WithGRPCConnectionPool.
|
||||
// Always assume pool size is 1 when a grpc.ClientConn is explicitly used.
|
||||
poolSize = 1
|
||||
}
|
||||
o.GRPCConnPoolSize = 0 // we don't *need* to set this to zero, but it's safe to.
|
||||
|
||||
if poolSize == 0 || poolSize == 1 {
|
||||
// Fast path for common case for a connection pool with a single connection.
|
||||
conn, err := dial(ctx, false, o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &singleConnPool{conn}, nil
|
||||
}
|
||||
|
||||
pool := &roundRobinConnPool{}
|
||||
for i := 0; i < poolSize; i++ {
|
||||
conn, err := dial(ctx, false, o)
|
||||
if err != nil {
|
||||
defer pool.Close() // NOTE: error from Close is ignored.
|
||||
return nil, err
|
||||
}
|
||||
pool.conns = append(pool.conns, conn)
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
// dialPoolNewAuth is an adapter to call new auth library.
|
||||
func dialPoolNewAuth(ctx context.Context, secure bool, poolSize int, ds *internal.DialSettings) (grpctransport.GRPCClientConnPool, error) {
|
||||
// honor options if set
|
||||
var creds *auth.Credentials
|
||||
if ds.InternalCredentials != nil {
|
||||
creds = oauth2adapt.AuthCredentialsFromOauth2Credentials(ds.InternalCredentials)
|
||||
} else if ds.Credentials != nil {
|
||||
creds = oauth2adapt.AuthCredentialsFromOauth2Credentials(ds.Credentials)
|
||||
} else if ds.AuthCredentials != nil {
|
||||
creds = ds.AuthCredentials
|
||||
} else if ds.TokenSource != nil {
|
||||
credOpts := &auth.CredentialsOptions{
|
||||
TokenProvider: oauth2adapt.TokenProviderFromTokenSource(ds.TokenSource),
|
||||
}
|
||||
if ds.QuotaProject != "" {
|
||||
credOpts.QuotaProjectIDProvider = auth.CredentialsPropertyFunc(func(ctx context.Context) (string, error) {
|
||||
return ds.QuotaProject, nil
|
||||
})
|
||||
}
|
||||
creds = auth.NewCredentials(credOpts)
|
||||
}
|
||||
|
||||
var skipValidation bool
|
||||
// If our clients explicitly setup the credential skip validation as it is
|
||||
// assumed correct
|
||||
if ds.SkipValidation || ds.InternalCredentials != nil {
|
||||
skipValidation = true
|
||||
}
|
||||
|
||||
var aud string
|
||||
if len(ds.Audiences) > 0 {
|
||||
aud = ds.Audiences[0]
|
||||
}
|
||||
metadata := map[string]string{}
|
||||
if ds.QuotaProject != "" {
|
||||
metadata["X-goog-user-project"] = ds.QuotaProject
|
||||
}
|
||||
if ds.RequestReason != "" {
|
||||
metadata["X-goog-request-reason"] = ds.RequestReason
|
||||
}
|
||||
|
||||
// Defaults for older clients that don't set this value yet
|
||||
defaultEndpointTemplate := ds.DefaultEndpointTemplate
|
||||
if defaultEndpointTemplate == "" {
|
||||
defaultEndpointTemplate = ds.DefaultEndpoint
|
||||
}
|
||||
|
||||
tokenURL, oauth2Client, err := internal.GetOAuth2Configuration(ctx, ds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pool, err := grpctransport.Dial(ctx, secure, &grpctransport.Options{
|
||||
DisableTelemetry: ds.TelemetryDisabled,
|
||||
DisableAuthentication: ds.NoAuth,
|
||||
Endpoint: ds.Endpoint,
|
||||
Metadata: metadata,
|
||||
GRPCDialOpts: ds.GRPCDialOpts,
|
||||
PoolSize: poolSize,
|
||||
Credentials: creds,
|
||||
DetectOpts: &credentials.DetectOptions{
|
||||
Scopes: ds.Scopes,
|
||||
Audience: aud,
|
||||
CredentialsFile: ds.CredentialsFile,
|
||||
CredentialsJSON: ds.CredentialsJSON,
|
||||
TokenURL: tokenURL,
|
||||
Client: oauth2Client,
|
||||
},
|
||||
InternalOptions: &grpctransport.InternalOptions{
|
||||
EnableNonDefaultSAForDirectPath: ds.AllowNonDefaultServiceAccount,
|
||||
EnableDirectPath: ds.EnableDirectPath,
|
||||
EnableDirectPathXds: ds.EnableDirectPathXds,
|
||||
EnableJWTWithScope: ds.EnableJwtWithScope,
|
||||
DefaultAudience: ds.DefaultAudience,
|
||||
DefaultEndpointTemplate: defaultEndpointTemplate,
|
||||
DefaultMTLSEndpoint: ds.DefaultMTLSEndpoint,
|
||||
DefaultScopes: ds.DefaultScopes,
|
||||
SkipValidation: skipValidation,
|
||||
},
|
||||
})
|
||||
return pool, err
|
||||
}
|
||||
|
||||
func dial(ctx context.Context, insecure bool, o *internal.DialSettings) (*grpc.ClientConn, error) {
|
||||
if o.HTTPClient != nil {
|
||||
return nil, errors.New("unsupported HTTP client specified")
|
||||
}
|
||||
if o.GRPCConn != nil {
|
||||
return o.GRPCConn, nil
|
||||
}
|
||||
transportCreds, endpoint, err := internal.GetGRPCTransportConfigAndEndpoint(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if insecure {
|
||||
transportCreds = grpcinsecure.NewCredentials()
|
||||
}
|
||||
|
||||
// Initialize gRPC dial options with transport-level security options.
|
||||
grpcOpts := []grpc.DialOption{
|
||||
grpc.WithTransportCredentials(transportCreds),
|
||||
}
|
||||
|
||||
// Authentication can only be sent when communicating over a secure connection.
|
||||
//
|
||||
// TODO: Should we be more lenient in the future and allow sending credentials
|
||||
// when dialing an insecure connection?
|
||||
if !o.NoAuth && !insecure {
|
||||
if o.APIKey != "" {
|
||||
grpcOpts = append(grpcOpts, grpc.WithPerRPCCredentials(grpcAPIKey{
|
||||
apiKey: o.APIKey,
|
||||
requestReason: o.RequestReason,
|
||||
}))
|
||||
} else {
|
||||
creds, err := internal.Creds(ctx, o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o.TokenSource == nil {
|
||||
// We only validate non-tokensource creds, as TokenSource-based credentials
|
||||
// don't propagate universe.
|
||||
credsUniverseDomain, err := internal.GetUniverseDomain(creds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o.GetUniverseDomain() != credsUniverseDomain {
|
||||
return nil, internal.ErrUniverseNotMatch(o.GetUniverseDomain(), credsUniverseDomain)
|
||||
}
|
||||
}
|
||||
grpcOpts = append(grpcOpts, grpc.WithPerRPCCredentials(grpcTokenSource{
|
||||
TokenSource: oauth.TokenSource{TokenSource: creds.TokenSource},
|
||||
quotaProject: internal.GetQuotaProject(creds, o.QuotaProject),
|
||||
requestReason: o.RequestReason,
|
||||
}))
|
||||
// Attempt Direct Path:
|
||||
logRateLimiter.Do(func() {
|
||||
logDirectPathMisconfig(endpoint, creds.TokenSource, o)
|
||||
})
|
||||
if isDirectPathEnabled(endpoint, o) && isTokenSourceDirectPathCompatible(creds.TokenSource, o) && metadata.OnGCE() {
|
||||
// Overwrite all of the previously specific DialOptions, DirectPath uses its own set of credentials and certificates.
|
||||
grpcOpts = []grpc.DialOption{
|
||||
grpc.WithCredentialsBundle(grpcgoogle.NewDefaultCredentialsWithOptions(
|
||||
grpcgoogle.DefaultCredentialsOptions{
|
||||
PerRPCCreds: oauth.TokenSource{TokenSource: creds.TokenSource},
|
||||
})),
|
||||
}
|
||||
if timeoutDialerOption != nil {
|
||||
grpcOpts = append(grpcOpts, timeoutDialerOption)
|
||||
}
|
||||
// Check if google-c2p resolver is enabled for DirectPath
|
||||
if isDirectPathXdsUsed(o) {
|
||||
// google-c2p resolver target must not have a port number
|
||||
if addr, _, err := net.SplitHostPort(endpoint); err == nil {
|
||||
endpoint = "google-c2p:///" + addr
|
||||
} else {
|
||||
endpoint = "google-c2p:///" + endpoint
|
||||
}
|
||||
} else {
|
||||
if !strings.HasPrefix(endpoint, "dns:///") {
|
||||
endpoint = "dns:///" + endpoint
|
||||
}
|
||||
grpcOpts = append(grpcOpts,
|
||||
// For now all DirectPath go clients will be using the following lb config, but in future
|
||||
// when different services need different configs, then we should change this to a
|
||||
// per-service config.
|
||||
grpc.WithDisableServiceConfig(),
|
||||
grpc.WithDefaultServiceConfig(`{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"pick_first":{}}]}}]}`))
|
||||
}
|
||||
// TODO(cbro): add support for system parameters (quota project, request reason) via chained interceptor.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add tracing, but before the other options, so that clients can override the
|
||||
// gRPC stats handler.
|
||||
// This assumes that gRPC options are processed in order, left to right.
|
||||
grpcOpts = addOCStatsHandler(grpcOpts, o)
|
||||
grpcOpts = addOpenTelemetryStatsHandler(grpcOpts, o)
|
||||
grpcOpts = append(grpcOpts, o.GRPCDialOpts...)
|
||||
if o.UserAgent != "" {
|
||||
grpcOpts = append(grpcOpts, grpc.WithUserAgent(o.UserAgent))
|
||||
}
|
||||
|
||||
return dialContext(ctx, endpoint, grpcOpts...)
|
||||
}
|
||||
|
||||
func addOCStatsHandler(opts []grpc.DialOption, settings *internal.DialSettings) []grpc.DialOption {
|
||||
if settings.TelemetryDisabled {
|
||||
return opts
|
||||
}
|
||||
return append(opts, grpc.WithStatsHandler(&ocgrpc.ClientHandler{}))
|
||||
}
|
||||
|
||||
func addOpenTelemetryStatsHandler(opts []grpc.DialOption, settings *internal.DialSettings) []grpc.DialOption {
|
||||
if settings.TelemetryDisabled {
|
||||
return opts
|
||||
}
|
||||
return append(opts, grpc.WithStatsHandler(otelGRPCStatsHandler()))
|
||||
}
|
||||
|
||||
// grpcTokenSource supplies PerRPCCredentials from an oauth.TokenSource.
|
||||
type grpcTokenSource struct {
|
||||
oauth.TokenSource
|
||||
|
||||
// Additional metadata attached as headers.
|
||||
quotaProject string
|
||||
requestReason string
|
||||
}
|
||||
|
||||
// GetRequestMetadata gets the request metadata as a map from a grpcTokenSource.
|
||||
func (ts grpcTokenSource) GetRequestMetadata(ctx context.Context, uri ...string) (
|
||||
map[string]string, error) {
|
||||
metadata, err := ts.TokenSource.GetRequestMetadata(ctx, uri...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Attach system parameter
|
||||
if ts.quotaProject != "" {
|
||||
metadata["X-goog-user-project"] = ts.quotaProject
|
||||
}
|
||||
if ts.requestReason != "" {
|
||||
metadata["X-goog-request-reason"] = ts.requestReason
|
||||
}
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
// grpcAPIKey supplies PerRPCCredentials from an API Key.
|
||||
type grpcAPIKey struct {
|
||||
apiKey string
|
||||
|
||||
// Additional metadata attached as headers.
|
||||
requestReason string
|
||||
}
|
||||
|
||||
// GetRequestMetadata gets the request metadata as a map from a grpcAPIKey.
|
||||
func (ts grpcAPIKey) GetRequestMetadata(ctx context.Context, uri ...string) (
|
||||
map[string]string, error) {
|
||||
metadata := map[string]string{
|
||||
"X-goog-api-key": ts.apiKey,
|
||||
}
|
||||
if ts.requestReason != "" {
|
||||
metadata["X-goog-request-reason"] = ts.requestReason
|
||||
}
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
// RequireTransportSecurity indicates whether the credentials requires transport security.
|
||||
func (ts grpcAPIKey) RequireTransportSecurity() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func isDirectPathEnabled(endpoint string, o *internal.DialSettings) bool {
|
||||
if !o.EnableDirectPath {
|
||||
return false
|
||||
}
|
||||
if !checkDirectPathEndPoint(endpoint) {
|
||||
return false
|
||||
}
|
||||
if strings.EqualFold(os.Getenv(disableDirectPath), "true") {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isDirectPathXdsUsed(o *internal.DialSettings) bool {
|
||||
// Method 1: Enable DirectPath xDS by env;
|
||||
if strings.EqualFold(os.Getenv(enableDirectPathXds), "true") {
|
||||
return true
|
||||
}
|
||||
// Method 2: Enable DirectPath xDS by option;
|
||||
if o.EnableDirectPathXds {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
func isTokenSourceDirectPathCompatible(ts oauth2.TokenSource, o *internal.DialSettings) bool {
|
||||
if ts == nil {
|
||||
return false
|
||||
}
|
||||
tok, err := ts.Token()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if tok == nil {
|
||||
return false
|
||||
}
|
||||
if o.AllowNonDefaultServiceAccount {
|
||||
return true
|
||||
}
|
||||
if source, _ := tok.Extra("oauth2.google.tokenSource").(string); source != "compute-metadata" {
|
||||
return false
|
||||
}
|
||||
if acct, _ := tok.Extra("oauth2.google.serviceAccount").(string); acct != "default" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func checkDirectPathEndPoint(endpoint string) bool {
|
||||
// Only [dns:///]host[:port] is supported, not other schemes (e.g., "tcp://" or "unix://").
|
||||
// Also don't try direct path if the user has chosen an alternate name resolver
|
||||
// (i.e., via ":///" prefix).
|
||||
//
|
||||
// TODO(cbro): once gRPC has introspectible options, check the user hasn't
|
||||
// provided a custom dialer in gRPC options.
|
||||
if strings.Contains(endpoint, "://") && !strings.HasPrefix(endpoint, "dns:///") {
|
||||
return false
|
||||
}
|
||||
|
||||
if endpoint == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func logDirectPathMisconfig(endpoint string, ts oauth2.TokenSource, o *internal.DialSettings) {
|
||||
if isDirectPathXdsUsed(o) {
|
||||
// Case 1: does not enable DirectPath
|
||||
if !isDirectPathEnabled(endpoint, o) {
|
||||
log.Println("WARNING: DirectPath is misconfigured. Please set the EnableDirectPath option along with the EnableDirectPathXds option.")
|
||||
} else {
|
||||
// Case 2: credential is not correctly set
|
||||
if !isTokenSourceDirectPathCompatible(ts, o) {
|
||||
log.Println("WARNING: DirectPath is misconfigured. Please make sure the token source is fetched from GCE metadata server and the default service account is used.")
|
||||
}
|
||||
// Case 3: not running on GCE
|
||||
if !metadata.OnGCE() {
|
||||
log.Println("WARNING: DirectPath is misconfigured. DirectPath is only available in a GCE environment.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func processAndValidateOpts(opts []option.ClientOption) (*internal.DialSettings, error) {
|
||||
var o internal.DialSettings
|
||||
for _, opt := range opts {
|
||||
opt.Apply(&o)
|
||||
}
|
||||
if err := o.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &o, nil
|
||||
}
|
||||
|
||||
type connPoolOption struct{ ConnPool }
|
||||
|
||||
// WithConnPool returns a ClientOption that specifies the ConnPool
|
||||
// connection to use as the basis of communications.
|
||||
//
|
||||
// This is only to be used by Google client libraries internally, for example
|
||||
// when creating a longrunning API client that shares the same connection pool
|
||||
// as a service client.
|
||||
func WithConnPool(p ConnPool) option.ClientOption {
|
||||
return connPoolOption{p}
|
||||
}
|
||||
|
||||
func (o connPoolOption) Apply(s *internal.DialSettings) {
|
||||
s.GRPCConnPool = o.ConnPool
|
||||
}
|
||||
52
vendor/google.golang.org/api/transport/grpc/dial_socketopt.go
generated
vendored
Normal file
52
vendor/google.golang.org/api/transport/grpc/dial_socketopt.go
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2019 Google LLC.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build go1.11 && linux
|
||||
// +build go1.11,linux
|
||||
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
const (
|
||||
// defaultTCPUserTimeout is the default TCP_USER_TIMEOUT socket option. By
|
||||
// default is 20 seconds.
|
||||
tcpUserTimeoutMilliseconds = 20000
|
||||
|
||||
// Copied from golang.org/x/sys/unix.TCP_USER_TIMEOUT.
|
||||
tcpUserTimeoutOp = 0x12
|
||||
)
|
||||
|
||||
func init() {
|
||||
// timeoutDialerOption is a grpc.DialOption that contains dialer with
|
||||
// socket option TCP_USER_TIMEOUT. This dialer requires go versions 1.11+.
|
||||
timeoutDialerOption = grpc.WithContextDialer(dialTCPUserTimeout)
|
||||
}
|
||||
|
||||
func dialTCPUserTimeout(ctx context.Context, addr string) (net.Conn, error) {
|
||||
control := func(network, address string, c syscall.RawConn) error {
|
||||
var syscallErr error
|
||||
controlErr := c.Control(func(fd uintptr) {
|
||||
syscallErr = syscall.SetsockoptInt(
|
||||
int(fd), syscall.IPPROTO_TCP, tcpUserTimeoutOp, tcpUserTimeoutMilliseconds)
|
||||
})
|
||||
if syscallErr != nil {
|
||||
return syscallErr
|
||||
}
|
||||
if controlErr != nil {
|
||||
return controlErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
d := &net.Dialer{
|
||||
Control: control,
|
||||
}
|
||||
return d.DialContext(ctx, "tcp", addr)
|
||||
}
|
||||
117
vendor/google.golang.org/api/transport/grpc/pool.go
generated
vendored
Normal file
117
vendor/google.golang.org/api/transport/grpc/pool.go
generated
vendored
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright 2020 Google LLC.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
|
||||
"cloud.google.com/go/auth/grpctransport"
|
||||
"google.golang.org/api/internal"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// ConnPool is a pool of grpc.ClientConns.
|
||||
type ConnPool = internal.ConnPool // NOTE(cbro): type alias to export the type. It must live in internal to avoid a circular dependency.
|
||||
|
||||
var _ ConnPool = &roundRobinConnPool{}
|
||||
var _ ConnPool = &singleConnPool{}
|
||||
|
||||
// singleConnPool is a special case for a single connection.
|
||||
type singleConnPool struct {
|
||||
*grpc.ClientConn
|
||||
}
|
||||
|
||||
func (p *singleConnPool) Conn() *grpc.ClientConn { return p.ClientConn }
|
||||
func (p *singleConnPool) Num() int { return 1 }
|
||||
|
||||
type roundRobinConnPool struct {
|
||||
conns []*grpc.ClientConn
|
||||
|
||||
idx uint32 // access via sync/atomic
|
||||
}
|
||||
|
||||
func (p *roundRobinConnPool) Num() int {
|
||||
return len(p.conns)
|
||||
}
|
||||
|
||||
func (p *roundRobinConnPool) Conn() *grpc.ClientConn {
|
||||
i := atomic.AddUint32(&p.idx, 1)
|
||||
return p.conns[i%uint32(len(p.conns))]
|
||||
}
|
||||
|
||||
func (p *roundRobinConnPool) Close() error {
|
||||
var errs multiError
|
||||
for _, conn := range p.conns {
|
||||
if err := conn.Close(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
if len(errs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (p *roundRobinConnPool) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error {
|
||||
return p.Conn().Invoke(ctx, method, args, reply, opts...)
|
||||
}
|
||||
|
||||
func (p *roundRobinConnPool) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
||||
return p.Conn().NewStream(ctx, desc, method, opts...)
|
||||
}
|
||||
|
||||
// multiError represents errors from multiple conns in the group.
|
||||
//
|
||||
// TODO: figure out how and whether this is useful to export. End users should
|
||||
// not be depending on the transport/grpc package directly, so there might need
|
||||
// to be some service-specific multi-error type.
|
||||
type multiError []error
|
||||
|
||||
func (m multiError) Error() string {
|
||||
s, n := "", 0
|
||||
for _, e := range m {
|
||||
if e != nil {
|
||||
if n == 0 {
|
||||
s = e.Error()
|
||||
}
|
||||
n++
|
||||
}
|
||||
}
|
||||
switch n {
|
||||
case 0:
|
||||
return "(0 errors)"
|
||||
case 1:
|
||||
return s
|
||||
case 2:
|
||||
return s + " (and 1 other error)"
|
||||
}
|
||||
return fmt.Sprintf("%s (and %d other errors)", s, n-1)
|
||||
}
|
||||
|
||||
type poolAdapter struct {
|
||||
pool grpctransport.GRPCClientConnPool
|
||||
}
|
||||
|
||||
func (p *poolAdapter) Conn() *grpc.ClientConn {
|
||||
return p.pool.Connection()
|
||||
}
|
||||
|
||||
func (p *poolAdapter) Num() int {
|
||||
return p.pool.Len()
|
||||
}
|
||||
|
||||
func (p *poolAdapter) Close() error {
|
||||
return p.pool.Close()
|
||||
}
|
||||
|
||||
func (p *poolAdapter) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error {
|
||||
return p.pool.Invoke(ctx, method, args, reply, opts...)
|
||||
}
|
||||
|
||||
func (p *poolAdapter) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
||||
return p.pool.NewStream(ctx, desc, method, opts...)
|
||||
}
|
||||
347
vendor/google.golang.org/api/transport/http/dial.go
generated
vendored
Normal file
347
vendor/google.golang.org/api/transport/http/dial.go
generated
vendored
Normal file
@@ -0,0 +1,347 @@
|
||||
// Copyright 2015 Google LLC.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package http supports network connections to HTTP servers.
|
||||
// This package is not intended for use by end developers. Use the
|
||||
// google.golang.org/api/option package to configure API clients.
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"cloud.google.com/go/auth"
|
||||
"cloud.google.com/go/auth/credentials"
|
||||
"cloud.google.com/go/auth/httptransport"
|
||||
"cloud.google.com/go/auth/oauth2adapt"
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
"golang.org/x/net/http2"
|
||||
"golang.org/x/oauth2"
|
||||
"google.golang.org/api/googleapi/transport"
|
||||
"google.golang.org/api/internal"
|
||||
"google.golang.org/api/internal/cert"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/api/transport/http/internal/propagation"
|
||||
)
|
||||
|
||||
// NewClient returns an HTTP client for use communicating with a Google cloud
|
||||
// service, configured with the given ClientOptions. It also returns the endpoint
|
||||
// for the service as specified in the options.
|
||||
func NewClient(ctx context.Context, opts ...option.ClientOption) (*http.Client, string, error) {
|
||||
settings, err := newSettings(opts)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
clientCertSource, dialTLSContext, endpoint, err := internal.GetHTTPTransportConfigAndEndpoint(settings)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
// TODO(cbro): consider injecting the User-Agent even if an explicit HTTP client is provided?
|
||||
if settings.HTTPClient != nil {
|
||||
return settings.HTTPClient, endpoint, nil
|
||||
}
|
||||
|
||||
if settings.IsNewAuthLibraryEnabled() {
|
||||
client, err := newClientNewAuth(ctx, nil, settings)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return client, endpoint, nil
|
||||
}
|
||||
trans, err := newTransport(ctx, defaultBaseTransport(ctx, clientCertSource, dialTLSContext), settings)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return &http.Client{Transport: trans}, endpoint, nil
|
||||
}
|
||||
|
||||
// newClientNewAuth is an adapter to call new auth library.
|
||||
func newClientNewAuth(ctx context.Context, base http.RoundTripper, ds *internal.DialSettings) (*http.Client, error) {
|
||||
// honor options if set
|
||||
var creds *auth.Credentials
|
||||
if ds.InternalCredentials != nil {
|
||||
creds = oauth2adapt.AuthCredentialsFromOauth2Credentials(ds.InternalCredentials)
|
||||
} else if ds.Credentials != nil {
|
||||
creds = oauth2adapt.AuthCredentialsFromOauth2Credentials(ds.Credentials)
|
||||
} else if ds.AuthCredentials != nil {
|
||||
creds = ds.AuthCredentials
|
||||
} else if ds.TokenSource != nil {
|
||||
credOpts := &auth.CredentialsOptions{
|
||||
TokenProvider: oauth2adapt.TokenProviderFromTokenSource(ds.TokenSource),
|
||||
}
|
||||
if ds.QuotaProject != "" {
|
||||
credOpts.QuotaProjectIDProvider = auth.CredentialsPropertyFunc(func(ctx context.Context) (string, error) {
|
||||
return ds.QuotaProject, nil
|
||||
})
|
||||
}
|
||||
creds = auth.NewCredentials(credOpts)
|
||||
}
|
||||
|
||||
var skipValidation bool
|
||||
// If our clients explicitly setup the credential skip validation as it is
|
||||
// assumed correct
|
||||
if ds.SkipValidation || ds.InternalCredentials != nil {
|
||||
skipValidation = true
|
||||
}
|
||||
|
||||
// Defaults for older clients that don't set this value yet
|
||||
defaultEndpointTemplate := ds.DefaultEndpointTemplate
|
||||
if defaultEndpointTemplate == "" {
|
||||
defaultEndpointTemplate = ds.DefaultEndpoint
|
||||
}
|
||||
|
||||
var aud string
|
||||
if len(ds.Audiences) > 0 {
|
||||
aud = ds.Audiences[0]
|
||||
}
|
||||
headers := http.Header{}
|
||||
if ds.QuotaProject != "" {
|
||||
headers.Set("X-goog-user-project", ds.QuotaProject)
|
||||
}
|
||||
if ds.RequestReason != "" {
|
||||
headers.Set("X-goog-request-reason", ds.RequestReason)
|
||||
}
|
||||
tokenURL, oauth2Client, err := internal.GetOAuth2Configuration(ctx, ds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client, err := httptransport.NewClient(&httptransport.Options{
|
||||
DisableTelemetry: ds.TelemetryDisabled,
|
||||
DisableAuthentication: ds.NoAuth,
|
||||
Headers: headers,
|
||||
Endpoint: ds.Endpoint,
|
||||
APIKey: ds.APIKey,
|
||||
Credentials: creds,
|
||||
ClientCertProvider: ds.ClientCertSource,
|
||||
BaseRoundTripper: base,
|
||||
DetectOpts: &credentials.DetectOptions{
|
||||
Scopes: ds.Scopes,
|
||||
Audience: aud,
|
||||
CredentialsFile: ds.CredentialsFile,
|
||||
CredentialsJSON: ds.CredentialsJSON,
|
||||
TokenURL: tokenURL,
|
||||
Client: oauth2Client,
|
||||
},
|
||||
InternalOptions: &httptransport.InternalOptions{
|
||||
EnableJWTWithScope: ds.EnableJwtWithScope,
|
||||
DefaultAudience: ds.DefaultAudience,
|
||||
DefaultEndpointTemplate: defaultEndpointTemplate,
|
||||
DefaultMTLSEndpoint: ds.DefaultMTLSEndpoint,
|
||||
DefaultScopes: ds.DefaultScopes,
|
||||
SkipValidation: skipValidation,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// NewTransport creates an http.RoundTripper for use communicating with a Google
|
||||
// cloud service, configured with the given ClientOptions. Its RoundTrip method delegates to base.
|
||||
func NewTransport(ctx context.Context, base http.RoundTripper, opts ...option.ClientOption) (http.RoundTripper, error) {
|
||||
settings, err := newSettings(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if settings.HTTPClient != nil {
|
||||
return nil, errors.New("transport/http: WithHTTPClient passed to NewTransport")
|
||||
}
|
||||
if settings.IsNewAuthLibraryEnabled() {
|
||||
client, err := newClientNewAuth(ctx, base, settings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.Transport, nil
|
||||
}
|
||||
return newTransport(ctx, base, settings)
|
||||
}
|
||||
|
||||
func newTransport(ctx context.Context, base http.RoundTripper, settings *internal.DialSettings) (http.RoundTripper, error) {
|
||||
paramTransport := ¶meterTransport{
|
||||
base: base,
|
||||
userAgent: settings.UserAgent,
|
||||
requestReason: settings.RequestReason,
|
||||
}
|
||||
var trans http.RoundTripper = paramTransport
|
||||
// Give OpenTelemetry precedence over OpenCensus in case user configuration
|
||||
// causes both to write the same header (`X-Cloud-Trace-Context`).
|
||||
trans = addOpenTelemetryTransport(trans, settings)
|
||||
trans = addOCTransport(trans, settings)
|
||||
switch {
|
||||
case settings.NoAuth:
|
||||
// Do nothing.
|
||||
case settings.APIKey != "":
|
||||
paramTransport.quotaProject = internal.GetQuotaProject(nil, settings.QuotaProject)
|
||||
trans = &transport.APIKey{
|
||||
Transport: trans,
|
||||
Key: settings.APIKey,
|
||||
}
|
||||
default:
|
||||
creds, err := internal.Creds(ctx, settings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if settings.TokenSource == nil {
|
||||
// We only validate non-tokensource creds, as TokenSource-based credentials
|
||||
// don't propagate universe.
|
||||
credsUniverseDomain, err := internal.GetUniverseDomain(creds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if settings.GetUniverseDomain() != credsUniverseDomain {
|
||||
return nil, internal.ErrUniverseNotMatch(settings.GetUniverseDomain(), credsUniverseDomain)
|
||||
}
|
||||
}
|
||||
paramTransport.quotaProject = internal.GetQuotaProject(creds, settings.QuotaProject)
|
||||
ts := creds.TokenSource
|
||||
if settings.ImpersonationConfig == nil && settings.TokenSource != nil {
|
||||
ts = settings.TokenSource
|
||||
}
|
||||
trans = &oauth2.Transport{
|
||||
Base: trans,
|
||||
Source: ts,
|
||||
}
|
||||
}
|
||||
return trans, nil
|
||||
}
|
||||
|
||||
func newSettings(opts []option.ClientOption) (*internal.DialSettings, error) {
|
||||
var o internal.DialSettings
|
||||
for _, opt := range opts {
|
||||
opt.Apply(&o)
|
||||
}
|
||||
if err := o.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o.GRPCConn != nil {
|
||||
return nil, errors.New("unsupported gRPC connection specified")
|
||||
}
|
||||
return &o, nil
|
||||
}
|
||||
|
||||
type parameterTransport struct {
|
||||
userAgent string
|
||||
quotaProject string
|
||||
requestReason string
|
||||
|
||||
base http.RoundTripper
|
||||
}
|
||||
|
||||
func (t *parameterTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
rt := t.base
|
||||
if rt == nil {
|
||||
return nil, errors.New("transport: no Transport specified")
|
||||
}
|
||||
newReq := *req
|
||||
newReq.Header = make(http.Header)
|
||||
for k, vv := range req.Header {
|
||||
newReq.Header[k] = vv
|
||||
}
|
||||
if t.userAgent != "" {
|
||||
// TODO(cbro): append to existing User-Agent header?
|
||||
newReq.Header.Set("User-Agent", t.userAgent)
|
||||
}
|
||||
|
||||
// Attach system parameters into the header
|
||||
if t.quotaProject != "" {
|
||||
newReq.Header.Set("X-Goog-User-Project", t.quotaProject)
|
||||
}
|
||||
if t.requestReason != "" {
|
||||
newReq.Header.Set("X-Goog-Request-Reason", t.requestReason)
|
||||
}
|
||||
|
||||
return rt.RoundTrip(&newReq)
|
||||
}
|
||||
|
||||
// defaultBaseTransport returns the base HTTP transport. It uses a default
|
||||
// transport, taking most defaults from http.DefaultTransport.
|
||||
// If TLSCertificate is available, set TLSClientConfig as well.
|
||||
func defaultBaseTransport(ctx context.Context, clientCertSource cert.Source, dialTLSContext func(context.Context, string, string) (net.Conn, error)) http.RoundTripper {
|
||||
// Copy http.DefaultTransport except for MaxIdleConnsPerHost setting,
|
||||
// which is increased due to reported performance issues under load in the
|
||||
// GCS client. Transport.Clone is only available in Go 1.13 and up.
|
||||
trans := clonedTransport(http.DefaultTransport)
|
||||
if trans == nil {
|
||||
trans = fallbackBaseTransport()
|
||||
}
|
||||
trans.MaxIdleConnsPerHost = 100
|
||||
|
||||
if clientCertSource != nil {
|
||||
trans.TLSClientConfig = &tls.Config{
|
||||
GetClientCertificate: clientCertSource,
|
||||
}
|
||||
}
|
||||
if dialTLSContext != nil {
|
||||
// If DialTLSContext is set, TLSClientConfig wil be ignored
|
||||
trans.DialTLSContext = dialTLSContext
|
||||
}
|
||||
|
||||
configureHTTP2(trans)
|
||||
|
||||
return trans
|
||||
}
|
||||
|
||||
// configureHTTP2 configures the ReadIdleTimeout HTTP/2 option for the
|
||||
// transport. This allows broken idle connections to be pruned more quickly,
|
||||
// preventing the client from attempting to re-use connections that will no
|
||||
// longer work.
|
||||
func configureHTTP2(trans *http.Transport) {
|
||||
http2Trans, err := http2.ConfigureTransports(trans)
|
||||
if err == nil {
|
||||
http2Trans.ReadIdleTimeout = time.Second * 31
|
||||
}
|
||||
}
|
||||
|
||||
// fallbackBaseTransport is used in <go1.13 as well as in the rare case if
|
||||
// http.DefaultTransport has been reassigned something that's not a
|
||||
// *http.Transport.
|
||||
func fallbackBaseTransport() *http.Transport {
|
||||
return &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
func addOpenTelemetryTransport(trans http.RoundTripper, settings *internal.DialSettings) http.RoundTripper {
|
||||
if settings.TelemetryDisabled {
|
||||
return trans
|
||||
}
|
||||
return otelhttp.NewTransport(trans)
|
||||
}
|
||||
|
||||
func addOCTransport(trans http.RoundTripper, settings *internal.DialSettings) http.RoundTripper {
|
||||
if settings.TelemetryDisabled {
|
||||
return trans
|
||||
}
|
||||
return &ochttp.Transport{
|
||||
Base: trans,
|
||||
Propagation: &propagation.HTTPFormat{},
|
||||
}
|
||||
}
|
||||
|
||||
// clonedTransport returns the given RoundTripper as a cloned *http.Transport.
|
||||
// It returns nil if the RoundTripper can't be cloned or coerced to
|
||||
// *http.Transport.
|
||||
func clonedTransport(rt http.RoundTripper) *http.Transport {
|
||||
t, ok := rt.(*http.Transport)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return t.Clone()
|
||||
}
|
||||
87
vendor/google.golang.org/api/transport/http/internal/propagation/http.go
generated
vendored
Normal file
87
vendor/google.golang.org/api/transport/http/internal/propagation/http.go
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright 2018 Google LLC.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build go1.8
|
||||
// +build go1.8
|
||||
|
||||
// Package propagation implements X-Cloud-Trace-Context header propagation used
|
||||
// by Google Cloud products.
|
||||
package propagation
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.opencensus.io/trace"
|
||||
"go.opencensus.io/trace/propagation"
|
||||
)
|
||||
|
||||
const (
|
||||
httpHeaderMaxSize = 200
|
||||
httpHeader = `X-Cloud-Trace-Context`
|
||||
)
|
||||
|
||||
var _ propagation.HTTPFormat = (*HTTPFormat)(nil)
|
||||
|
||||
// HTTPFormat implements propagation.HTTPFormat to propagate
|
||||
// traces in HTTP headers for Google Cloud Platform and Stackdriver Trace.
|
||||
type HTTPFormat struct{}
|
||||
|
||||
// SpanContextFromRequest extracts a Stackdriver Trace span context from incoming requests.
|
||||
func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanContext, ok bool) {
|
||||
h := req.Header.Get(httpHeader)
|
||||
// See https://cloud.google.com/trace/docs/faq for the header HTTPFormat.
|
||||
// Return if the header is empty or missing, or if the header is unreasonably
|
||||
// large, to avoid making unnecessary copies of a large string.
|
||||
if h == "" || len(h) > httpHeaderMaxSize {
|
||||
return trace.SpanContext{}, false
|
||||
}
|
||||
|
||||
// Parse the trace id field.
|
||||
slash := strings.Index(h, `/`)
|
||||
if slash == -1 {
|
||||
return trace.SpanContext{}, false
|
||||
}
|
||||
tid, h := h[:slash], h[slash+1:]
|
||||
|
||||
buf, err := hex.DecodeString(tid)
|
||||
if err != nil {
|
||||
return trace.SpanContext{}, false
|
||||
}
|
||||
copy(sc.TraceID[:], buf)
|
||||
|
||||
// Parse the span id field.
|
||||
spanstr := h
|
||||
semicolon := strings.Index(h, `;`)
|
||||
if semicolon != -1 {
|
||||
spanstr, h = h[:semicolon], h[semicolon+1:]
|
||||
}
|
||||
sid, err := strconv.ParseUint(spanstr, 10, 64)
|
||||
if err != nil {
|
||||
return trace.SpanContext{}, false
|
||||
}
|
||||
binary.BigEndian.PutUint64(sc.SpanID[:], sid)
|
||||
|
||||
// Parse the options field, options field is optional.
|
||||
if !strings.HasPrefix(h, "o=") {
|
||||
return sc, true
|
||||
}
|
||||
o, err := strconv.ParseUint(h[2:], 10, 64)
|
||||
if err != nil {
|
||||
return trace.SpanContext{}, false
|
||||
}
|
||||
sc.TraceOptions = trace.TraceOptions(o)
|
||||
return sc, true
|
||||
}
|
||||
|
||||
// SpanContextToRequest modifies the given request to include a Stackdriver Trace header.
|
||||
func (f *HTTPFormat) SpanContextToRequest(sc trace.SpanContext, req *http.Request) {
|
||||
sid := binary.BigEndian.Uint64(sc.SpanID[:])
|
||||
header := fmt.Sprintf("%s/%d;o=%d", hex.EncodeToString(sc.TraceID[:]), sid, int64(sc.TraceOptions))
|
||||
req.Header.Set(httpHeader, header)
|
||||
}
|
||||
Reference in New Issue
Block a user