maintenance.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2016 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 grpcproxy
  15. import (
  16. "io"
  17. "golang.org/x/net/context"
  18. "github.com/coreos/etcd/clientv3"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. )
  21. type maintenanceProxy struct {
  22. client *clientv3.Client
  23. }
  24. func NewMaintenanceProxy(c *clientv3.Client) pb.MaintenanceServer {
  25. return &maintenanceProxy{
  26. client: c,
  27. }
  28. }
  29. func (mp *maintenanceProxy) Defragment(ctx context.Context, dr *pb.DefragmentRequest) (*pb.DefragmentResponse, error) {
  30. conn := mp.client.ActiveConnection()
  31. return pb.NewMaintenanceClient(conn).Defragment(ctx, dr)
  32. }
  33. func (mp *maintenanceProxy) Snapshot(sr *pb.SnapshotRequest, stream pb.Maintenance_SnapshotServer) error {
  34. conn := mp.client.ActiveConnection()
  35. ctx, cancel := context.WithCancel(stream.Context())
  36. defer cancel()
  37. sc, err := pb.NewMaintenanceClient(conn).Snapshot(ctx, sr)
  38. if err != nil {
  39. return err
  40. }
  41. for {
  42. rr, err := sc.Recv()
  43. if err != nil {
  44. if err == io.EOF {
  45. return nil
  46. }
  47. return err
  48. }
  49. err = stream.Send(rr)
  50. if err != nil {
  51. return err
  52. }
  53. }
  54. }
  55. func (mp *maintenanceProxy) Hash(ctx context.Context, r *pb.HashRequest) (*pb.HashResponse, error) {
  56. conn := mp.client.ActiveConnection()
  57. return pb.NewMaintenanceClient(conn).Hash(ctx, r)
  58. }
  59. func (mp *maintenanceProxy) Alarm(ctx context.Context, r *pb.AlarmRequest) (*pb.AlarmResponse, error) {
  60. conn := mp.client.ActiveConnection()
  61. return pb.NewMaintenanceClient(conn).Alarm(ctx, r)
  62. }
  63. func (mp *maintenanceProxy) Status(ctx context.Context, r *pb.StatusRequest) (*pb.StatusResponse, error) {
  64. conn := mp.client.ActiveConnection()
  65. return pb.NewMaintenanceClient(conn).Status(ctx, r)
  66. }