lease.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 v3rpc
  15. import (
  16. "io"
  17. "github.com/coreos/etcd/etcdserver"
  18. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/lease"
  21. "golang.org/x/net/context"
  22. )
  23. type LeaseServer struct {
  24. hdr header
  25. le etcdserver.Lessor
  26. }
  27. func NewLeaseServer(s *etcdserver.EtcdServer) pb.LeaseServer {
  28. return &LeaseServer{le: s, hdr: newHeader(s)}
  29. }
  30. func (ls *LeaseServer) LeaseGrant(ctx context.Context, cr *pb.LeaseGrantRequest) (*pb.LeaseGrantResponse, error) {
  31. resp, err := ls.le.LeaseGrant(ctx, cr)
  32. if err == lease.ErrLeaseExists {
  33. return nil, rpctypes.ErrGRPCLeaseExist
  34. }
  35. if err != nil {
  36. return nil, err
  37. }
  38. ls.hdr.fill(resp.Header)
  39. return resp, err
  40. }
  41. func (ls *LeaseServer) LeaseRevoke(ctx context.Context, rr *pb.LeaseRevokeRequest) (*pb.LeaseRevokeResponse, error) {
  42. resp, err := ls.le.LeaseRevoke(ctx, rr)
  43. if err != nil {
  44. return nil, rpctypes.ErrGRPCLeaseNotFound
  45. }
  46. ls.hdr.fill(resp.Header)
  47. return resp, nil
  48. }
  49. func (ls *LeaseServer) LeaseTimeToLive(ctx context.Context, rr *pb.LeaseTimeToLiveRequest) (*pb.LeaseTimeToLiveResponse, error) {
  50. resp, err := ls.le.LeaseTimeToLive(ctx, rr)
  51. if err != nil {
  52. return nil, rpctypes.ErrGRPCLeaseNotFound
  53. }
  54. ls.hdr.fill(resp.Header)
  55. return resp, nil
  56. }
  57. func (ls *LeaseServer) LeaseKeepAlive(stream pb.Lease_LeaseKeepAliveServer) error {
  58. for {
  59. req, err := stream.Recv()
  60. if err == io.EOF {
  61. return nil
  62. }
  63. if err != nil {
  64. return err
  65. }
  66. // Create header before we sent out the renew request.
  67. // This can make sure that the revision is strictly smaller or equal to
  68. // when the keepalive happened at the local server (when the local server is the leader)
  69. // or remote leader.
  70. // Without this, a lease might be revoked at rev 3 but client can see the keepalive succeeded
  71. // at rev 4.
  72. resp := &pb.LeaseKeepAliveResponse{ID: req.ID, Header: &pb.ResponseHeader{}}
  73. ls.hdr.fill(resp.Header)
  74. ttl, err := ls.le.LeaseRenew(lease.LeaseID(req.ID))
  75. if err == lease.ErrLeaseNotFound {
  76. err = nil
  77. ttl = 0
  78. }
  79. if err != nil {
  80. return err
  81. }
  82. resp.TTL = ttl
  83. err = stream.Send(resp)
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. }