lease_client_adapter.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 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. func (c *ls2lc) LeaseLeases(ctx context.Context, in *pb.LeaseLeasesRequest, opts ...grpc.CallOption) (*pb.LeaseLeasesResponse, error) {
  42. return c.leaseServer.LeaseLeases(ctx, in)
  43. }
  44. // ls2lcClientStream implements Lease_LeaseKeepAliveClient
  45. type ls2lcClientStream struct{ chanClientStream }
  46. // ls2lcServerStream implements Lease_LeaseKeepAliveServer
  47. type ls2lcServerStream struct{ chanServerStream }
  48. func (s *ls2lcClientStream) Send(rr *pb.LeaseKeepAliveRequest) error {
  49. return s.SendMsg(rr)
  50. }
  51. func (s *ls2lcClientStream) Recv() (*pb.LeaseKeepAliveResponse, error) {
  52. var v interface{}
  53. if err := s.RecvMsg(&v); err != nil {
  54. return nil, err
  55. }
  56. return v.(*pb.LeaseKeepAliveResponse), nil
  57. }
  58. func (s *ls2lcServerStream) Send(rr *pb.LeaseKeepAliveResponse) error {
  59. return s.SendMsg(rr)
  60. }
  61. func (s *ls2lcServerStream) Recv() (*pb.LeaseKeepAliveRequest, error) {
  62. var v interface{}
  63. if err := s.RecvMsg(&v); err != nil {
  64. return nil, err
  65. }
  66. return v.(*pb.LeaseKeepAliveRequest), nil
  67. }