github.com/puellanivis/breton@v0.2.16/lib/files/cachefiles/context.go (about)

     1  package cachefiles
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  type (
     9  	keyExpiration struct{}
    10  	keyReentrance struct{}
    11  )
    12  
    13  // WithExpire returns a Context that includes information for the cache FileStore to expire buffers after the given timeout.
    14  func WithExpire(ctx context.Context, timeout time.Duration) context.Context {
    15  	return context.WithValue(ctx, keyExpiration{}, timeout)
    16  }
    17  
    18  // GetExpire returns the expiration timeout specified for the given Context.
    19  func GetExpire(ctx context.Context) (time.Duration, bool) {
    20  	timeout, ok := ctx.Value(keyExpiration{}).(time.Duration)
    21  
    22  	return timeout, ok
    23  }
    24  
    25  // isReentrant returns either:
    26  // a new sub-context with a reentrance key attached, along with a false bool,
    27  // or the same context input with a true bool.
    28  func isReentrant(ctx context.Context) (rctx context.Context, reentrant bool) {
    29  	if v := ctx.Value(keyReentrance{}); v != nil {
    30  		return ctx, true
    31  	}
    32  
    33  	return context.WithValue(ctx, keyReentrance{}, struct{}{}), false
    34  }