tchannel

package
Version: v1.57.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2021 License: MIT Imports: 32 Imported by: 30

Documentation

Overview

Package tchannel implements a YARPC transport based on the TChannel protocol. The TChannel transport provides support for Unary RPCs only.

Usage

To use TChannel with YARPC load balancers, use the NewTransport constructor. To use TChannel with a channel shared with other systems like Ringpop, use NewChannelTransport.

You can let YARPC own and manage the TChannel Channel for you by providing the service name. Note that this is the name of the local service, not the name of the service you will be sending requests to.

tchannelTransport, err := tchannel.NewTransport(tchannel.ServiceName("myservice"))

Alternatively, and only when necessary to share an underlying channel, ChannelTransport must be constructed to use this transport. You can provide an existing TChannel Channel to construct the ChannelTransport. In this configuration, the transport cannot use YARPC load balancers.

ch := getTChannelChannel()
tchannelTransport, err := tchannel.NewChannelTransport(tchannel.WithChannel(ch))

To serve a YARPC application over TChannel, pass a TChannel inbound in your yarpc.Config.

myInbound := tchannelTransport.NewInbound()
dispatcher := yarpc.NewDispatcher(yarpc.Config{
	Name: "myservice",
	Inbounds: yarpc.Inbounds{myInbound},
})

To make requests to a YARPC application that supports TChannel, pass a TChannel outbound in your yarpc.Config.

myserviceOutbound := tchannelTransport.NewOutbound()
dispatcher := yarpc.NewDispatcher(yarpc.Config{
	Name: "myservice",
	Outbounds: yarpc.Outbounds{
		"outboundservice": {Unary: myserviceOutbound},
	},
})

Configuration

A TChannel transport may be configured using YARPC's configuration system. See TransportConfig, InboundConfig, and OutboundConfig for details on the different configuration parameters supported by this transport.

Index

Examples

Constants

View Source
const (
	// ErrorCodeHeaderKey is the response header key for the error code.
	ErrorCodeHeaderKey = "$rpc$-error-code"
	// ErrorNameHeaderKey is the response header key for the error name.
	ErrorNameHeaderKey = "$rpc$-error-name"
	// ErrorMessageHeaderKey is the response header key for the error message.
	ErrorMessageHeaderKey = "$rpc$-error-message"
	// ServiceHeaderKey is the response header key for the respond service
	ServiceHeaderKey = "$rpc$-service"
	// ApplicationErrorNameHeaderKey is the response header key for the application error name.
	ApplicationErrorNameHeaderKey = "$rpc$-application-error-name"
	// ApplicationErrorDetailsHeaderKey is the response header key for the
	// application error details string.
	ApplicationErrorDetailsHeaderKey = "$rpc$-application-error-details"
	// ApplicationErrorCodeHeaderKey is the response header key for the application error code.
	ApplicationErrorCodeHeaderKey = "$rpc$-application-error-code"
	// CallerProcedureHeader is the header key for the procedure of the caller making the request.
	CallerProcedureHeader = "$rpc$-caller-procedure"
)
View Source
const (
	// TransportName is the name of the transport.
	//
	// This value is what is used as transport.Request#Transport and transport.Namer
	// for Outbounds.
	TransportName = "tchannel"
)

Variables

This section is empty.

Functions

func TransportSpec added in v1.9.0

func TransportSpec(opts ...Option) yarpcconfig.TransportSpec

TransportSpec returns a TransportSpec for the TChannel unary transport.

Types

type Channel added in v0.5.0

type Channel interface {
	BeginCall(
		ctx context.Context,
		hostPort, serviceName, methodName string,
		callOptions *tchannel.CallOptions,
	) (*tchannel.OutboundCall, error)
	Close()
	GetSubChannel(serviceName string, opts ...tchannel.SubChannelOption) *tchannel.SubChannel
	ListenAndServe(hostPort string) error
	PeerInfo() tchannel.LocalPeerInfo
	RootPeers() *tchannel.RootPeerList
	ServiceName() string
	State() tchannel.ChannelState
}

