protorpc

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2021 License: BSD-3-Clause Imports: 12 Imported by: 30

README


protorpc

██████╗ ██████╗  ██████╗ ████████╗ ██████╗       ██████╗ ██████╗  ██████╗
██╔══██╗██╔══██╗██╔═══██╗╚══██╔══╝██╔═══██╗      ██╔══██╗██╔══██╗██╔════╝
██████╔╝██████╔╝██║   ██║   ██║   ██║   ██║█████╗██████╔╝██████╔╝██║
██╔═══╝ ██╔══██╗██║   ██║   ██║   ██║   ██║╚════╝██╔══██╗██╔═══╝ ██║
██║     ██║  ██║╚██████╔╝   ██║   ╚██████╔╝      ██║  ██║██║     ╚██████╗
╚═╝     ╚═╝  ╚═╝ ╚═════╝    ╚═╝    ╚═════╝       ╚═╝  ╚═╝╚═╝      ╚═════╝

Build Status GoDoc

Install

Install protorpc package:

  1. go install github.com/golang/protobuf/protoc-gen-go
  2. go get github.com/chai2010/protorpc
  3. go run hello.go

Install protoc-gen-go plugin:

  1. install protoc at first: http://github.com/google/protobuf/releases
  2. go get github.com/golang/protobuf/protoc-gen-go
  3. go get github.com/chai2010/protorpc/protoc-gen-protorpc
  4. go generate github.com/chai2010/protorpc/examples/service.pb
  5. go test github.com/chai2010/protorpc/examples/service.pb

Examples

First, create echo.proto:

syntax = "proto3";

package service;

message EchoRequest {
	string msg = 1;
}

message EchoResponse {
	string msg = 1;
}

service EchoService {
	rpc Echo (EchoRequest) returns (EchoResponse);
	rpc EchoTwice (EchoRequest) returns (EchoResponse);
}

Second, generate echo.pb.go and echo.pb.protorpc.go from echo.proto (we can use go generate to invoke this command, see proto.go).

protoc --go_out=. echo.proto
protoc --protorpc_out=. echo.proto

Now, we can use the stub code like this:

package main

import (
	"fmt"
	"log"

	"github.com/chai2010/protorpc"
	service "github.com/chai2010/protorpc/examples/service.pb"
)

type Echo int

func (t *Echo) Echo(args *service.EchoRequest, reply *service.EchoResponse) error {
	reply.Msg = args.Msg
	return nil
}

func (t *Echo) EchoTwice(args *service.EchoRequest, reply *service.EchoResponse) error {
	reply.Msg = args.Msg + args.Msg
	return nil
}

func init() {
	go service.ListenAndServeEchoService("tcp", `127.0.0.1:9527`, new(Echo))
}

func main() {
	echoClient, err := service.DialEchoService("tcp", `127.0.0.1:9527`)
	if err != nil {
		log.Fatalf("service.DialEchoService: %v", err)
	}
	defer echoClient.Close()

	args := &service.EchoRequest{Msg: "你好, 世界!"}
	reply, err := echoClient.EchoTwice(args)
	if err != nil {
		log.Fatalf("echoClient.EchoTwice: %v", err)
	}
	fmt.Println(reply.Msg)

	// or use normal client
	client, err := protorpc.Dial("tcp", `127.0.0.1:9527`)
	if err != nil {
		log.Fatalf("protorpc.Dial: %v", err)
	}
	defer client.Close()

	echoClient1 := &service.EchoServiceClient{client}
	echoClient2 := &service.EchoServiceClient{client}
	reply, err = echoClient1.EchoTwice(args)
	reply, err = echoClient2.EchoTwice(args)
	_, _ = reply, err

	// Output:
	// 你好, 世界!你好, 世界!
}

More examples.

standard net/rpc

First, create echo.proto:

syntax = "proto3";

package service;

message EchoRequest {
	string msg = 1;
}

message EchoResponse {
	string msg = 1;
}

service EchoService {
	rpc Echo (EchoRequest) returns (EchoResponse);
	rpc EchoTwice (EchoRequest) returns (EchoResponse);
}

Second, generate echo.pb.go from echo.proto with protoc-gen-stdrpc plugin.

protoc --stdrpc_out=. echo.proto

The stdrpc plugin generated code do not depends protorpc package, it use gob as the default rpc encoding.

Add prefix

$ ENV_PROTOC_GEN_PROTORPC_FLAG_PREFIX=abc protoc --protorpc_out=. x.proto

BUGS

Report bugs to chaishushan@gmail.com.

Thanks!

