lease.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 {
  48. return nil, togRPCError(err)
  49. }
  50. ls.hdr.fill(resp.Header)
  51. return resp, nil
  52. }
  53. func (ls *LeaseServer) LeaseKeepAlive(stream pb.Lease_LeaseKeepAliveServer) error {
  54. for {
  55. req, err := stream.Recv()
  56. if err == io.EOF {
  57. return nil
  58. }
  59. if err != nil {
  60. return err
  61. }
  62. // Create header before we sent out the renew request.
  63. // This can make sure that the revision is strictly smaller or equal to
  64. // when the keepalive happened at the local server (when the local server is the leader)
  65. // or remote leader.
  66. // Without this, a lease might be revoked at rev 3 but client can see the keepalive succeeded
  67. // at rev 4.
  68. resp := &pb.LeaseKeepAliveResponse{ID: req.ID, Header: &pb.ResponseHeader{}}
  69. ls.hdr.fill(resp.Header)
  70. ttl, err := ls.le.LeaseRenew(lease.LeaseID(req.ID))
  71. if err == lease.ErrLeaseNotFound {
  72. err = nil
  73. ttl = 0
  74. }
  75. if err != nil {
  76. return err
  77. }
  78. resp.TTL = ttl
  79. err = stream.Send(resp)
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. }