sslobodr | d046be8 | 2019-01-16 10:02:22 -0500 | [diff] [blame^] | 1 | // 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 | |
| 15 | package grpcproxy |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | |
| 20 | "github.com/coreos/etcd/clientv3" |
| 21 | "github.com/coreos/etcd/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 | } |