lease.go 2.8 KB

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