election.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. package grpcproxy
  15. import (
  16. "context"
  17. "go.etcd.io/etcd/clientv3"
  18. "go.etcd.io/etcd/etcdserver/api/v3election/v3electionpb"
  19. )
  20. type electionProxy struct {
  21. client *clientv3.Client
  22. }
  23. func NewElectionProxy(client *clientv3.Client) v3electionpb.ElectionServer {
  24. return &electionProxy{client: client}
  25. }
  26. func (ep *electionProxy) Campaign(ctx context.Context, req *v3electionpb.CampaignRequest) (*v3electionpb.CampaignResponse, error) {
  27. return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Campaign(ctx, req)
  28. }
  29. func (ep *electionProxy) Proclaim(ctx context.Context, req *v3electionpb.ProclaimRequest) (*v3electionpb.ProclaimResponse, error) {
  30. return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Proclaim(ctx, req)
  31. }
  32. func (ep *electionProxy) Leader(ctx context.Context, req *v3electionpb.LeaderRequest) (*v3electionpb.LeaderResponse, error) {
  33. return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Leader(ctx, req)
  34. }
  35. func (ep *electionProxy) Observe(req *v3electionpb.LeaderRequest, s v3electionpb.Election_ObserveServer) error {
  36. conn := ep.client.ActiveConnection()
  37. ctx, cancel := context.WithCancel(s.Context())
  38. defer cancel()
  39. sc, err := v3electionpb.NewElectionClient(conn).Observe(ctx, req)
  40. if err != nil {
  41. return err
  42. }
  43. for {
  44. rr, err := sc.Recv()
  45. if err != nil {
  46. return err
  47. }
  48. if err = s.Send(rr); err != nil {
  49. return err
  50. }
  51. }
  52. }
  53. func (ep *electionProxy) Resign(ctx context.Context, req *v3electionpb.ResignRequest) (*v3electionpb.ResignResponse, error) {
  54. return v3electionpb.NewElectionClient(ep.client.ActiveConnection()).Resign(ctx, req)
  55. }