election_client_adapter.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2017 The etcd Authors
  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 adapter
  15. import (
  16. "context"
  17. "go.etcd.io/etcd/etcdserver/api/v3election/v3electionpb"
  18. "google.golang.org/grpc"
  19. )
  20. type es2ec struct{ es v3electionpb.ElectionServer }
  21. func ElectionServerToElectionClient(es v3electionpb.ElectionServer) v3electionpb.ElectionClient {
  22. return &es2ec{es}
  23. }
  24. func (s *es2ec) Campaign(ctx context.Context, r *v3electionpb.CampaignRequest, opts ...grpc.CallOption) (*v3electionpb.CampaignResponse, error) {
  25. return s.es.Campaign(ctx, r)
  26. }
  27. func (s *es2ec) Proclaim(ctx context.Context, r *v3electionpb.ProclaimRequest, opts ...grpc.CallOption) (*v3electionpb.ProclaimResponse, error) {
  28. return s.es.Proclaim(ctx, r)
  29. }
  30. func (s *es2ec) Leader(ctx context.Context, r *v3electionpb.LeaderRequest, opts ...grpc.CallOption) (*v3electionpb.LeaderResponse, error) {
  31. return s.es.Leader(ctx, r)
  32. }
  33. func (s *es2ec) Resign(ctx context.Context, r *v3electionpb.ResignRequest, opts ...grpc.CallOption) (*v3electionpb.ResignResponse, error) {
  34. return s.es.Resign(ctx, r)
  35. }
  36. func (s *es2ec) Observe(ctx context.Context, in *v3electionpb.LeaderRequest, opts ...grpc.CallOption) (v3electionpb.Election_ObserveClient, error) {
  37. cs := newPipeStream(ctx, func(ss chanServerStream) error {
  38. return s.es.Observe(in, &es2ecServerStream{ss})
  39. })
  40. return &es2ecClientStream{cs}, nil
  41. }
  42. // es2ecClientStream implements Election_ObserveClient
  43. type es2ecClientStream struct{ chanClientStream }
  44. // es2ecServerStream implements Election_ObserveServer
  45. type es2ecServerStream struct{ chanServerStream }
  46. func (s *es2ecClientStream) Send(rr *v3electionpb.LeaderRequest) error {
  47. return s.SendMsg(rr)
  48. }
  49. func (s *es2ecClientStream) Recv() (*v3electionpb.LeaderResponse, error) {
  50. var v interface{}
  51. if err := s.RecvMsg(&v); err != nil {
  52. return nil, err
  53. }
  54. return v.(*v3electionpb.LeaderResponse), nil
  55. }
  56. func (s *es2ecServerStream) Send(rr *v3electionpb.LeaderResponse) error {
  57. return s.SendMsg(rr)
  58. }
  59. func (s *es2ecServerStream) Recv() (*v3electionpb.LeaderRequest, error) {
  60. var v interface{}
  61. if err := s.RecvMsg(&v); err != nil {
  62. return nil, err
  63. }
  64. return v.(*v3electionpb.LeaderRequest), nil
  65. }