drpc

package module
Version: v0.0.30 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: MIT Imports: 3 Imported by: 29

README

DRPC

A drop-in, lightweight gRPC replacement.

Go Report Card Go Doc Beta Zulip Chat

Highlights

  • Simple, at just a few thousand lines of code.
  • Small dependencies. Only 3 requirements in go.mod, and 9 lines of go mod graph!
  • Compatible. Works for many gRPC use-cases as-is!
  • Fast. DRPC has a lightning quick wire format.
  • Extensible. DRPC is transport agnostic, supports middleware, and is designed around interfaces.
  • Battle Tested. Already used in production for years across tens of thousands of servers.

External Packages

  • go.bryk.io/pkg/net/drpc

    • Simplified TLS setup (for client and server)
    • Server middleware, including basic components for logging, token-based auth, rate limit, panic recovery, etc
    • Client middleware, including basic components for logging, custom metadata, panic recovery, etc
    • Bi-directional streaming support over upgraded HTTP(S) connections using WebSockets
    • Concurrent RPCs via connection pool
  • Open an issue or join the Zulip chat if you'd like to be featured here.

Other Languages

DRPC can be made compatible with RPC clients generated from other languages. For example, Twirp clients and grpc-web clients can be used against the drpchttp package.

Native implementations can have some advantages, and so some bindings for other languages are in progress, all in various states of completeness. Join the Zulip chat if you want more information or to help out with any!

Language Repository Status
C++ https://github.com/storj/drpc-cpp Incomplete
Rust https://github.com/zeebo/drpc-rs Incomplete
Node https://github.com/mjpitz/drpc-node Incomplete

Licensing

DRPC is licensed under the MIT/expat license. See the LICENSE file for more.


Benchmarks

These microbenchmarks attempt to provide a comparison and come with some caveats. First, it does not send data over a network connection which is expected to be the bottleneck almost all of the time. Second, no attempt was made to do the benchmarks in a controlled environment (CPU scaling disabled, noiseless, etc.). Third, no tuning was done to ensure they're both performing optimally, so there is an inherent advantage for DRPC because the author is familiar with how it works.

Measure Benchmark Small Medium Large
gRPCDRPCdelta gRPCDRPCdelta gRPCDRPCdelta
time/op Unitary 30.2µs8.6µs-71.60% 38.0µs11.1µs-70.88% 1.33ms0.63ms-52.30%
Input Stream 878ns759ns-13.54% 2.85µs2.00µs-29.69% 508µs249µs-51.08%
Output Stream 862ns757ns-12.18% 2.76µs1.99µs-27.92% 487µs239µs-50.94%
Bidir Stream 9.81µs3.30µs-66.38% 14.8µs4.9µs-66.69% 1.31ms0.55ms-58.41%
speed Unitary 70.0kB/s230.0kB/s+228.57% 54.0MB/s185.3MB/s+243.44% 791MB/s1658MB/s+109.62%
Input Stream 2.29MB/s2.64MB/s+15.37% 721MB/s1026MB/s+42.21% 2.06GB/s4.22GB/s+104.32%
Output Stream 2.32MB/s2.64MB/s+13.67% 743MB/s1031MB/s+38.74% 2.15GB/s4.39GB/s+103.75%
Bidir Stream 200kB/s604kB/s+201.87% 138MB/s415MB/s+200.20% 799MB/s1920MB/s+140.44%
mem/op Unitary 8.37kB1.29kB-84.59% 21.8kB7.7kB-64.81% 6.50MB3.16MB-51.38%
Input Stream 399B80B-79.96% 7.09kB2.13kB-69.97% 3.20MB1.05MB-67.16%
Output Stream 309B80B-74.13% 6.98kB2.13kB-69.53% 3.20MB1.05MB-67.17%
Bidir Stream 1.02kB0.24kB-76.40% 14.4kB4.3kB-69.99% 6.52MB2.10MB-67.74%
allocs/op Unitary 1697-95.86% 1719-94.74% 4039-97.76%
Input Stream 111-90.91% 122-83.33% 1212-98.35%
Output Stream 91-88.89% 102-80.00% 1172-98.29%
Bidir Stream 413-92.68% 445-88.64% 2725-98.16%

Lines of code

Package Lines
storj.io/drpc/drpchttp 475
storj.io/drpc/cmd/protoc-gen-go-drpc 418
storj.io/drpc/drpcstream 390
storj.io/drpc/drpcwire 332
storj.io/drpc/drpcmanager 300
storj.io/drpc/drpcmigrate 237
storj.io/drpc/drpcsignal 133
storj.io/drpc/drpcconn 116
storj.io/drpc/drpcmetadata 115
storj.io/drpc/drpcmux 95
storj.io/drpc/drpcserver 76
storj.io/drpc/drpccache 54
storj.io/drpc 47
storj.io/drpc/drpcerr 42
storj.io/drpc/drpcctx 37
storj.io/drpc/drpcdebug 22
storj.io/drpc/drpcenc 15
storj.io/drpc/internal/drpcopts 11
Total 2915

Documentation

Overview

Package drpc is a light replacement for gprc.

Index

Constants

This section is empty.

Variables

View Source
var (
	Error         = errs.Class("drpc")
	InternalError = errs.Class("internal error")
	ProtocolError = errs.Class("protocol error")
	ClosedError   = errs.Class("closed")
)

These error classes represent some common errors that drpc generates.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	// Close closes the connection.
	Close() error

	// Closed returns a channel that is closed if the connection is definitely closed.
	Closed() <-chan struct{}

	// Invoke issues a unary RPC to the remote. Only one Invoke or Stream may be
	// open at once.
	Invoke(ctx context.Context, rpc string, enc Encoding, in, out Message) error

	// NewStream starts a stream with the remote. Only one Invoke or Stream may be
	// open at once.
	NewStream(ctx context.Context, rpc string, enc Encoding) (Stream, error)
}

Conn represents a client connection to a server.

type Description

type Description interface {
	// NumMethods returns the number of methods available.
	NumMethods() int

	// Method returns the information about the nth method along with a handler
	// to invoke it. The method interface that it returns is expected to be
	// a method expression like `(*Type).HandlerName`.
	Method(n int) (rpc string, encoding Encoding, receiver Receiver, method interface{}, ok bool)
}

Description is the interface implemented by things that can be registered by a Server.

type Encoding added in v0.0.18

type Encoding interface {
	// Marshal returns the encoded form of msg.
	Marshal(msg Message) ([]byte, error)

	// Unmarshal reads the encoded form of some Message into msg.
	// The buf is expected to contain only a single complete Message.
	Unmarshal(buf []byte, msg Message) error
}

Encoding represents a way to marshal/unmarshal Message types.

type Handler

type Handler interface {
	// HandleRPC executes the RPC identified by the rpc string using the stream to
	// communicate with the remote.
	HandleRPC(stream Stream, rpc string) (err error)
}

Handler handles streams and RPCs dispatched to it by a Server.

type Message

type Message interface{}

Message is a protobuf message. It is expected to be used with an Encoding. This exists so that one can use whatever protobuf library/runtime they want.

type Mux added in v0.0.10

type Mux interface {
	// Register marks that the description should dispatch RPCs that it describes to
	// the provided srv.
	Register(srv interface{}, desc Description) error
}

Mux is a type that can have an implementation and a Description registered with it.

type Receiver added in v0.0.10

type Receiver = func(srv interface{}, ctx context.Context, in1, in2 interface{}) (out Message, err error)

Receiver is invoked by a server for a given RPC.

type Stream

type Stream interface {
	// Context returns the context associated with the stream. It is canceled
	// when the Stream is closed and no more messages will ever be sent or
	// received on it.
	Context() context.Context

	// MsgSend sends the Message to the remote.
	MsgSend(msg Message, enc Encoding) error

	// MsgRecv receives a Message from the remote.
	MsgRecv(msg Message, enc Encoding) error

	// CloseSend signals to the remote that we will no longer send any messages.
	CloseSend() error

	// Close closes the stream.
	Close() error
}

Stream is a bi-directional stream of messages to some other party.

type Transport

type Transport interface {
	io.Reader
	io.Writer
	io.Closer
}

Transport is an interface describing what is required for a drpc connection. Any net.Conn can be used as a Transport.

Source Files

Directories

Path Synopsis
cmd
Package drpccache implements per stream cache for drpc.
Package drpccache implements per stream cache for drpc.
Package drpcconn creates a drpc client connection from a transport.
Package drpcconn creates a drpc client connection from a transport.
Package drpcctx has helpers to interact with context.Context.
Package drpcctx has helpers to interact with context.Context.
Package drpcdebug provides helpers for debugging.
Package drpcdebug provides helpers for debugging.
Package drpcenc holds some helper functions for encoding messages.
Package drpcenc holds some helper functions for encoding messages.
Package drpcerr lets one associate error codes with errors.
Package drpcerr lets one associate error codes with errors.
Package drpchttp implements a net/http handler for unitary RPCs.
Package drpchttp implements a net/http handler for unitary RPCs.
Package drpcmanager reads packets from a transport to make streams.
Package drpcmanager reads packets from a transport to make streams.
Package drpcmetadata define the structure of the metadata supported by drpc library.
Package drpcmetadata define the structure of the metadata supported by drpc library.
Package drpcmigrate provides tools to support drpc concurrently alongside gRPC on the same ports.
Package drpcmigrate provides tools to support drpc concurrently alongside gRPC on the same ports.
Package drpcmux is a handler to dispatch rpcs to implementations.
Package drpcmux is a handler to dispatch rpcs to implementations.
Package drpcserver allows one to execute registered rpcs.
Package drpcserver allows one to execute registered rpcs.
Package drpcsignal holds a helper type to signal errors.
Package drpcsignal holds a helper type to signal errors.
Package drpcstream sends protobufs using the dprc wire protocol.
Package drpcstream sends protobufs using the dprc wire protocol.
Package drpcwire provides low level helpers for the drpc wire protocol.
Package drpcwire provides low level helpers for the drpc wire protocol.
examples
drpc Module
internal
drpcopts
Package drpcopts contains internal options.
Package drpcopts contains internal options.
grpccompat Module

Jump to

Keyboard shortcuts

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