github.com/argoproj/argo-cd/v3@v3.2.1/util/buffered_context/buffered_context.go (about)

     1  package buffered_context
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  // WithEarlierDeadline creates a new context with a deadline before the given context's deadline. The buffer parameter
     9  // determines how much earlier the new deadline is. Returns the new context (with the original context as its parent)
    10  // and a pointer to a cancel function for the new context.
    11  //
    12  // If the given context doesn't have a deadline, return the original context unchanged and a do-nothing cancel function.
    13  func WithEarlierDeadline(originalCtx context.Context, buffer time.Duration) (context.Context, context.CancelFunc) {
    14  	var cancelFunc context.CancelFunc = func() {}
    15  	bufferedCtx := originalCtx
    16  	if deadline, ok := originalCtx.Deadline(); ok {
    17  		bufferedCtx, cancelFunc = context.WithDeadline(originalCtx, deadline.Add(-1*buffer))
    18  	}
    19  	return bufferedCtx, cancelFunc
    20  }