lease.go 2.4 KB

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