Skip to content

Commit

Permalink
Merge pull request #16 from VoycerAG/hardening_performance
Browse files Browse the repository at this point in the history
Write concern for new images
  • Loading branch information
sharpner committed Jan 14, 2016
2 parents e06196e + e06ea84 commit 09ef95b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 8 additions & 1 deletion server/gridfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"log"
"time"

"gopkg.in/mgo.v2"
Expand Down Expand Up @@ -168,7 +169,11 @@ func (g GridfsStorage) StoreChildImage(
original Cacheable,
entry *Entry,
) (Cacheable, error) {
gridfs := g.Connection.DB(database).GridFS("fs")
con := g.Connection.Copy()
defer con.Close()
con.EnsureSafe(&mgo.Safe{W: 1, J: true})

gridfs := con.DB(database).GridFS("fs")
targetfile, err := gridfs.Create(getRandomFilename(imageFormat))

if err != nil {
Expand All @@ -180,6 +185,8 @@ func (g GridfsStorage) StoreChildImage(
_, err = io.Copy(targetfile, reader)

if err != nil {
log.Printf("Error for filename %s with size %dx%d\n", original.Name(), entry.Width, entry.Height)
log.Printf("Could not write file completely, cleaning %s\n", targetfile.Name())
targetfile.Abort()
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func imageHandler(
return
}

// we found a image but did not want resizing
// we found an image but did not want resizing
if found {
if !isModified(foundImage, &r.Header) {
w.WriteHeader(http.StatusNotModified)
Expand Down Expand Up @@ -201,7 +201,7 @@ func imageHandler(
controller, err := paint.NewController(foundImage.Data(), customResizers)

if err != nil {
log.Printf("%d image could not be decoded Reason %s.\n", http.StatusNotFound, err.Error())
log.Printf("%d image could not be decoded. Reason: [%s].\n", http.StatusNotFound, err.Error())
w.WriteHeader(http.StatusNotFound)
return
}
Expand All @@ -218,11 +218,12 @@ func imageHandler(
buffer := bufio.NewWriter(&b)
controller.Encode(buffer)
buffer.Flush()
data := b.Bytes()

targetfile, err := storage.StoreChildImage(
requestConfig.Database,
controller.Format(),
bytes.NewReader(b.Bytes()),
bytes.NewReader(data),
controller.Image().Bounds().Dx(),
controller.Image().Bounds().Dy(),
foundImage,
Expand All @@ -235,8 +236,8 @@ func imageHandler(
return
}

w.Write(b.Bytes())
setCacheHeaders(targetfile, w)
w.Write(data)

log.Printf("%d image succesfully resized and returned.\n", http.StatusOK)
}
Expand Down

0 comments on commit 09ef95b

Please sign in to comment.