github.phpd.cn/hashicorp/packer@v1.3.2/packer/rpc/cache.go (about)

     1  package rpc
     2  
     3  import (
     4  	"log"
     5  	"net/rpc"
     6  
     7  	"github.com/hashicorp/packer/packer"
     8  )
     9  
    10  // An implementation of packer.Cache where the cache is actually executed
    11  // over an RPC connection.
    12  type cache struct {
    13  	client *rpc.Client
    14  }
    15  
    16  // CacheServer wraps a packer.Cache implementation and makes it exportable
    17  // as part of a Golang RPC server.
    18  type CacheServer struct {
    19  	cache packer.Cache
    20  }
    21  
    22  type CacheRLockResponse struct {
    23  	Path   string
    24  	Exists bool
    25  }
    26  
    27  func (c *cache) Lock(key string) (result string) {
    28  	if err := c.client.Call("Cache.Lock", key, &result); err != nil {
    29  		log.Printf("[ERR] Cache.Lock error: %s", err)
    30  		return
    31  	}
    32  
    33  	return
    34  }
    35  
    36  func (c *cache) RLock(key string) (string, bool) {
    37  	var result CacheRLockResponse
    38  	if err := c.client.Call("Cache.RLock", key, &result); err != nil {
    39  		log.Printf("[ERR] Cache.RLock error: %s", err)
    40  		return "", false
    41  	}
    42  
    43  	return result.Path, result.Exists
    44  }
    45  
    46  func (c *cache) Unlock(key string) {
    47  	if err := c.client.Call("Cache.Unlock", key, new(interface{})); err != nil {
    48  		log.Printf("[ERR] Cache.Unlock error: %s", err)
    49  		return
    50  	}
    51  }
    52  
    53  func (c *cache) RUnlock(key string) {
    54  	if err := c.client.Call("Cache.RUnlock", key, new(interface{})); err != nil {
    55  		log.Printf("[ERR] Cache.RUnlock error: %s", err)
    56  		return
    57  	}
    58  }
    59  
    60  func (c *CacheServer) Lock(key string, result *string) error {
    61  	*result = c.cache.Lock(key)
    62  	return nil
    63  }
    64  
    65  func (c *CacheServer) Unlock(key string, result *interface{}) error {
    66  	c.cache.Unlock(key)
    67  	return nil
    68  }
    69  
    70  func (c *CacheServer) RLock(key string, result *CacheRLockResponse) error {
    71  	path, exists := c.cache.RLock(key)
    72  	*result = CacheRLockResponse{path, exists}
    73  	return nil
    74  }
    75  
    76  func (c *CacheServer) RUnlock(key string, result *interface{}) error {
    77  	c.cache.RUnlock(key)
    78  	return nil
    79  }