go.uber.org/yarpc@v1.72.1/transport/tchannel/outbound_channel.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 tchannel
    22  
    23  import (
    24  	"net"
    25  
    26  	"github.com/uber/tchannel-go"
    27  	"go.uber.org/yarpc/api/peer"
    28  	"golang.org/x/net/context"
    29  )
    30  
    31  var _ peer.Transport = (*outboundChannel)(nil)
    32  
    33  type dialerFunc = func(ctx context.Context, network, hostPort string) (net.Conn, error)
    34  
    35  // outboundChannel holds a TChannel channel for outbound use only with custom
    36  // dialer. This is used for creating TLS connections with different TLS config
    37  // per outbound.
    38  type outboundChannel struct {
    39  	t      *Transport
    40  	dialer dialerFunc
    41  	ch     *tchannel.Channel
    42  }
    43  
    44  // newOutboundChannel returns outbound channel with given transport and dialer.
    45  func newOutboundChannel(t *Transport, dialer dialerFunc) *outboundChannel {
    46  	return &outboundChannel{
    47  		t:      t,
    48  		dialer: dialer,
    49  	}
    50  }
    51  
    52  // RetainPeer delegates to the transport RetainPeer along with its channel.
    53  func (o *outboundChannel) RetainPeer(pid peer.Identifier, sub peer.Subscriber) (peer.Peer, error) {
    54  	return o.t.retainPeer(pid, sub, o.ch)
    55  }
    56  
    57  // ReleasePeer delegates to the transport ReleasePeer.
    58  func (o *outboundChannel) ReleasePeer(pid peer.Identifier, sub peer.Subscriber) error {
    59  	return o.t.ReleasePeer(pid, sub)
    60  }
    61  
    62  // start creates channel used for managing outbound peers.
    63  // This is invoked by the transport when it is started.
    64  func (o *outboundChannel) start() (err error) {
    65  	o.ch, err = tchannel.NewChannel(o.t.name, &tchannel.ChannelOptions{
    66  		Dialer:              o.dialer,
    67  		OnPeerStatusChanged: o.t.onPeerStatusChanged,
    68  		Tracer:              o.t.tracer,
    69  	})
    70  	return err
    71  }
    72  
    73  // stop closes the outbound channel stopping outbound connections.
    74  // This is invoked by the transport when it is stopping.
    75  func (o *outboundChannel) stop() {
    76  	o.ch.Close()
    77  }