pb

package
v0.0.0-...-c687ad2 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 10 Imported by: 3

Documentation

Overview

Package thrift adds support to use Thrift services over TChannel.

To start listening to a Thrift service using TChannel, create the channel, and register the service using:

server := thrift.NewServer(tchan)
server.Register(gen.NewTChan[SERVICE]Server(handler)

// Any number of services can be registered on the same Thrift server.
server.Register(gen.NewTChan[SERVICE2]Server(handler)

To use a Thrift client use the generated TChan client:

thriftClient := thrift.NewClient(ch, "hyperbahnService", nil)
client := gen.NewTChan[SERVICE]Client(thriftClient)

// Any number of service clients can be made using the same Thrift client.
client2 := gen.NewTChan[SERVICE2]Client(thriftClient)

This client can be used similar to a standard Thrift client, except a Context is passed with options (such as timeout).

TODO(prashant): Add and document header support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadHeaders

func ReadHeaders(r io.Reader) (map[string]string, error)

ReadHeaders reads key-value pairs encoded using WriteHeaders.

func ReadStruct

func ReadStruct(r tchannel.ArgReader, s PbStruct) error

ReadStruct reads the given Thrift struct. 目前没有通过buf来做缓存 可能会出现half read

func WriteHeaders

func WriteHeaders(w io.Writer, headers map[string]string) error

WriteHeaders writes the given key-value pairs using the following encoding: len~2 (k~4 v~4)~len

func WriteStruct

func WriteStruct(w tchannel.ArgWriter, s PbStruct) error

直接通过pb做序列化 WriteStruct writes the given pb struct to a writer.

Types

type ClientOptions

type ClientOptions struct {
	// HostPort specifies a specific server to hit.
	HostPort string
}

ClientOptions are options to customize the client.

type Context

type Context tchannel.ContextWithHeaders

Context is a Thrift Context which contains request and response headers.

func NewContext

func NewContext(timeout time.Duration) (Context, context.CancelFunc)

NewContext returns a Context that can be used to make Thrift calls.

func WithHeaders

func WithHeaders(ctx context.Context, headers map[string]string) Context

WithHeaders returns a Context that can be used to make a call with request headers.

func Wrap

func Wrap(ctx context.Context) Context

Wrap returns a Thrift Context that wraps around a Context.

type PbStruct

type PbStruct interface {
	XXX_Unmarshal(b []byte) error
	XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
}

要求interface可以序列化和反序列化

type PostResponseCB

type PostResponseCB func(ctx context.Context, method string, response PbStruct)

PostResponseCB registers a callback that is run after a response has been compeltely processed (e.g. written to the channel). This gives the server a chance to clean up resources from the response object

type RegisterOption

type RegisterOption interface {
	Apply(h *handler)
}

RegisterOption is the interface for options to Register.

func OptPostResponse

func OptPostResponse(cb PostResponseCB) RegisterOption

OptPostResponse registers a PostResponseCB.

type Server

type Server struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Server handles incoming TChannel calls and forwards them to the matching TChanServer.

func NewServer

func NewServer(registrar tchannel.Registrar) *Server

NewServer returns a server that can serve thrift services over TChannel.

func (*Server) Handle

func (s *Server) Handle(ctx context.Context, call *tchannel.InboundCall)

Handle handles an incoming TChannel call and forwards it to the correct handler.

func (*Server) Register

func (s *Server) Register(svr TChanServer, opts ...RegisterOption)

Register registers the given TChanServer to be called on any incoming call for its' services. TODO(prashant): Replace Register call with this call.

func (*Server) SetContextFn

func (s *Server) SetContextFn(f func(ctx context.Context, method string, headers map[string]string) Context)

SetContextFn sets the function used to convert a context.Context to a thrift.Context. Note: This API may change and is only intended to bridge different contexts.

type TChanClient

type TChanClient interface {
	// Call should be passed the method to call and the request/response Thrift structs.
	Call(ctx Context, serviceName, methodName string, req, resp PbStruct) (success bool, err error)
	CallStreaming(ctx Context, pbService, methodName string) (success bool, err error, reader tchannel.ArgReader, writer tchannel.ArgWriter)
}

TChanClient abstracts calling a Thrift endpoint, and is used by the generated client code.

func NewClient

func NewClient(ch *tchannel.Channel, serviceName string, opts *ClientOptions) TChanClient

NewClient returns a Client that makes calls over the given tchannel to the given Hyperbahn service.

type TChanServer

type TChanServer interface {
	// Handle should read the request from the given reqReader, and return the response struct.
	// The arguments returned are success, result struct, unexpected error
	Handle(ctx Context, methodName string, reader tchannel.ArgReader, writer tchannel.ArgWriter) (success bool, resp PbStruct, err error)

	// Service returns the service name.
	Service() string

	// Methods returns the method names handled by this server.
	Methods() []string

	Types() []bool
}

TChanServer abstracts handling of an RPC that is implemented by the generated server code.

Directories

Path Synopsis
Package arg2 contains tchannel thrift Arg2 interfaces for external use.
Package arg2 contains tchannel thrift Arg2 interfaces for external use.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL