Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: support vtproto #52

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions drpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type Transport interface {
// This exists so that one can use whatever protobuf library/runtime they want.
type Message interface{}

type VTProtoMessage interface {
FromVTPool() interface{}
ReturnToVTPool()
}

// Conn represents a client connection to a server.
type Conn interface {
// Close closes the connection.
Expand Down
26 changes: 22 additions & 4 deletions drpcmux/handle_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,29 @@ func (m *Mux) HandleRPC(stream drpc.Stream, rpc string) (err error) {
return drpc.ProtocolError.New("unknown rpc: %q", rpc)
}

in := interface{}(stream)
var (
in = interface{}(stream)
msg drpc.Message
)
if data.in1 != streamType {
msg, ok := reflect.New(data.in1.Elem()).Interface().(drpc.Message)
if !ok {
return drpc.InternalError.New("invalid rpc input type")
// If its an vtprotobuf supported message type
if data.in1.Implements(vtMessageType) {
p := reflect.New(data.in1.Elem()).Interface().(drpc.VTProtoMessage).FromVTPool()
if p == nil {
return drpc.InternalError.New("unable to get message from vt pool")
}
msg, ok = p.(drpc.Message)
if !ok {
return drpc.InternalError.New("invalid rpc input type")
}

defer p.(drpc.VTProtoMessage).ReturnToVTPool()
} else {
m, ok := reflect.New(data.in1.Elem()).Interface().(drpc.Message)
if !ok {
return drpc.InternalError.New("invalid rpc input type")
}
msg = m
}
if err := stream.MsgRecv(msg, data.enc); err != nil {
return errs.Wrap(err)
Expand Down
5 changes: 3 additions & 2 deletions drpcmux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func New() *Mux {
}

var (
streamType = reflect.TypeOf((*drpc.Stream)(nil)).Elem()
messageType = reflect.TypeOf((*drpc.Message)(nil)).Elem()
streamType = reflect.TypeOf((*drpc.Stream)(nil)).Elem()
messageType = reflect.TypeOf((*drpc.Message)(nil)).Elem()
vtMessageType = reflect.TypeOf((*drpc.VTProtoMessage)(nil)).Elem()
)

type rpcData struct {
Expand Down
2 changes: 1 addition & 1 deletion examples/drpc/pb/sesamestreet.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/drpc/pb/sesamestreet_drpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions examples/drpc_vtproto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# DRPC example

This example is a bare-bones DRPC use case. It is intended
to show the minimal differences from the gRPC basic example.


```
protoc --go_out=paths=source_relative:. --plugin protoc-gen-go="${GOPATH}/bin/protoc-gen-go" \
--go-grpc_out=paths=source_relative:. --plugin protoc-gen-go-grpc="${GOPATH}/bin/protoc-gen-go-grpc" \
--go-drpc_out=paths=source_relative:. --plugin protoc-gen-go-drpc="${GOPATH}/bin/protoc-gen-go-drpc" \
--go-vtproto_out=paths=source_relative:. --plugin protoc-gen-go-vtproto="${GOPATH}/bin/protoc-gen-go-vtproto" \
--go-vtproto_opt=pool=storj.io/drpc/examples/drpc_vtproto/pb.Cookie \
--go-vtproto_opt=pool=storj.io/drpc/examples/drpc_vtproto/pb.CookiePool \
--go-vtproto_opt=features=marshal+unmarshal+equal+clone+size+pool \
sesamestreet.proto
```
Loading