From 7252b779d7818ef769a1320fab2745b8a450ddb6 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Sat, 4 Jun 2016 07:53:07 -0700 Subject: [PATCH] gcs tweaks for performance --- blob/gcs.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/blob/gcs.go b/blob/gcs.go index c23efa3ec..533136f88 100644 --- a/blob/gcs.go +++ b/blob/gcs.go @@ -72,7 +72,7 @@ func (gcs *gcsStorage) GetBlock(b string) ([]byte, error) { v, err := gcs.objectsService.Get(gcs.BucketName, gcs.getObjectNameString(b)).Download() if err != nil { if err, ok := err.(*googleapi.Error); ok { - if err.Code == 404 { + if err.Code == http.StatusNotFound { return nil, ErrBlockNotFound } } @@ -86,23 +86,25 @@ func (gcs *gcsStorage) GetBlock(b string) ([]byte, error) { } func (gcs *gcsStorage) PutBlock(b string, data io.ReadCloser, options PutOptions) error { + defer data.Close() + object := gcsclient.Object{ Name: gcs.getObjectNameString(b), } - defer data.Close() - t0 := time.Now() call := gcs.objectsService.Insert(gcs.BucketName, &object).Media(data) if !options.Overwrite { + if ok, _ := gcs.BlockExists(b); ok { + return nil + } + call = call.IfGenerationMatch(0) } _, err := call.Do() - dt := time.Since(t0) - log.Printf("PutBlock completed in %v and returned %v", dt, err) if err != nil { if err, ok := err.(*googleapi.Error); ok { - if err.Code == 412 { + if err.Code == http.StatusPreconditionFailed { // Condition not met indicates that the block already exists. return nil } @@ -206,6 +208,8 @@ func saveToken(file string, token *oauth2.Token) { func NewGCSStorage(options *GCSStorageOptions) (Storage, error) { ctx := context.TODO() + //ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{}) + gcs := &gcsStorage{ GCSStorageOptions: *options, } @@ -226,7 +230,7 @@ func NewGCSStorage(options *GCSStorageOptions) (Storage, error) { var err error if !gcs.IgnoreDefaultCredentials { - client, _ = google.DefaultClient(context.TODO(), scope) + client, _ = google.DefaultClient(ctx, scope) } if client == nil {