OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / hashicorp / go-plugin / examples / grpc / shared / grpc.go
1 package shared
2
3 import (
4         "github.com/hashicorp/go-plugin/examples/grpc/proto"
5         "golang.org/x/net/context"
6 )
7
8 // GRPCClient is an implementation of KV that talks over RPC.
9 type GRPCClient struct{ client proto.KVClient }
10
11 func (m *GRPCClient) Put(key string, value []byte) error {
12         _, err := m.client.Put(context.Background(), &proto.PutRequest{
13                 Key:   key,
14                 Value: value,
15         })
16         return err
17 }
18
19 func (m *GRPCClient) Get(key string) ([]byte, error) {
20         resp, err := m.client.Get(context.Background(), &proto.GetRequest{
21                 Key: key,
22         })
23         if err != nil {
24                 return nil, err
25         }
26
27         return resp.Value, nil
28 }
29
30 // Here is the gRPC server that GRPCClient talks to.
31 type GRPCServer struct {
32         // This is the real implementation
33         Impl KV
34 }
35
36 func (m *GRPCServer) Put(
37         ctx context.Context,
38         req *proto.PutRequest) (*proto.Empty, error) {
39         return &proto.Empty{}, m.Impl.Put(req.Key, req.Value)
40 }
41
42 func (m *GRPCServer) Get(
43         ctx context.Context,
44         req *proto.GetRequest) (*proto.GetResponse, error) {
45         v, err := m.Impl.Get(req.Key)
46         return &proto.GetResponse{Value: v}, err
47 }