Channel is the interface exposed by TChannel. The TChannel transport for YARPC is built on top of this interface.

See https://godoc.org/github.com/uber/tchannel-go#Channel for more information about these methods.

type ChannelInbound added in v1.0.0

type ChannelInbound struct {
	// contains filtered or unexported fields
}

ChannelInbound receives YARPC requests over TChannel. It may be constructed using the NewInbound method on ChannelTransport. If you have a YARPC peer.Chooser, use the unqualified tchannel.Transport instead (instead of the tchannel.ChannelTransport).

Example
package main

import (
	"log"

	"go.uber.org/yarpc"
	"go.uber.org/yarpc/transport/tchannel"
)

func main() {
	transport, err := tchannel.NewChannelTransport(tchannel.ServiceName("myservice"))
	if err != nil {
		log.Fatal(err)
	}

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name:     "myservice",
		Inbounds: yarpc.Inbounds{transport.NewInbound()},
	})

	if err := dispatcher.Start(); err != nil {
		log.Fatal(err)
	}
	defer dispatcher.Stop()
}
Output:

func (*ChannelInbound) Channel added in v1.0.0

func (i *ChannelInbound) Channel() Channel

Channel returns the underlying Channel for this Inbound.

func (*ChannelInbound) Introspect added in v1.0.0

func (i *ChannelInbound) Introspect() introspection.InboundStatus

Introspect returns the state of the inbound for introspection purposes.

func (*ChannelInbound) IsRunning added in v1.0.0

func (i *ChannelInbound) IsRunning() bool

IsRunning returns whether the ChannelInbound is running.

func (*ChannelInbound) SetRouter added in v1.0.0

func (i *ChannelInbound) SetRouter(router transport.Router)

SetRouter configures a router to handle incoming requests. This satisfies the transport.Inbound interface, and would be called by a dispatcher when it starts.

func (*ChannelInbound) Start added in v1.0.0

func (i *ChannelInbound) Start() error

Start starts this Inbound. Note that this does not start listening for connections; that occurs when you start the underlying ChannelTransport is started.

func (*ChannelInbound) Stop added in v1.0.0

func (i *ChannelInbound) Stop() error

Stop stops the TChannel outbound. This currently does nothing.

func (*ChannelInbound) Transports added in v1.0.0

func (i *ChannelInbound) Transports() []transport.Transport

Transports returns a slice containing the ChannelInbound's underlying ChannelTransport.

type ChannelOutbound added in v1.0.0

type ChannelOutbound struct {
	// contains filtered or unexported fields
}

ChannelOutbound sends YARPC requests over TChannel. It may be constructed using the NewOutbound or NewSingleOutbound methods on the tchannel.ChannelTransport. If you have a YARPC peer.Chooser, use the unqualified tchannel.Transport instead (instead of the tchannel.ChannelTransport).

Example
package main

import (
	"log"

	"go.uber.org/yarpc"
	"go.uber.org/yarpc/transport/tchannel"
)

func main() {
	transport, err := tchannel.NewChannelTransport(tchannel.ServiceName("myclient"))
	if err != nil {
		log.Fatal(err)
	}

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name: "myclient",
		Outbounds: yarpc.Outbounds{
			"myservice": {Unary: transport.NewOutbound()},
		},
	})

	if err := dispatcher.Start(); err != nil {
		log.Fatal(err)
	}
	defer dispatcher.Stop()
}
Output:

Example (Single)
package main

import (
	"log"

	"go.uber.org/yarpc"
	"go.uber.org/yarpc/transport/tchannel"
)

