...

Source file src/github.com/Microsoft/hcsshim/cmd/ncproxy/computeagent_cache.go

Documentation: github.com/Microsoft/hcsshim/cmd/ncproxy

     1  //go:build windows
     2  
     3  package main
     4  
     5  import (
     6  	"sync"
     7  
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  var errNilCache = errors.New("cannot access a nil cache")
    12  
    13  type computeAgentCache struct {
    14  	// lock for synchronizing read/write access to `cache`
    15  	rw sync.RWMutex
    16  	// mapping of container ID to shim compute agent ttrpc service
    17  	cache map[string]*computeAgentClient
    18  }
    19  
    20  func newComputeAgentCache() *computeAgentCache {
    21  	return &computeAgentCache{
    22  		cache: make(map[string]*computeAgentClient),
    23  	}
    24  }
    25  
    26  func (c *computeAgentCache) getAllAndClear() ([]*computeAgentClient, error) {
    27  	// set c.cache to nil first so that subsequent attempts to reads and writes
    28  	// return an error
    29  	c.rw.Lock()
    30  	cacheCopy := c.cache
    31  	c.cache = nil
    32  	c.rw.Unlock()
    33  
    34  	if cacheCopy == nil {
    35  		return nil, errNilCache
    36  	}
    37  
    38  	results := []*computeAgentClient{}
    39  	for _, agent := range cacheCopy {
    40  		results = append(results, agent)
    41  	}
    42  	return results, nil
    43  }
    44  
    45  func (c *computeAgentCache) get(cid string) (*computeAgentClient, error) {
    46  	c.rw.RLock()
    47  	defer c.rw.RUnlock()
    48  	if c.cache == nil {
    49  		return nil, errNilCache
    50  	}
    51  	result := c.cache[cid]
    52  	return result, nil
    53  }
    54  
    55  func (c *computeAgentCache) put(cid string, agent *computeAgentClient) error {
    56  	c.rw.Lock()
    57  	defer c.rw.Unlock()
    58  	if c.cache == nil {
    59  		return errNilCache
    60  	}
    61  	c.cache[cid] = agent
    62  	return nil
    63  }
    64  
    65  func (c *computeAgentCache) getAndDelete(cid string) (*computeAgentClient, error) {
    66  	c.rw.Lock()
    67  	defer c.rw.Unlock()
    68  	if c.cache == nil {
    69  		return nil, errNilCache
    70  	}
    71  	result := c.cache[cid]
    72  	delete(c.cache, cid)
    73  	return result, nil
    74  }
    75  

View as plain text