maintenance.go 2.1 KB

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