func main() {
	transport, err := tchannel.NewChannelTransport(tchannel.ServiceName("myclient"))
	if err != nil {
		log.Fatal(err)
	}

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name: "myclient",
		Outbounds: yarpc.Outbounds{
			"myservice": {Unary: transport.NewSingleOutbound("127.0.0.0:4040")},
		},
	})

	if err := dispatcher.Start(); err != nil {
		log.Fatal(err)
	}
	defer dispatcher.Stop()
}
Output:

func (*ChannelOutbound) Call added in v1.0.0

Call sends an RPC over this TChannel outbound.

func (*ChannelOutbound) Introspect added in v1.0.0

Introspect returns basic status about this outbound.

func (*ChannelOutbound) IsRunning added in v1.0.0

func (o *ChannelOutbound) IsRunning() bool

IsRunning returns whether the ChannelOutbound is running.

func (*ChannelOutbound) Start added in v1.0.0

func (o *ChannelOutbound) Start() error

Start starts the TChannel outbound.

func (*ChannelOutbound) Stop added in v1.0.0

func (o *ChannelOutbound) Stop() error

Stop stops the TChannel outbound.

func (*ChannelOutbound) Transports added in v1.0.0

func (o *ChannelOutbound) Transports() []transport.Transport

Transports returns the underlying TChannel Transport for this outbound.

type ChannelTransport added in v1.0.0

type ChannelTransport struct {
	// contains filtered or unexported fields
}

ChannelTransport maintains TChannel peers and creates inbounds and outbounds for TChannel. If you have a YARPC peer.Chooser, use the unqualified tchannel.Transport instead.

func NewChannelTransport added in v1.0.0

func NewChannelTransport(opts ...TransportOption) (*ChannelTransport, error)

NewChannelTransport is a YARPC transport that facilitates sending and receiving YARPC requests through TChannel. It uses a shared TChannel Channel for both, incoming and outgoing requests, ensuring reuse of connections and other resources.

Either the local service name (with the ServiceName option) or a user-owned TChannel (with the WithChannel option) MUST be specified.

ChannelTransport uses the underlying TChannel Channel for load balancing and peer managament. Use NewTransport and its NewOutbound to support YARPC peer.Choosers.

func (*ChannelTransport) Channel added in v1.0.0

func (t *ChannelTransport) Channel() Channel

Channel returns the underlying TChannel "Channel" instance.

func (*ChannelTransport) IsRunning added in v1.0.0

func (t *ChannelTransport) IsRunning() bool

IsRunning returns whether the ChannelTransport is running.

func (*ChannelTransport) ListenAddr added in v1.0.0

func (t *ChannelTransport) ListenAddr() string

ListenAddr exposes the listen address of the transport.

func (*ChannelTransport) NewInbound added in v1.0.0

func (t *ChannelTransport) NewInbound() *ChannelInbound

NewInbound returns a new TChannel inbound backed by a shared TChannel transport. The returned ChannelInbound does not support peer.Chooser and uses TChannel's own internal load balancing peer selection. If you have a YARPC peer.Chooser, use the unqualified tchannel.NewInbound instead. There should only be one inbound for TChannel since all outbounds send the listening port over non-ephemeral connections so a service can deduplicate locally- and remotely-initiated persistent connections.

func (*ChannelTransport) NewOutbound added in v1.0.0

func (t *ChannelTransport) NewOutbound() *ChannelOutbound

NewOutbound builds a new TChannel outbound using the transport's shared channel to make requests to any connected peer.

func (*ChannelTransport) NewSingleOutbound added in v1.0.0

func (t *ChannelTransport) NewSingleOutbound(addr string) *ChannelOutbound

NewSingleOutbound builds a new TChannel outbound using the transport's shared channel to a specific peer.

func (*ChannelTransport) Start added in v1.0.0

func (t *ChannelTransport) Start() error

Start starts the TChannel transport. This starts making connections and accepting inbound requests. All inbounds must have been assigned a router to accept inbound requests before this is called.

func (*ChannelTransport) Stop added in v1.0.0

func (t *ChannelTransport) Stop() error

