inprocgrpc

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: MIT Imports: 17 Imported by: 18

Documentation

Overview

Package inprocgrpc provides an in-process gRPC channel implementation. The in-process channel makes RPCs that are effectively in-process function calls. The function calls run in a separate goroutine from the caller so as to fully behave like a normal RPC call, including context deadlines and cancellations. This can be useful for same-process hand-off to RPC endpoints and for testing. It has less overhead than using a loopback socket connection as it elides marshaling to bytes and back.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientContext added in v1.1.0

func ClientContext(ctx context.Context) context.Context

ClientContext, when called on a server context, returns the original client context passed into Channel.Invoke() / Channel.NewStream().

Types

type Channel

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

Channel is a gRPC channel where RPCs amount to an in-process method call. This is in contrast to a normal gRPC channel, where request messages marshaled to another process over a TCP socket and responses unmarshaled from the same socket.

So the in-process channel functions as both a client channel AND a server. It handles method calls from a gRPC client stub and then delivers them directly to a registered server implementation.

The server-side of an RPC is executed in a separate goroutine, so things like deadlines and context cancellation work, even for unary RPCs.

func (*Channel) Invoke

func (c *Channel) Invoke(ctx context.Context, method string, req, resp interface{}, opts ...grpc.CallOption) error

Invoke satisfies the grpchan.Channel interface and supports sending unary RPCs via the in-process channel.

func (*Channel) NewStream

func (c *Channel) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)

NewStream satisfies the grpchan.Channel interface and supports sending streaming RPCs via the in-process channel.

func (*Channel) RegisterService

func (c *Channel) RegisterService(desc *grpc.ServiceDesc, svr interface{})

RegisterService registers the given service and implementation. Like a normal gRPC server, an in-process channel only allows a single implementation for a particular service. Services are identified by their fully-qualified name (e.g. "<package>.<service>").

func (*Channel) WithCloner

func (c *Channel) WithCloner(cloner Cloner) *Channel

WithCloner configures the in-process channel to use the given cloner to copy data from client to server and vice versa. Messages must be copied to avoid concurrent use of message instances.

If no cloner is configured, the default cloner can only properly support proto.Message instances. If the messages do not implement proto.Message (or if you use gogo/protobuf, whose values implement the golang/proto interface but will cause a runtime panic), you must provide a custom cloner.

func (*Channel) WithServerStreamInterceptor

func (c *Channel) WithServerStreamInterceptor(interceptor grpc.StreamServerInterceptor) *Channel

WithServerStreamInterceptor configures the in-process channel to use the given server interceptor for streaming RPCs when dispatching.

func (*Channel) WithServerUnaryInterceptor

func (c *Channel) WithServerUnaryInterceptor(interceptor grpc.UnaryServerInterceptor) *Channel

WithServerUnaryInterceptor configures the in-process channel to use the given server interceptor for unary RPCs when dispatching.

type Cloner

type Cloner interface {
	Copy(out, in interface{}) error
	Clone(interface{}) (interface{}, error)
}

Cloner knows how to make copies of messages. It can be asked to copy one value into another, and it can also be asked to simply synthesize a new value that is a copy of some input value.

This is used to copy messages between in-process client and server. Copying will usually be more efficient than marshalling to bytes and back (though that is a valid strategy that a custom Cloner implementation could take). Copies are made to avoid sharing values across client and server goroutines.

func CloneFunc

func CloneFunc(fn func(interface{}) (interface{}, error)) Cloner

CloneFunc adapts a single clone function to the Cloner interface. The given function implements the Clone method. To implement the Copy method, the given function is invoked and then reflection is used to shallow copy the clone to the output.

func CodecCloner

func CodecCloner(codec encoding.Codec) Cloner

CodecCloner uses the given codec to implement the Cloner interface. The Copy method is implemented by using the code to marshal the input to bytes and then unmarshal from bytes into the output value. The Clone method then uses reflection to create a new value of the same type and uses this strategy to then copy the input to the newly created value.

func CopyFunc

func CopyFunc(fn func(out, in interface{}) error) Cloner

CopyFunc adapts a single copy function to the Cloner interface. The given function implements the Copy method. To implement the Clone method, a new value of the same type is created using reflection and then the given function is used to copy the input to the newly created value.

type ProtoCloner

type ProtoCloner struct{}

ProtoCloner is the default cloner used by an in-process channel. This implementation can correctly handle protobuf messages. Copy and clone operations will fail if the input message is not a protobuf message (in which case a custom cloner must be used).

func (ProtoCloner) Clone

func (ProtoCloner) Clone(in interface{}) (interface{}, error)

func (ProtoCloner) Copy

func (ProtoCloner) Copy(out, in interface{}) error

Jump to

Keyboard shortcuts

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