Peter Malina github.com/gelidus
CTO @ FlowUp github.com/flowup
<person>
<name>John Doe</name>
<email>jdoe@example.com</email>
</person>XML
// Schema
message Person {
string name = 1;
string email = 2;
}
// Textual representation
person {
name: "John Doe"
email: "jdoe@example.com"
}Protocol Buffers
28 bytes, 100-200ns to parse
69 bytes, 5K-10Kns to parse
syntax = "proto3";
package mypackage;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
message Outside {
string Field1 = 1 [(gogoproto.jsontag) = "myfield"];
string Field2 = 2 [(gogoproto.moretags) = "xml:\",comment\""];
}// Calculator is a service that adds two numbers
service Calculator {
rpc Add (AddRequest) returns (AddReply) {}
}
message AddRequest {
int32 x = 1;
int32 y = 2;
}
message AddReply {
int32 res = 1;
}type CalculatorServer interface {
Add(context.Context, *AddRequest) (*AddReply, error)
}
// ... there is far more code ...~Generates~
type calculatorServer struct {}
// Add handles request to add two numbers
func (s *calculatorServer) Add(ctx context.Context, req *AddRequest) (*AddReply, error) {
return &AddReply{
Res: req.X + req.Y
}, nil
}
s := grpc.NewServer()
proto.RegisterCalculatorServer(s, &calculatorServer{})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}And start the server
// dial grpc connection
conn, _ := grpc.Dial("127.0.0.1:8000")
defer conn.Close()
// create stub
calculator := NewCalculatorClient(conn)
// call remote method
res, err := calculator.Add(context.Background(), &AddRequest{5, 10})
// <AddReply> res.Res == 15
if err != nil {
// ...
}