mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-30 10:12:35 +00:00 
			
		
		
		
	Respect increment value in grace period calculations (api/LifetimeWatcher) (#14836)
This commit is contained in:
		 Anton Averchenkov
					Anton Averchenkov
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							18ee7d90be
						
					
				
				
					commit
					9c6d25ad16
				
			| @@ -113,7 +113,9 @@ type LifetimeWatcherInput struct { | ||||
|  | ||||
| 	// The new TTL, in seconds, that should be set on the lease. The TTL set | ||||
| 	// here may or may not be honored by the vault server, based on Vault | ||||
| 	// configuration or any associated max TTL values. | ||||
| 	// configuration or any associated max TTL values. If specified, the | ||||
| 	// minimum of this value and the remaining lease duration will be used | ||||
| 	// for grace period calculations. | ||||
| 	Increment int | ||||
|  | ||||
| 	// RenewBehavior controls what happens when a renewal errors or the | ||||
| @@ -257,7 +259,7 @@ func (r *LifetimeWatcher) doRenewWithOptions(tokenMode bool, nonRenewable bool, | ||||
|  | ||||
| 	initialTime := time.Now() | ||||
| 	priorDuration := time.Duration(initLeaseDuration) * time.Second | ||||
| 	r.calculateGrace(priorDuration) | ||||
| 	r.calculateGrace(priorDuration, time.Duration(r.increment)*time.Second) | ||||
| 	var errorBackoff backoff.BackOff | ||||
|  | ||||
| 	for { | ||||
| @@ -345,7 +347,7 @@ func (r *LifetimeWatcher) doRenewWithOptions(tokenMode bool, nonRenewable bool, | ||||
| 			// extending. Once it stops extending, we've hit the max and need to | ||||
| 			// rely on the grace duration. | ||||
| 			if remainingLeaseDuration > priorDuration { | ||||
| 				r.calculateGrace(remainingLeaseDuration) | ||||
| 				r.calculateGrace(remainingLeaseDuration, time.Duration(r.increment)*time.Second) | ||||
| 			} | ||||
| 			priorDuration = remainingLeaseDuration | ||||
|  | ||||
| @@ -373,16 +375,21 @@ func (r *LifetimeWatcher) doRenewWithOptions(tokenMode bool, nonRenewable bool, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // calculateGrace calculates the grace period based on a reasonable set of | ||||
| // assumptions given the total lease time; it also adds some jitter to not have | ||||
| // clients be in sync. | ||||
| func (r *LifetimeWatcher) calculateGrace(leaseDuration time.Duration) { | ||||
| 	if leaseDuration <= 0 { | ||||
| // calculateGrace calculates the grace period based on the minimum of the | ||||
| // remaining lease duration and the token increment value; it also adds some | ||||
| // jitter to not have clients be in sync. | ||||
| func (r *LifetimeWatcher) calculateGrace(leaseDuration, increment time.Duration) { | ||||
| 	minDuration := leaseDuration | ||||
| 	if minDuration > increment && increment > 0 { | ||||
| 		minDuration = increment | ||||
| 	} | ||||
|  | ||||
| 	if minDuration <= 0 { | ||||
| 		r.grace = 0 | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	leaseNanos := float64(leaseDuration.Nanoseconds()) | ||||
| 	leaseNanos := float64(minDuration.Nanoseconds()) | ||||
| 	jitterMax := 0.1 * leaseNanos | ||||
|  | ||||
| 	// For a given lease duration, we want to allow 80-90% of that to elapse, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user