Stop stops the TChannel transport. It starts rejecting incoming requests and draining connections before closing them. In a future version of YARPC, Stop will block until the underlying channel has closed completely.

type Inbound

type Inbound struct {
	// contains filtered or unexported fields
}

Inbound receives YARPC requests over TChannel. It may be constructed using the NewInbound method on a tchannel.Transport.

Example
package main

import (
	"log"

	"go.uber.org/yarpc"
	"go.uber.org/yarpc/transport/tchannel"
)

func main() {
	transport, err := tchannel.NewTransport(tchannel.ServiceName("myservice"))
	if err != nil {
		log.Fatal(err)
	}

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name:     "myservice",
		Inbounds: yarpc.Inbounds{transport.NewInbound()},
	})

	if err := dispatcher.Start(); err != nil {
		log.Fatal(err)
	}
	defer dispatcher.Stop()
}
Output:

func (*Inbound) Introspect added in v1.4.0

func (i *Inbound) Introspect() introspection.InboundStatus

Introspect returns the state of the inbound for introspection purposes.

func (*Inbound) IsRunning added in v1.4.0

func (i *Inbound) IsRunning() bool

IsRunning returns whether the Inbound is running.

func (*Inbound) SetRouter added in v1.4.0

func (i *Inbound) SetRouter(router transport.Router)

SetRouter configures a router to handle incoming requests. This satisfies the transport.Inbound interface, and would be called by a dispatcher when it starts.

func (*Inbound) Start added in v1.4.0

func (i *Inbound) Start() error

Start starts this Inbound. Note that this does not start listening for connections; that occurs when you start the underlying ChannelTransport is started.

func (*Inbound) Stop added in v1.4.0

func (i *Inbound) Stop() error

Stop stops the TChannel outbound. This currently does nothing.

func (*Inbound) Transports added in v1.4.0

func (i *Inbound) Transports() []transport.Transport

Transports returns a slice containing the Inbound's underlying Transport.

type InboundConfig added in v1.9.0

type InboundConfig struct {
	// Address to listen on. Defaults to ":0" (all network interfaces and a
	// random OS-assigned port).
	Address string `config:"address,interpolate"`
}

InboundConfig configures a TChannel inbound.

inbounds:
  tchannel:
    address: :4040

At most one TChannel inbound may be defined in a single YARPC service.

type Option added in v1.9.0

type Option interface {
	// contains filtered or unexported methods
}

Option allows customizing the YARPC TChannel transport. TransportSpec() accepts any TransportOption, and may in the future also accept inbound and outbound options.

type Outbound added in v1.4.0

type Outbound struct {
	// contains filtered or unexported fields
}

Outbound sends YARPC requests over TChannel. It may be constructed using the NewOutbound or NewSingleOutbound methods on the TChannel Transport.

Example
package main

import (
	"log"

	"go.uber.org/yarpc"
	"go.uber.org/yarpc/transport/tchannel"
)

func main() {
	transport, err := tchannel.NewTransport(tchannel.ServiceName("myclient"))
	if err != nil {
		log.Fatal(err)
	}

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name: "myclient",
		Outbounds: yarpc.Outbounds{
			"myservice": {Unary: transport.NewSingleOutbound("127.0.0.0:4040")},
		},
	})

	if err := dispatcher.Start(); err != nil {
		log.Fatal(err)
	}
	defer dispatcher.Stop()
}
Output:

func (*Outbound) Call added in v1.4.0

Call sends an RPC over this TChannel outbound.

func (*Outbound) Chooser added in v1.9.0

func (o *Outbound) Chooser() peer.Chooser

Chooser returns the outbound's peer chooser.

func (*Outbound) Introspect added in v1.4.0

func (o *Outbound) Introspect() introspection.OutboundStatus

Introspect returns basic status about this outbound.

func (*Outbound) IsRunning added in v1.4.0

func (o *Outbound) IsRunning() bool

IsRunning returns whether the ChannelOutbound is running.

