github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/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 type CacheRLockResponse struct { 21 Path string 22 Exists bool 23 } 24 25 func (c *cache) Lock(key string) (result string) { 26 if err := c.client.Call("Cache.Lock", key, &result); err != nil { 27 panic(err) 28 } 29 30 return 31 } 32 33 func (c *cache) RLock(key string) (string, bool) { 34 var result CacheRLockResponse 35 if err := c.client.Call("Cache.RLock", key, &result); err != nil { 36 panic(err) 37 } 38 39 return result.Path, result.Exists 40 } 41 42 func (c *cache) Unlock(key string) { 43 if err := c.client.Call("Cache.Unlock", key, new(interface{})); err != nil { 44 panic(err) 45 } 46 } 47 48 func (c *cache) RUnlock(key string) { 49 if err := c.client.Call("Cache.RUnlock", key, new(interface{})); err != nil { 50 panic(err) 51 } 52 } 53 54 func (c *CacheServer) Lock(key string, result *string) error { 55 *result = c.cache.Lock(key) 56 return nil 57 } 58 59 func (c *CacheServer) Unlock(key string, result *interface{}) error { 60 c.cache.Unlock(key) 61 return nil 62 } 63 64 func (c *CacheServer) RLock(key string, result *CacheRLockResponse) error { 65 path, exists := c.cache.RLock(key) 66 *result = CacheRLockResponse{path, exists} 67 return nil 68 } 69 70 func (c *CacheServer) RUnlock(key string, result *interface{}) error { 71 c.cache.RUnlock(key) 72 return nil 73 }