Documentation

Overview

Package protorpc implements a Protobuf-RPC ClientCodec and ServerCodec for the rpc package.

To install it, you must first have Go (version 1) installed (see http://golang.org/doc/install). Next, install the standard protocol buffer implementation from http://github.com/google/protobuf/; you must be running version 2.3 or higher.

Finally run

go get github.com/chai2010/protorpc
go get github.com/chai2010/protorpc/protoc-gen-protorpc

to install the support library and protocol compiler.

Here is a simple proto file("arith.pb/arith.proto"):

package arith;

message ArithRequest {
	optional int32 a = 1;
	optional int32 b = 2;
}

message ArithResponse {
	optional int32 val = 1;
	optional int32 quo = 2;
	optional int32 rem = 3;
}

service ArithService {
	rpc multiply (ArithRequest) returns (ArithResponse);
	rpc divide (ArithRequest) returns (ArithResponse);
}

Then use "protoc-gen-go" to generate "arith.pb.go" file:

cd arith.pb && protoc --go_out=. arith.proto

Use "protoc-gen-protorpc" to generate "arith.pb.protorpc.go" file (include stub code):

cd arith.pb && protoc --protorpc_out=. arith.proto

The server calls (for TCP service):

package server

import (
	"errors"

	"github.com/golang/protobuf/proto"

	"./arith.pb"
)

type Arith int

func (t *Arith) Multiply(args *arith.ArithRequest, reply *arith.ArithResponse) error {
	reply.Val = proto.Int32(args.GetA() * args.GetB())
	return nil
}

func (t *Arith) Divide(args *arith.ArithRequest, reply *arith.ArithResponse) error {
	if args.GetB() == 0 {
		return errors.New("divide by zero")
	}
	reply.Quo = proto.Int32(args.GetA() / args.GetB())
	reply.Rem = proto.Int32(args.GetA() % args.GetB())
	return nil
}

func main() {
	arith.ListenAndServeArithService("tcp", ":1984", new(Arith))
}

At this point, clients can see a service "Arith" with methods "ArithService.Multiply" and "ArithService.Divide". To invoke one, a client first dials the server:

stub, err := arith.DialArithService("tcp", "127.0.0.1:1984")
if err != nil {
	log.Fatal(`arith.DialArithService("tcp", "127.0.0.1:1984"):`, err)
}
defer stub.Close()

Then it can make a remote call with stub:

var args ArithRequest

args.A = proto.Int32(7)
args.B = proto.Int32(8)
reply, err := stub.Multiply(&args, &reply)
if err != nil {
	log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d*%d=%d", args.GetA(), args.GetB(), reply.GetVal())

More example:

go test github.com/chai2010/protorpc/internal/service.pb

Report bugs to <chaishushan@gmail.com>.

Thanks!

Index

Constants

This section is empty.

Variables

View Source
var (
	UseSnappy            = true
	UseCrc32ChecksumIEEE = true
)

Functions

func Dial

func Dial(network, address string) (*rpc.Client, error)

Dial connects to a Protobuf-RPC server at the specified network address.

func DialTimeout

func DialTimeout(network, address string, timeout time.Duration) (*rpc.Client, error)

DialTimeout connects to a Protobuf-RPC server at the specified network address.

func NewClient

func NewClient(conn io.ReadWriteCloser) *rpc.Client

NewClient returns a new rpc.Client to handle requests to the set of services at the other end of the connection.

func NewClientCodec

func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec

NewClientCodec returns a new rpc.ClientCodec using Protobuf-RPC on conn.

func NewServerCodec

func NewServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec

NewServerCodec returns a serverCodec that communicates with the ClientCodec on the other end of the given conn.

func ServeConn

func ServeConn(conn io.ReadWriteCloser)

ServeConn runs the Protobuf-RPC server on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement.

Types

This section is empty.

Directories

Path Synopsis
examples
message.pb
Package message is a generated protocol buffer package.
Package message is a generated protocol buffer package.
proto3.pb
Package proto3_proto is a generated protocol buffer package.
Package proto3_proto is a generated protocol buffer package.
service.pb
Package service is a generated protocol buffer package.
Package service is a generated protocol buffer package.
stdrpc.pb
Package service is a generated protocol buffer package.
Package service is a generated protocol buffer package.
protoc-gen-go is a plugin for the Google protocol buffer compiler to generate Go code.
protoc-gen-go is a plugin for the Google protocol buffer compiler to generate Go code.
Package protorpc_wire is a generated protocol buffer package.
Package protorpc_wire is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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