maintenance_client_adapter.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  18. "google.golang.org/grpc"
  19. )
  20. type mts2mtc struct{ mts pb.MaintenanceServer }
  21. func MaintenanceServerToMaintenanceClient(mts pb.MaintenanceServer) pb.MaintenanceClient {
  22. return &mts2mtc{mts}
  23. }
  24. func (s *mts2mtc) Alarm(ctx context.Context, r *pb.AlarmRequest, opts ...grpc.CallOption) (*pb.AlarmResponse, error) {
  25. return s.mts.Alarm(ctx, r)
  26. }
  27. func (s *mts2mtc) Status(ctx context.Context, r *pb.StatusRequest, opts ...grpc.CallOption) (*pb.StatusResponse, error) {
  28. return s.mts.Status(ctx, r)
  29. }
  30. func (s *mts2mtc) Defragment(ctx context.Context, dr *pb.DefragmentRequest, opts ...grpc.CallOption) (*pb.DefragmentResponse, error) {
  31. return s.mts.Defragment(ctx, dr)
  32. }
  33. func (s *mts2mtc) Hash(ctx context.Context, r *pb.HashRequest, opts ...grpc.CallOption) (*pb.HashResponse, error) {
  34. return s.mts.Hash(ctx, r)
  35. }
  36. func (s *mts2mtc) HashKV(ctx context.Context, r *pb.HashKVRequest, opts ...grpc.CallOption) (*pb.HashKVResponse, error) {
  37. return s.mts.HashKV(ctx, r)
  38. }
  39. func (s *mts2mtc) MoveLeader(ctx context.Context, r *pb.MoveLeaderRequest, opts ...grpc.CallOption) (*pb.MoveLeaderResponse, error) {
  40. return s.mts.MoveLeader(ctx, r)
  41. }
  42. func (s *mts2mtc) Snapshot(ctx context.Context, in *pb.SnapshotRequest, opts ...grpc.CallOption) (pb.Maintenance_SnapshotClient, error) {
  43. cs := newPipeStream(ctx, func(ss chanServerStream) error {
  44. return s.mts.Snapshot(in, &ss2scServerStream{ss})
  45. })
  46. return &ss2scClientStream{cs}, nil
  47. }
  48. // ss2scClientStream implements Maintenance_SnapshotClient
  49. type ss2scClientStream struct{ chanClientStream }
  50. // ss2scServerStream implements Maintenance_SnapshotServer
  51. type ss2scServerStream struct{ chanServerStream }
  52. func (s *ss2scClientStream) Send(rr *pb.SnapshotRequest) error {
  53. return s.SendMsg(rr)
  54. }
  55. func (s *ss2scClientStream) Recv() (*pb.SnapshotResponse, error) {
  56. var v interface{}
  57. if err := s.RecvMsg(&v); err != nil {
  58. return nil, err
  59. }
  60. return v.(*pb.SnapshotResponse), nil
  61. }
  62. func (s *ss2scServerStream) Send(rr *pb.SnapshotResponse) error {
  63. return s.SendMsg(rr)
  64. }
  65. func (s *ss2scServerStream) Recv() (*pb.SnapshotRequest, error) {
  66. var v interface{}
  67. if err := s.RecvMsg(&v); err != nil {
  68. return nil, err
  69. }
  70. return v.(*pb.SnapshotRequest), nil
  71. }