go.uber.org/yarpc@v1.72.1/transport/grpc/peer.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package grpc
    22  
    23  import (
    24  	"context"
    25  
    26  	"go.uber.org/yarpc/api/peer"
    27  	"go.uber.org/yarpc/peer/abstractpeer"
    28  	"go.uber.org/zap"
    29  	"google.golang.org/grpc"
    30  	"google.golang.org/grpc/connectivity"
    31  )
    32  
    33  type grpcPeer struct {
    34  	*abstractpeer.Peer
    35  
    36  	t          *Transport
    37  	ctx        context.Context
    38  	cancel     context.CancelFunc
    39  	clientConn *grpc.ClientConn
    40  	stoppedC   chan struct{}
    41  }
    42  
    43  func (t *Transport) newPeer(address string, options *dialOptions) (*grpcPeer, error) {
    44  	dialOptions := append([]grpc.DialOption{
    45  		grpc.WithUserAgent(UserAgent),
    46  		grpc.WithDefaultCallOptions(
    47  			grpc.CallCustomCodec(customCodec{}),
    48  			grpc.MaxCallRecvMsgSize(t.options.clientMaxRecvMsgSize),
    49  			grpc.MaxCallSendMsgSize(t.options.clientMaxSendMsgSize),
    50  		),
    51  	}, options.grpcOptions(t)...)
    52  
    53  	if t.options.clientMaxHeaderListSize != nil {
    54  		dialOptions = append(dialOptions, grpc.WithMaxHeaderListSize(*t.options.clientMaxHeaderListSize))
    55  	}
    56  
    57  	clientConn, err := grpc.Dial(address, dialOptions...)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	ctx, cancel := context.WithCancel(context.Background())
    63  
    64  	grpcPeer := &grpcPeer{
    65  		Peer:       abstractpeer.NewPeer(abstractpeer.PeerIdentifier(address), t),
    66  		t:          t,
    67  		ctx:        ctx,
    68  		cancel:     cancel,
    69  		clientConn: clientConn,
    70  		stoppedC:   make(chan struct{}),
    71  	}
    72  
    73  	go grpcPeer.monitorConnectionStatus()
    74  
    75  	return grpcPeer, nil
    76  }
    77  
    78  func (p *grpcPeer) monitorConnectionStatus() {
    79  	p.setConnectionStatus(peer.Unavailable)
    80  	var grpcStatus connectivity.State
    81  	for {
    82  		grpcStatus = p.clientConn.GetState()
    83  		yarpcStatus := grpcStatusToYARPCStatus(grpcStatus)
    84  		p.setConnectionStatus(yarpcStatus)
    85  
    86  		if !p.clientConn.WaitForStateChange(p.ctx, grpcStatus) {
    87  			break
    88  		}
    89  	}
    90  	p.setConnectionStatus(peer.Unavailable)
    91  
    92  	// Close always returns an error.
    93  	_ = p.clientConn.Close()
    94  	close(p.stoppedC)
    95  }
    96  
    97  func (p *grpcPeer) setConnectionStatus(status peer.ConnectionStatus) {
    98  	p.t.options.logger.Debug(
    99  		"peer status change",
   100  		zap.String("status", status.String()),
   101  		zap.String("peer", p.Peer.Identifier()),
   102  		zap.String("transport", "grpc"),
   103  	)
   104  	p.Peer.SetStatus(status)
   105  	p.Peer.NotifyStatusChanged()
   106  }
   107  
   108  // StartRequest and EndRequest are no-ops now.
   109  // They previously aggregated pending request count from all subscibed peer
   110  // lists and distributed change notifications.
   111  // This was fraught with concurrency hazards so we moved pending request count
   112  // tracking into the lists themselves.
   113  
   114  func (p *grpcPeer) StartRequest() {}
   115  
   116  func (p *grpcPeer) EndRequest() {}
   117  
   118  func (p *grpcPeer) stop() {
   119  	p.cancel()
   120  }
   121  
   122  func (p *grpcPeer) wait() {
   123  	<-p.stoppedC
   124  }
   125  
   126  func grpcStatusToYARPCStatus(grpcStatus connectivity.State) peer.ConnectionStatus {
   127  	switch grpcStatus {
   128  	case connectivity.Ready:
   129  		return peer.Available
   130  	case connectivity.Connecting:
   131  		return peer.Connecting
   132  	default:
   133  		return peer.Unavailable
   134  	}
   135  }