From 7cc75c5f3829bb8d20cb250b122a91e8bbbbe68f Mon Sep 17 00:00:00 2001 From: Calvin Leung Huang Date: Fri, 1 Mar 2019 11:20:36 -0800 Subject: [PATCH] agent/caching: refactor ContextInfo (#6311) * agent/caching: refactor ContextInfo * use NewContextInfo in NewLeaseCache --- command/agent/cache/cachememdb/index.go | 23 +++++++++++---- command/agent/cache/lease_cache.go | 37 +++++++------------------ 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/command/agent/cache/cachememdb/index.go b/command/agent/cache/cachememdb/index.go index 4d932ca4f2..ae822cf1d0 100644 --- a/command/agent/cache/cachememdb/index.go +++ b/command/agent/cache/cachememdb/index.go @@ -2,12 +2,6 @@ package cachememdb import "context" -type ContextInfo struct { - Ctx context.Context - CancelFunc context.CancelFunc - DoneCh chan struct{} -} - // Index holds the response to be cached along with multiple other values that // serve as pointers to refer back to this index. type Index struct { @@ -95,3 +89,20 @@ func validIndexName(indexName string) bool { } return true } + +type ContextInfo struct { + Ctx context.Context + CancelFunc context.CancelFunc + DoneCh chan struct{} +} + +func NewContextInfo(ctx context.Context) *ContextInfo { + if ctx == nil { + return nil + } + + ctxInfo := new(ContextInfo) + ctxInfo.Ctx, ctxInfo.CancelFunc = context.WithCancel(ctx) + ctxInfo.DoneCh = make(chan struct{}) + return ctxInfo +} diff --git a/command/agent/cache/lease_cache.go b/command/agent/cache/lease_cache.go index 0705171d25..bccf695f20 100644 --- a/command/agent/cache/lease_cache.go +++ b/command/agent/cache/lease_cache.go @@ -68,7 +68,7 @@ type LeaseCache struct { proxier Proxier logger hclog.Logger db *cachememdb.CacheMemDB - baseCtxInfo *ContextInfo + baseCtxInfo *cachememdb.ContextInfo } // LeaseCacheConfig is the configuration for initializing a new @@ -80,13 +80,6 @@ type LeaseCacheConfig struct { Logger hclog.Logger } -// ContextInfo holds a derived context and cancelFunc pair. -type ContextInfo struct { - Ctx context.Context - CancelFunc context.CancelFunc - DoneCh chan struct{} -} - // NewLeaseCache creates a new instance of a LeaseCache. func NewLeaseCache(conf *LeaseCacheConfig) (*LeaseCache, error) { if conf == nil { @@ -107,11 +100,7 @@ func NewLeaseCache(conf *LeaseCacheConfig) (*LeaseCache, error) { } // Create a base context for the lease cache layer - baseCtx, baseCancelFunc := context.WithCancel(conf.BaseContext) - baseCtxInfo := &ContextInfo{ - Ctx: baseCtx, - CancelFunc: baseCancelFunc, - } + baseCtxInfo := cachememdb.NewContextInfo(conf.BaseContext) return &LeaseCache{ client: conf.Client, @@ -215,7 +204,7 @@ func (c *LeaseCache) Send(ctx context.Context, req *SendRequest) (*SendResponse, return resp, nil } - var renewCtxInfo *ContextInfo + var renewCtxInfo *cachememdb.ContextInfo switch { case secret.LeaseID != "": c.logger.Debug("processing lease response", "path", req.Request.URL.Path, "method", req.Request.Method) @@ -231,10 +220,7 @@ func (c *LeaseCache) Send(ctx context.Context, req *SendRequest) (*SendResponse, } // Derive a context for renewal using the token's context - newCtxInfo := new(ContextInfo) - newCtxInfo.Ctx, newCtxInfo.CancelFunc = context.WithCancel(entry.RenewCtxInfo.Ctx) - newCtxInfo.DoneCh = make(chan struct{}) - renewCtxInfo = newCtxInfo + renewCtxInfo = cachememdb.NewContextInfo(entry.RenewCtxInfo.Ctx) index.Lease = secret.LeaseID index.LeaseToken = req.Token @@ -317,14 +303,11 @@ func (c *LeaseCache) Send(ctx context.Context, req *SendRequest) (*SendResponse, return resp, nil } -func (c *LeaseCache) createCtxInfo(ctx context.Context) *ContextInfo { +func (c *LeaseCache) createCtxInfo(ctx context.Context) *cachememdb.ContextInfo { if ctx == nil { ctx = c.baseCtxInfo.Ctx } - ctxInfo := new(ContextInfo) - ctxInfo.Ctx, ctxInfo.CancelFunc = context.WithCancel(ctx) - ctxInfo.DoneCh = make(chan struct{}) - return ctxInfo + return cachememdb.NewContextInfo(ctx) } func (c *LeaseCache) startRenewing(ctx context.Context, index *cachememdb.Index, req *SendRequest, secret *api.Secret) { @@ -495,7 +478,7 @@ func (c *LeaseCache) handleCacheClear(ctx context.Context, clearType string, cle return nil } - c.logger.Debug("cancelling context of index attached to token") + c.logger.Debug("canceling context of index attached to token") index.RenewCtxInfo.CancelFunc() @@ -509,19 +492,19 @@ func (c *LeaseCache) handleCacheClear(ctx context.Context, clearType string, cle return nil } - c.logger.Debug("cancelling context of index attached to accessor") + c.logger.Debug("canceling context of index attached to accessor") index.RenewCtxInfo.CancelFunc() case "all": // Cancel the base context which triggers all the goroutines to // stop and evict entries from cache. - c.logger.Debug("cancelling base context") + c.logger.Debug("canceling base context") c.baseCtxInfo.CancelFunc() // Reset the base context baseCtx, baseCancel := context.WithCancel(ctx) - c.baseCtxInfo = &ContextInfo{ + c.baseCtxInfo = &cachememdb.ContextInfo{ Ctx: baseCtx, CancelFunc: baseCancel, }