github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/cluster/dialprovider.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     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  
    15  package cluster
    16  
    17  import (
    18  	"crypto/tls"
    19  	"time"
    20  
    21  	"google.golang.org/grpc"
    22  	"google.golang.org/grpc/backoff"
    23  	"google.golang.org/grpc/credentials"
    24  
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/creds"
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/dbfactory"
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/grpcendpoint"
    28  )
    29  
    30  // We wrap the default environment dial provider. In the standby replication
    31  // case, we want the following differences:
    32  //
    33  // - client interceptors for transmitting our replication role.
    34  // - do not use environment credentials. (for now).
    35  type grpcDialProvider struct {
    36  	orig   dbfactory.GRPCDialProvider
    37  	ci     *clientinterceptor
    38  	tlsCfg *tls.Config
    39  	creds  credentials.PerRPCCredentials
    40  }
    41  
    42  func (p grpcDialProvider) GetGRPCDialParams(config grpcendpoint.Config) (dbfactory.GRPCRemoteConfig, error) {
    43  	config.TLSConfig = p.tlsCfg
    44  	config.Creds = p.creds
    45  	if config.Creds != nil && config.TLSConfig != nil {
    46  		if c, ok := config.Creds.(*creds.RPCCreds); ok {
    47  			c.RequireTLS = true
    48  		}
    49  	}
    50  	config.WithEnvCreds = false
    51  	cfg, err := p.orig.GetGRPCDialParams(config)
    52  	if err != nil {
    53  		return dbfactory.GRPCRemoteConfig{}, err
    54  	}
    55  
    56  	cfg.DialOptions = append(cfg.DialOptions, p.ci.Options()...)
    57  	cfg.DialOptions = append(cfg.DialOptions, grpc.WithConnectParams(grpc.ConnectParams{
    58  		Backoff: backoff.Config{
    59  			BaseDelay:  250 * time.Millisecond,
    60  			Multiplier: 1.6,
    61  			Jitter:     0.6,
    62  			MaxDelay:   10 * time.Second,
    63  		},
    64  		MinConnectTimeout: 250 * time.Millisecond,
    65  	}))
    66  
    67  	return cfg, nil
    68  }