go.etcd.io/etcd@v3.3.27+incompatible/proxy/grpcproxy/adapter/election_client_adapter.go (about)

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