go.uber.org/yarpc@v1.72.1/transport/tchannel/channel_inbound.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  	"go.uber.org/yarpc/api/transport"
    25  	"go.uber.org/yarpc/api/x/introspection"
    26  	"go.uber.org/yarpc/pkg/lifecycle"
    27  	"go.uber.org/zap"
    28  )
    29  
    30  // ChannelInbound receives YARPC requests over TChannel.
    31  // It may be constructed using the NewInbound method on ChannelTransport.
    32  // If you have a YARPC peer.Chooser, use the unqualified tchannel.Transport
    33  // instead (instead of the tchannel.ChannelTransport).
    34  type ChannelInbound struct {
    35  	transport *ChannelTransport
    36  
    37  	once *lifecycle.Once
    38  }
    39  
    40  // NewInbound returns a new TChannel inbound backed by a shared TChannel
    41  // transport.  The returned ChannelInbound does not support peer.Chooser
    42  // and uses TChannel's own internal load balancing peer selection.
    43  // If you have a YARPC peer.Chooser, use the unqualified tchannel.NewInbound
    44  // instead.
    45  // There should only be one inbound for TChannel since all outbounds send the
    46  // listening port over non-ephemeral connections so a service can deduplicate
    47  // locally- and remotely-initiated persistent connections.
    48  func (t *ChannelTransport) NewInbound() *ChannelInbound {
    49  	return &ChannelInbound{
    50  		once:      lifecycle.NewOnce(),
    51  		transport: t,
    52  	}
    53  }
    54  
    55  // SetRouter configures a router to handle incoming requests.
    56  // This satisfies the transport.Inbound interface, and would be called
    57  // by a dispatcher when it starts.
    58  func (i *ChannelInbound) SetRouter(router transport.Router) {
    59  	i.transport.router = router
    60  }
    61  
    62  // Transports returns a slice containing the ChannelInbound's underlying
    63  // ChannelTransport.
    64  func (i *ChannelInbound) Transports() []transport.Transport {
    65  	return []transport.Transport{i.transport}
    66  }
    67  
    68  // Channel returns the underlying Channel for this Inbound.
    69  func (i *ChannelInbound) Channel() Channel {
    70  	return i.transport.ch
    71  }
    72  
    73  // Start starts this Inbound. Note that this does not start listening for
    74  // connections; that occurs when you start the underlying ChannelTransport is
    75  // started.
    76  func (i *ChannelInbound) Start() error {
    77  	return i.once.Start(func() error {
    78  		i.transport.logger.Info("started TChannel inbound", zap.String("address", i.transport.ListenAddr()))
    79  		if i.transport.router == nil || len(i.transport.router.Procedures()) == 0 {
    80  			i.transport.logger.Warn("no procedures specified for tchannel inbound")
    81  		}
    82  		return nil
    83  	})
    84  }
    85  
    86  // Stop stops the TChannel outbound. This currently does nothing.
    87  func (i *ChannelInbound) Stop() error {
    88  	return i.once.Stop(nil)
    89  }
    90  
    91  // IsRunning returns whether the ChannelInbound is running.
    92  func (i *ChannelInbound) IsRunning() bool {
    93  	return i.once.IsRunning()
    94  }
    95  
    96  // Introspect returns the state of the inbound for introspection purposes.
    97  func (i *ChannelInbound) Introspect() introspection.InboundStatus {
    98  	c := i.transport.Channel()
    99  	stateString := ""
   100  	if c != nil {
   101  		stateString = c.State().String()
   102  	}
   103  	return introspection.InboundStatus{
   104  		Transport: "tchannel",
   105  		Endpoint:  i.transport.ListenAddr(),
   106  		State:     stateString,
   107  	}
   108  }