...

Source file src/cloud.google.com/go/auth/grpctransport/pool.go

Documentation: cloud.google.com/go/auth/grpctransport

     1  // Copyright 2023 Google LLC
     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 grpctransport
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"sync/atomic"
    21  
    22  	"google.golang.org/grpc"
    23  )
    24  
    25  // GRPCClientConnPool is an interface that satisfies
    26  // [google.golang.org/grpc.ClientConnInterface] and has some utility functions
    27  // that are needed for connection lifecycle when using in a client library. It
    28  // may be a pool or a single connection. This interface is not intended to, and
    29  // can't be, implemented by others.
    30  type GRPCClientConnPool interface {
    31  	// Connection returns a [google.golang.org/grpc.ClientConn] from the pool.
    32  	//
    33  	// ClientConn aren't returned to the pool and should not be closed directly.
    34  	Connection() *grpc.ClientConn
    35  
    36  	// Len returns the number of connections in the pool. It will always return
    37  	// the same value.
    38  	Len() int
    39  
    40  	// Close closes every ClientConn in the pool. The error returned by Close
    41  	// may be a single error or multiple errors.
    42  	Close() error
    43  
    44  	grpc.ClientConnInterface
    45  
    46  	// private ensure others outside this package can't implement this type
    47  	private()
    48  }
    49  
    50  // singleConnPool is a special case for a single connection.
    51  type singleConnPool struct {
    52  	*grpc.ClientConn
    53  }
    54  
    55  func (p *singleConnPool) Connection() *grpc.ClientConn { return p.ClientConn }
    56  func (p *singleConnPool) Len() int                     { return 1 }
    57  func (p *singleConnPool) private()                     {}
    58  
    59  type roundRobinConnPool struct {
    60  	conns []*grpc.ClientConn
    61  
    62  	idx uint32 // access via sync/atomic
    63  }
    64  
    65  func (p *roundRobinConnPool) Len() int {
    66  	return len(p.conns)
    67  }
    68  
    69  func (p *roundRobinConnPool) Connection() *grpc.ClientConn {
    70  	i := atomic.AddUint32(&p.idx, 1)
    71  	return p.conns[i%uint32(len(p.conns))]
    72  }
    73  
    74  func (p *roundRobinConnPool) Close() error {
    75  	var errs multiError
    76  	for _, conn := range p.conns {
    77  		if err := conn.Close(); err != nil {
    78  			errs = append(errs, err)
    79  		}
    80  	}
    81  	if len(errs) == 0 {
    82  		return nil
    83  	}
    84  	return errs
    85  }
    86  
    87  func (p *roundRobinConnPool) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error {
    88  	return p.Connection().Invoke(ctx, method, args, reply, opts...)
    89  }
    90  
    91  func (p *roundRobinConnPool) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
    92  	return p.Connection().NewStream(ctx, desc, method, opts...)
    93  }
    94  
    95  func (p *roundRobinConnPool) private() {}
    96  
    97  // multiError represents errors from multiple conns in the group.
    98  type multiError []error
    99  
   100  func (m multiError) Error() string {
   101  	s, n := "", 0
   102  	for _, e := range m {
   103  		if e != nil {
   104  			if n == 0 {
   105  				s = e.Error()
   106  			}
   107  			n++
   108  		}
   109  	}
   110  	switch n {
   111  	case 0:
   112  		return "(0 errors)"
   113  	case 1:
   114  		return s
   115  	case 2:
   116  		return s + " (and 1 other error)"
   117  	}
   118  	return fmt.Sprintf("%s (and %d other errors)", s, n-1)
   119  }
   120  

View as plain text