@@ -39,11 +39,11 @@ const (
3939
4040// Config contains a cloning configuration.
4141type Config struct {
42- MaxIdleMinutes uint `yaml:"maxIdleMinutes"`
43- AccessHost string `yaml:"accessHost"`
44- ProtectionLeaseDurationMinutes uint `yaml:"protectionLeaseDurationMinutes"`
45- ProtectionRenewalDurationMinutes uint `yaml:"protectionRenewalDurationMinutes "`
46- ProtectionExpiryWarningMinutes uint `yaml:"protectionExpiryWarningMinutes"`
42+ MaxIdleMinutes uint `yaml:"maxIdleMinutes"`
43+ AccessHost string `yaml:"accessHost"`
44+ ProtectionLeaseDurationMinutes uint `yaml:"protectionLeaseDurationMinutes"`
45+ ProtectionMaxDurationMinutes uint `yaml:"protectionMaxDurationMinutes "`
46+ ProtectionExpiryWarningMinutes uint `yaml:"protectionExpiryWarningMinutes"`
4747}
4848
4949// Base provides cloning service.
@@ -181,7 +181,7 @@ func (c *Base) CreateClone(cloneRequest *types.CloneCreateRequest) (*models.Clon
181181
182182 var protectedTill * models.LocalTime
183183 if cloneRequest .Protected {
184- protectedTill = c .calculateInitialProtectionTime ( )
184+ protectedTill = c .calculateProtectionTime ( cloneRequest . ProtectionDurationMinutes )
185185 }
186186
187187 clone := & models.Clone {
@@ -280,10 +280,10 @@ func (c *Base) fillCloneSession(cloneID string, session *resources.Session) {
280280 clone .DB .Host , clone .DB .Port , clone .DB .Username , clone .DB .DBName )
281281
282282 clone .Metadata = models.CloneMetadata {
283- CloningTime : w .TimeStartedAt .Sub (w .TimeCreatedAt ).Seconds (),
284- MaxIdleMinutes : c .config .MaxIdleMinutes ,
285- ProtectionLeaseDurationMinutes : c .config .ProtectionLeaseDurationMinutes ,
286- ProtectionRenewalDurationMinutes : c .config .ProtectionRenewalDurationMinutes ,
283+ CloningTime : w .TimeStartedAt .Sub (w .TimeCreatedAt ).Seconds (),
284+ MaxIdleMinutes : c .config .MaxIdleMinutes ,
285+ ProtectionLeaseDurationMinutes : c .config .ProtectionLeaseDurationMinutes ,
286+ ProtectionMaxDurationMinutes : c .config .ProtectionMaxDurationMinutes ,
287287 }
288288}
289289
@@ -464,16 +464,12 @@ func (c *Base) UpdateClone(id string, patch types.CloneUpdateRequest) (*models.C
464464
465465 c .cloneMutex .Lock ()
466466
467- if patch .RenewLease && w .Clone .Protected {
468- w .Clone .ProtectedTill = c .calculateRenewalTime ()
469- } else if patch .Protected != w .Clone .Protected {
470- w .Clone .Protected = patch .Protected
471-
472- if patch .Protected {
473- w .Clone .ProtectedTill = c .calculateInitialProtectionTime ()
474- } else {
475- w .Clone .ProtectedTill = nil
476- }
467+ if patch .Protected {
468+ w .Clone .Protected = true
469+ w .Clone .ProtectedTill = c .calculateProtectionTime (patch .ProtectionDurationMinutes )
470+ } else {
471+ w .Clone .Protected = false
472+ w .Clone .ProtectedTill = nil
477473 }
478474
479475 clone = w .Clone
@@ -484,29 +480,34 @@ func (c *Base) UpdateClone(id string, patch types.CloneUpdateRequest) (*models.C
484480 return clone , nil
485481}
486482
487- // calculateInitialProtectionTime calculates the protection expiry time for a new protection lease.
488- func (c * Base ) calculateInitialProtectionTime () * models.LocalTime {
489- if c .config .ProtectionLeaseDurationMinutes == 0 {
490- return nil
483+ // calculateProtectionTime calculates the protection expiry time based on the requested duration.
484+ // If durationMinutes is nil, uses the default from config.
485+ // If durationMinutes is 0, returns nil (infinite protection) unless max duration is configured.
486+ // If max duration is configured, the duration is capped at the max.
487+ func (c * Base ) calculateProtectionTime (durationMinutes * uint ) * models.LocalTime {
488+ var minutes uint
489+
490+ if durationMinutes != nil {
491+ minutes = * durationMinutes
492+ } else {
493+ minutes = c .config .ProtectionLeaseDurationMinutes
491494 }
492495
493- expiry := time . Now (). Add ( time . Duration ( c .config .ProtectionLeaseDurationMinutes ) * time . Minute )
496+ maxMinutes := c .config .ProtectionMaxDurationMinutes
494497
495- return models .NewLocalTime (expiry )
496- }
497-
498- // calculateRenewalTime calculates the protection expiry time for a lease renewal.
499- func (c * Base ) calculateRenewalTime () * models.LocalTime {
500- renewalMinutes := c .config .ProtectionRenewalDurationMinutes
501- if renewalMinutes == 0 {
502- renewalMinutes = c .config .ProtectionLeaseDurationMinutes
498+ if minutes == 0 {
499+ if maxMinutes > 0 {
500+ minutes = maxMinutes
501+ } else {
502+ return nil
503+ }
503504 }
504505
505- if renewalMinutes == 0 {
506- return nil
506+ if maxMinutes > 0 && minutes > maxMinutes {
507+ minutes = maxMinutes
507508 }
508509
509- expiry := time .Now ().Add (time .Duration (renewalMinutes ) * time .Minute )
510+ expiry := time .Now ().Add (time .Duration (minutes ) * time .Minute )
510511
511512 return models .NewLocalTime (expiry )
512513}
@@ -644,11 +645,11 @@ func (c *Base) ResetClone(cloneID string, resetOptions types.ResetCloneRequest)
644645func (c * Base ) GetCloningState () models.Cloning {
645646 clones := c .GetClones ()
646647 cloning := models.Cloning {
647- ExpectedCloningTime : c .getExpectedCloningTime (),
648- Clones : clones ,
649- NumClones : uint64 (len (clones )),
650- ProtectionLeaseDurationMinutes : c .config .ProtectionLeaseDurationMinutes ,
651- ProtectionRenewalDurationMinutes : c .config .ProtectionRenewalDurationMinutes ,
648+ ExpectedCloningTime : c .getExpectedCloningTime (),
649+ Clones : clones ,
650+ NumClones : uint64 (len (clones )),
651+ ProtectionLeaseDurationMinutes : c .config .ProtectionLeaseDurationMinutes ,
652+ ProtectionMaxDurationMinutes : c .config .ProtectionMaxDurationMinutes ,
652653 }
653654
654655 return cloning
0 commit comments