...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package grpcproxy
16
17 import (
18 "context"
19
20 "go.etcd.io/etcd/client/v3"
21 "go.etcd.io/etcd/server/v3/etcdserver/api/v3election/v3electionpb"
22 )
23
24 type electionProxy struct {
25 client *clientv3.Client
26 }
27
28 func NewElectionProxy(client *clientv3.Client) v3electionpb.ElectionServer {
29 return &electionProxy{client: client}
30 }
31
32 func (ep *electionProxy) Campaign(ctx context.Context, req *v3electionpb.CampaignRequest) (*v3electionpb.CampaignResponse, error) {
33 return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Campaign(ctx, req)
34 }
35
36 func (ep *electionProxy) Proclaim(ctx context.Context, req *v3electionpb.ProclaimRequest) (*v3electionpb.ProclaimResponse, error) {
37 return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Proclaim(ctx, req)
38 }
39
40 func (ep *electionProxy) Leader(ctx context.Context, req *v3electionpb.LeaderRequest) (*v3electionpb.LeaderResponse, error) {
41 return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Leader(ctx, req)
42 }
43
44 func (ep *electionProxy) Observe(req *v3electionpb.LeaderRequest, s v3electionpb.Election_ObserveServer) error {
45 conn := ep.client.ActiveConnection()
46 ctx, cancel := context.WithCancel(s.Context())
47 defer cancel()
48 sc, err := v3electionpb.NewElectionClient(conn).Observe(ctx, req)
49 if err != nil {
50 return err
51 }
52 for {
53 rr, err := sc.Recv()
54 if err != nil {
55 return err
56 }
57 if err = s.Send(rr); err != nil {
58 return err
59 }
60 }
61 }
62
63 func (ep *electionProxy) Resign(ctx context.Context, req *v3electionpb.ResignRequest) (*v3electionpb.ResignResponse, error) {
64 return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Resign(ctx, req)
65 }
66
View as plain text