|
|
@@ -44,6 +44,21 @@ type LeaseKeepAliveResponse struct {
|
|
|
TTL int64
|
|
|
}
|
|
|
|
|
|
+// LeaseTimeToLiveResponse is used to convert the protobuf lease timetolive response.
|
|
|
+type LeaseTimeToLiveResponse struct {
|
|
|
+ *pb.ResponseHeader
|
|
|
+ ID LeaseID `json:"id"`
|
|
|
+
|
|
|
+ // TTL is the remaining TTL in seconds for the lease; the lease will expire in under TTL+1 seconds.
|
|
|
+ TTL int64 `json:"ttl"`
|
|
|
+
|
|
|
+ // GrantedTTL is the initial granted time in seconds upon lease creation/renewal.
|
|
|
+ GrantedTTL int64 `json:"granted-ttl"`
|
|
|
+
|
|
|
+ // Keys is the list of keys attached to this lease.
|
|
|
+ Keys [][]byte `json:"keys"`
|
|
|
+}
|
|
|
+
|
|
|
const (
|
|
|
// defaultTTL is the assumed lease TTL used for the first keepalive
|
|
|
// deadline before the actual TTL is known to the client.
|
|
|
@@ -61,6 +76,9 @@ type Lease interface {
|
|
|
// Revoke revokes the given lease.
|
|
|
Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error)
|
|
|
|
|
|
+ // TimeToLive retrieves the lease information of the given lease ID.
|
|
|
+ TimeToLive(ctx context.Context, id LeaseID, opts ...LeaseOption) (*LeaseTimeToLiveResponse, error)
|
|
|
+
|
|
|
// KeepAlive keeps the given lease alive forever.
|
|
|
KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error)
|
|
|
|
|
|
@@ -170,6 +188,30 @@ func (l *lessor) Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (l *lessor) TimeToLive(ctx context.Context, id LeaseID, opts ...LeaseOption) (*LeaseTimeToLiveResponse, error) {
|
|
|
+ cctx, cancel := context.WithCancel(ctx)
|
|
|
+ done := cancelWhenStop(cancel, l.stopCtx.Done())
|
|
|
+ defer close(done)
|
|
|
+
|
|
|
+ for {
|
|
|
+ r := toLeaseTimeToLiveRequest(id, opts...)
|
|
|
+ resp, err := l.remote.LeaseTimeToLive(cctx, r)
|
|
|
+ if err == nil {
|
|
|
+ gresp := &LeaseTimeToLiveResponse{
|
|
|
+ ResponseHeader: resp.GetHeader(),
|
|
|
+ ID: LeaseID(resp.ID),
|
|
|
+ TTL: resp.TTL,
|
|
|
+ GrantedTTL: resp.GrantedTTL,
|
|
|
+ Keys: resp.Keys,
|
|
|
+ }
|
|
|
+ return gresp, nil
|
|
|
+ }
|
|
|
+ if isHaltErr(cctx, err) {
|
|
|
+ return nil, toErr(cctx, err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func (l *lessor) KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error) {
|
|
|
ch := make(chan *LeaseKeepAliveResponse, leaseResponseChSize)
|
|
|
|