github.com/demonoid81/containerd@v1.3.4/lease.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package containerd 18 19 import ( 20 "context" 21 "time" 22 23 "github.com/containerd/containerd/leases" 24 ) 25 26 // WithLease attaches a lease on the context 27 func (c *Client) WithLease(ctx context.Context) (context.Context, func(context.Context) error, error) { 28 _, ok := leases.FromContext(ctx) 29 if ok { 30 return ctx, func(context.Context) error { 31 return nil 32 }, nil 33 } 34 35 ls := c.LeasesService() 36 37 l, err := ls.Create(ctx, leases.WithRandomID(), leases.WithExpiration(24*time.Hour)) 38 if err != nil { 39 return nil, nil, err 40 } 41 42 ctx = leases.WithLease(ctx, l.ID) 43 return ctx, func(ctx context.Context) error { 44 return ls.Delete(ctx, l) 45 }, nil 46 }