github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/cache.go (about)

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