WithLease attaches a lease on the context
(ctx context.Context, opts ...leases.Opt)
| 136 | |
| 137 | // WithLease attaches a lease on the context |
| 138 | func (ts *localTransferService) withLease(ctx context.Context, opts ...leases.Opt) (context.Context, func(context.Context) error, error) { |
| 139 | nop := func(context.Context) error { return nil } |
| 140 | |
| 141 | _, ok := leases.FromContext(ctx) |
| 142 | if ok { |
| 143 | return ctx, nop, nil |
| 144 | } |
| 145 | |
| 146 | ls := ts.config.Leases |
| 147 | if ls == nil { |
| 148 | return ctx, nop, nil |
| 149 | } |
| 150 | |
| 151 | if len(opts) == 0 { |
| 152 | // Use default lease configuration if no options provided |
| 153 | opts = []leases.Opt{ |
| 154 | leases.WithRandomID(), |
| 155 | leases.WithExpiration(24 * time.Hour), |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | l, err := ls.Create(ctx, opts...) |
| 160 | if err != nil { |
| 161 | return ctx, nop, err |
| 162 | } |
| 163 | |
| 164 | ctx = leases.WithLease(ctx, l.ID) |
| 165 | return ctx, func(ctx context.Context) error { |
| 166 | return ls.Delete(ctx, l) |
| 167 | }, nil |
| 168 | } |
| 169 | |
| 170 | type TransferConfig struct { |
| 171 | // Leases manager is used to create leases during operations if none, exists |
no test coverage detected