blob: 4b4a4cc42109b121a98f5f099380b0c651cf0313 [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001// Copyright 2017 The etcd Lockors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package grpcproxy
16
17import (
18 "context"
19
20 "github.com/coreos/etcd/clientv3"
21 "github.com/coreos/etcd/etcdserver/api/v3election/v3electionpb"
22)
23
24type electionProxy struct {
25 client *clientv3.Client
26}
27
28func NewElectionProxy(client *clientv3.Client) v3electionpb.ElectionServer {
29 return &electionProxy{client: client}
30}
31
32func (ep *electionProxy) Campaign(ctx context.Context, req *v3electionpb.CampaignRequest) (*v3electionpb.CampaignResponse, error) {
33 return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Campaign(ctx, req)
34}
35
36func (ep *electionProxy) Proclaim(ctx context.Context, req *v3electionpb.ProclaimRequest) (*v3electionpb.ProclaimResponse, error) {
37 return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Proclaim(ctx, req)
38}
39
40func (ep *electionProxy) Leader(ctx context.Context, req *v3electionpb.LeaderRequest) (*v3electionpb.LeaderResponse, error) {
41 return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Leader(ctx, req)
42}
43
44func (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
63func (ep *electionProxy) Resign(ctx context.Context, req *v3electionpb.ResignRequest) (*v3electionpb.ResignResponse, error) {
64 return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Resign(ctx, req)
65}