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