func (*Outbound) Start added in v1.4.0

func (o *Outbound) Start() error

Start starts the TChannel outbound.

func (*Outbound) Stop added in v1.4.0

func (o *Outbound) Stop() error

Stop stops the TChannel outbound.

func (*Outbound) TransportName added in v1.43.0

func (o *Outbound) TransportName() string

TransportName is the transport name that will be set on `transport.Request` struct.

func (*Outbound) Transports added in v1.4.0

func (o *Outbound) Transports() []transport.Transport

Transports returns the underlying TChannel Transport for this outbound.

type OutboundConfig added in v1.9.0

type OutboundConfig struct {
	yarpcconfig.PeerChooser
}

OutboundConfig configures a TChannel outbound.

outbounds:
  myservice:
    tchannel:
      peer: 127.0.0.1:4040

type ResponseErrorMeta added in v1.47.0

type ResponseErrorMeta struct {
	Code tchannel.SystemErrCode
}

ResponseErrorMeta exposes TChannel specific information from an error.

This API is experimental and subject to change or break at anytime.

func GetResponseErrorMeta added in v1.47.0

func GetResponseErrorMeta(err error) *ResponseErrorMeta

GetResponseErrorMeta extracts the TChannel specific error response information from an error. Returns nil if no information is available.

This API is experimental and subject to change or break at anytime.

type Transport added in v1.4.0

type Transport struct {
	// contains filtered or unexported fields
}

Transport is a TChannel transport suitable for use with YARPC's peer selection system. The transport implements peer.Transport so multiple peer.List implementations can retain and release shared peers. The transport implements transport.Transport so it is suitable for lifecycle management.

func NewTransport added in v1.4.0

func NewTransport(opts ...TransportOption) (*Transport, error)

NewTransport is a YARPC transport that facilitates sending and receiving YARPC requests through TChannel. It uses a shared TChannel Channel for both, incoming and outgoing requests, ensuring reuse of connections and other resources.

Either the local service name (with the ServiceName option) or a user-owned TChannel (with the WithChannel option) MUST be specified.

func (*Transport) IsRunning added in v1.4.0

func (t *Transport) IsRunning() bool

IsRunning returns whether the TChannel transport is running.

func (*Transport) ListenAddr added in v1.4.0

func (t *Transport) ListenAddr() string

ListenAddr exposes the listen address of the transport.

func (*Transport) NewInbound added in v1.4.0

func (t *Transport) NewInbound() *Inbound

NewInbound returns a new TChannel inbound backed by a shared TChannel transport. There should only be one inbound for TChannel since all outbounds send the listening port over non-ephemeral connections so a service can deduplicate locally- and remotely-initiated persistent connections.

func (*Transport) NewOutbound added in v1.4.0

func (t *Transport) NewOutbound(chooser peer.Chooser) *Outbound

NewOutbound builds a new TChannel outbound that selects a peer for each request using the given peer chooser.

func (*Transport) NewSingleOutbound added in v1.4.0

func (t *Transport) NewSingleOutbound(addr string) *Outbound

NewSingleOutbound builds a new TChannel outbound always using the peer with the given address.

func (*Transport) ReleasePeer added in v1.4.0

func (t *Transport) ReleasePeer(pid peer.Identifier, sub peer.Subscriber) error

ReleasePeer releases a peer from the peer.Subscriber and removes that peer from the Transport if nothing is listening to it.

func (*Transport) RetainPeer added in v1.4.0

func (t *Transport) RetainPeer(pid peer.Identifier, sub peer.Subscriber) (peer.Peer, error)

RetainPeer adds a peer subscriber (typically a peer chooser) and causes the transport to maintain persistent connections with that peer.

func (*Transport) Start added in v1.4.0

func (t *Transport) Start() error

Start starts the TChannel transport. This starts making connections and accepting inbound requests. All inbounds must have been assigned a router to accept inbound requests before this is called.

