Add locking around base context (#6321)

Got offline 👍 from Calvin.
This commit is contained in:
Vishal Nayak 2019-03-01 20:30:14 -05:00 committed by GitHub
parent d514ff573a
commit ce42e9ea1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,6 +12,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"sync"
"github.com/hashicorp/errwrap" "github.com/hashicorp/errwrap"
hclog "github.com/hashicorp/go-hclog" hclog "github.com/hashicorp/go-hclog"
@ -69,6 +70,7 @@ type LeaseCache struct {
logger hclog.Logger logger hclog.Logger
db *cachememdb.CacheMemDB db *cachememdb.CacheMemDB
baseCtxInfo *cachememdb.ContextInfo baseCtxInfo *cachememdb.ContextInfo
l *sync.RWMutex
} }
// LeaseCacheConfig is the configuration for initializing a new // LeaseCacheConfig is the configuration for initializing a new
@ -108,6 +110,7 @@ func NewLeaseCache(conf *LeaseCacheConfig) (*LeaseCache, error) {
logger: conf.Logger, logger: conf.Logger,
db: db, db: db,
baseCtxInfo: baseCtxInfo, baseCtxInfo: baseCtxInfo,
l: &sync.RWMutex{},
}, nil }, nil
} }
@ -305,7 +308,9 @@ func (c *LeaseCache) Send(ctx context.Context, req *SendRequest) (*SendResponse,
func (c *LeaseCache) createCtxInfo(ctx context.Context) *cachememdb.ContextInfo { func (c *LeaseCache) createCtxInfo(ctx context.Context) *cachememdb.ContextInfo {
if ctx == nil { if ctx == nil {
c.l.RLock()
ctx = c.baseCtxInfo.Ctx ctx = c.baseCtxInfo.Ctx
c.l.RUnlock()
} }
return cachememdb.NewContextInfo(ctx) return cachememdb.NewContextInfo(ctx)
} }
@ -500,14 +505,15 @@ func (c *LeaseCache) handleCacheClear(ctx context.Context, clearType string, cle
// Cancel the base context which triggers all the goroutines to // Cancel the base context which triggers all the goroutines to
// stop and evict entries from cache. // stop and evict entries from cache.
c.logger.Debug("canceling base context") c.logger.Debug("canceling base context")
c.l.Lock()
c.baseCtxInfo.CancelFunc() c.baseCtxInfo.CancelFunc()
// Reset the base context // Reset the base context
baseCtx, baseCancel := context.WithCancel(ctx) baseCtx, baseCancel := context.WithCancel(ctx)
c.baseCtxInfo = &cachememdb.ContextInfo{ c.baseCtxInfo = &cachememdb.ContextInfo{
Ctx: baseCtx, Ctx: baseCtx,
CancelFunc: baseCancel, CancelFunc: baseCancel,
} }
c.l.Unlock()
// Reset the memdb instance // Reset the memdb instance
if err := c.db.Flush(); err != nil { if err := c.db.Flush(); err != nil {