lease_client_adapter.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "golang.org/x/net/context"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "google.golang.org/grpc"
  19. )
  20. type ls2lc struct {
  21. leaseServer pb.LeaseServer
  22. }
  23. func LeaseServerToLeaseClient(ls pb.LeaseServer) pb.LeaseClient {
  24. return &ls2lc{ls}
  25. }
  26. func (c *ls2lc) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (*pb.LeaseGrantResponse, error) {
  27. return c.leaseServer.LeaseGrant(ctx, in)
  28. }
  29. func (c *ls2lc) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (*pb.LeaseRevokeResponse, error) {
  30. return c.leaseServer.LeaseRevoke(ctx, in)
  31. }
  32. func (c *ls2lc) LeaseKeepAlive(ctx context.Context, opts ...grpc.CallOption) (pb.Lease_LeaseKeepAliveClient, error) {
  33. cs := newPipeStream(ctx, func(ss chanServerStream) error {
  34. return c.leaseServer.LeaseKeepAlive(&ls2lcServerStream{ss})
  35. })
  36. return &ls2lcClientStream{cs}, nil
  37. }
  38. func (c *ls2lc) LeaseTimeToLive(ctx context.Context, in *pb.LeaseTimeToLiveRequest, opts ...grpc.CallOption) (*pb.LeaseTimeToLiveResponse, error) {
  39. return c.leaseServer.LeaseTimeToLive(ctx, in)
  40. }
  41. // ls2lcClientStream implements Lease_LeaseKeepAliveClient
  42. type ls2lcClientStream struct{ chanClientStream }
  43. // ls2lcServerStream implements Lease_LeaseKeepAliveServer
  44. type ls2lcServerStream struct{ chanServerStream }
  45. func (s *ls2lcClientStream) Send(rr *pb.LeaseKeepAliveRequest) error {
  46. return s.SendMsg(rr)
  47. }
  48. func (s *ls2lcClientStream) Recv() (*pb.LeaseKeepAliveResponse, error) {
  49. var v interface{}
  50. if err := s.RecvMsg(&v); err != nil {
  51. return nil, err
  52. }
  53. return v.(*pb.LeaseKeepAliveResponse), nil
  54. }
  55. func (s *ls2lcServerStream) Send(rr *pb.LeaseKeepAliveResponse) error {
  56. return s.SendMsg(rr)
  57. }
  58. func (s *ls2lcServerStream) Recv() (*pb.LeaseKeepAliveRequest, error) {
  59. var v interface{}
  60. if err := s.RecvMsg(&v); err != nil {
  61. return nil, err
  62. }
  63. return v.(*pb.LeaseKeepAliveRequest), nil
  64. }