func (*Transport) Stop added in v1.4.0

func (t *Transport) Stop() error

Stop stops the TChannel transport. It starts rejecting incoming requests and draining connections before closing them. In a future version of YARPC, Stop will block until the underlying channel has closed completely.

type TransportConfig added in v1.9.0

type TransportConfig struct {
	ConnTimeout time.Duration       `config:"connTimeout"`
	ConnBackoff yarpcconfig.Backoff `config:"connBackoff"`
}

TransportConfig configures a shared TChannel transport. This is shared between all TChannel outbounds and inbounds of a Dispatcher.

transports:
  tchannel:
    connTimeout: 500ms
    connBackoff:
      exponential:
        first: 10ms
        max: 30s

type TransportOption added in v1.0.0

type TransportOption func(*transportOptions)

TransportOption customizes the behavior of a TChannel Transport.

func ConnBackoff added in v1.10.0

func ConnBackoff(s backoffapi.Strategy) TransportOption

ConnBackoff specifies the connection backoff strategy for delays between connection attempts for each peer.

ConnBackoff accepts a function that creates new backoff instances. The transport uses this to make referentially independent backoff instances that will not be shared across goroutines.

The backoff instance is a function that accepts connection attempts and returns a duration.

The default is exponential backoff starting with 10ms fully jittered, doubling each attempt, with a maximum interval of 30s.

func ConnTimeout added in v1.10.0

func ConnTimeout(d time.Duration) TransportOption

ConnTimeout specifies the time that TChannel will wait for a connection attempt to any retained peer.

The default is half of a second.

func Dialer added in v1.44.0

func Dialer(dialer func(ctx context.Context, network, hostPort string) (net.Conn, error)) TransportOption

Dialer sets a dialer function for outbound calls.

The function signature matches the net.Dialer DialContext method. https://golang.org/pkg/net/#Dialer.DialContext

func ListenAddr

func ListenAddr(addr string) TransportOption

ListenAddr specifies the port the TChannel should listen on. This defaults to ":0" (all interfaces, OS-assigned port).

transport := NewChannelTransport(ServiceName("myservice"), ListenAddr(":4040"))

This option has no effect if WithChannel was used and the TChannel was already listening, and it is disallowed for transports constructed with the YARPC configuration system.

func Listener added in v1.27.0

func Listener(l net.Listener) TransportOption

Listener sets a net.Listener to use for the channel. This only applies to NewTransport (will not work with NewChannelTransport).

The default is to depend on the ListenAddr (no-op).

func Logger added in v1.21.0

func Logger(logger *zap.Logger) TransportOption

Logger sets a logger to use for internal logging.

The default is to not write any logs.

func OriginalHeaders added in v1.28.0

func OriginalHeaders() TransportOption

OriginalHeaders specifies whether to forward headers without canonicalizing them

func ServiceName added in v1.0.0

func ServiceName(name string) TransportOption

ServiceName informs the NewChannelTransport constructor which service name to use if it needs to construct a root Channel object, as when called without the WithChannel option.

ServiceName specifies the name of the current service for the YARPC-owned TChannel Channel. If the WithChannel option is not specified, the TChannel Transport will build its own TChannel Chanel and use this name for that Channel.

This option has no effect if WithChannel was used, and it is disallowed for transports constructed with the YARPC configuration system.

func Tracer added in v1.0.0

func Tracer(tracer opentracing.Tracer) TransportOption

Tracer specifies the request tracer used for RPCs passing through the TChannel transport.

func WithChannel added in v1.0.0

func WithChannel(ch Channel) TransportOption

WithChannel specifies the TChannel Channel to use to send and receive YARPC requests. The instance may already have handlers registered against it; these will be left unchanged.

If this option is not passed, the Transport will build and manage its own Channel. The behavior of that TChannel may be customized using the ListenAddr and ServiceName options.

This option is disallowed for NewTransport and transports constructed with the YARPC configuration system.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL