Merge pull request #621 from vadmeste/pr_out_forbid_upload_of_file_bigger_than_memory_backend

This commit is contained in:
Harshavardhana 2015-05-28 14:09:41 -07:00
commit 56566d055e
3 changed files with 25 additions and 8 deletions

View File

@ -18,6 +18,10 @@ package drivers
import "fmt"
// InternalError - generic internal error
type InternalError struct {
}
// BackendError - generic disk backend error
type BackendError struct {
Path string
@ -127,6 +131,11 @@ func EmbedError(bucket, object string, err error) ImplementationError {
}
}
// Return string an error formatted as the given text
func (e InternalError) Error() string {
return "Internal error occured"
}
// Return string an error formatted as the given text
func (e ObjectNotFound) Error() string {
return "Object not Found: " + e.Bucket + "#" + e.Object

View File

@ -271,8 +271,11 @@ func (memory *memoryDriver) createObject(bucket, key, contentType, expectedMD5Su
totalLength := len(readBytes)
memory.lock.Lock()
memory.objects.Set(objectKey, readBytes)
ok := memory.objects.Set(objectKey, readBytes)
memory.lock.Unlock()
if !ok {
return "", iodine.New(drivers.InternalError{}, nil)
}
// setting up for de-allocation
readBytes = nil

View File

@ -115,23 +115,28 @@ func (r *Cache) Get(key string) (interface{}, bool) {
}
// Set will persist a value to the cache
func (r *Cache) Set(key string, value interface{}) {
func (r *Cache) Set(key string, value interface{}) bool {
r.Lock()
// remove random key if only we reach the maxSize threshold,
// if not assume infinite memory
defer r.Unlock()
valueLen := uint64(len(value.([]byte)))
if r.maxSize > 0 {
// check if the size of the object is not bigger than the
// capacity of the cache
if valueLen > r.maxSize {
return false
}
// remove random key if only we reach the maxSize threshold
for key := range r.items {
for (r.currentSize + uint64(len(value.([]byte)))) > r.maxSize {
for (r.currentSize + valueLen) > r.maxSize {
r.Delete(key)
}
break
}
}
r.items[key] = value
r.currentSize += uint64(len(value.([]byte)))
r.currentSize += valueLen
r.updatedAt[key] = time.Now()
r.Unlock()
return
return true
}
// Expire expires keys which have expired