Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: common/gcloud/gs/writer.go

Issue 2951393002: [errors] de-specialize Transient in favor of Tags. (Closed)
Patch Set: more refactor Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package gs 5 package gs
6 6
7 import ( 7 import (
8 "io" 8 "io"
9 "time" 9 "time"
10 10
11 gs "cloud.google.com/go/storage" 11 gs "cloud.google.com/go/storage"
12 "github.com/luci/luci-go/common/errors"
13 log "github.com/luci/luci-go/common/logging" 12 log "github.com/luci/luci-go/common/logging"
14 "github.com/luci/luci-go/common/retry" 13 "github.com/luci/luci-go/common/retry"
14 "github.com/luci/luci-go/common/retry/transient"
15 "golang.org/x/net/context" 15 "golang.org/x/net/context"
16 ) 16 )
17 17
18 // Writer is an augmented io.WriteCloser instance. 18 // Writer is an augmented io.WriteCloser instance.
19 type Writer interface { 19 type Writer interface {
20 io.WriteCloser 20 io.WriteCloser
21 21
22 // Count returns the number of bytes written by the object. 22 // Count returns the number of bytes written by the object.
23 Count() int64 23 Count() int64
24 } 24 }
(...skipping 10 matching lines...) Expand all
35 relpath string 35 relpath string
36 count int64 36 count int64
37 } 37 }
38 38
39 // Write writes data with exponenital backoff/retry. 39 // Write writes data with exponenital backoff/retry.
40 func (w *prodWriter) Write(d []byte) (a int, err error) { 40 func (w *prodWriter) Write(d []byte) (a int, err error) {
41 if w.Writer == nil { 41 if w.Writer == nil {
42 w.Writer = w.client.baseClient.Bucket(w.bucket).Object(w.relpath ).NewWriter(w) 42 w.Writer = w.client.baseClient.Bucket(w.bucket).Object(w.relpath ).NewWriter(w)
43 } 43 }
44 44
45 » err = retry.Retry(w, retry.TransientOnly(retry.Default), func() (ierr er ror) { 45 » err = retry.Retry(w, transient.Only(retry.Default), func() (ierr error) {
46 a, ierr = w.Writer.Write(d) 46 a, ierr = w.Writer.Write(d)
47 47
48 // Assume all Write errors are transient. 48 // Assume all Write errors are transient.
49 » » ierr = errors.WrapTransient(ierr) 49 » » ierr = transient.Tag.Apply(ierr)
50 return 50 return
51 }, func(err error, d time.Duration) { 51 }, func(err error, d time.Duration) {
52 log.Fields{ 52 log.Fields{
53 log.ErrorKey: err, 53 log.ErrorKey: err,
54 "delay": d, 54 "delay": d,
55 "bucket": w.bucket, 55 "bucket": w.bucket,
56 "path": w.relpath, 56 "path": w.relpath,
57 }.Warningf(w, "Transient error on GS write. Retrying...") 57 }.Warningf(w, "Transient error on GS write. Retrying...")
58 }) 58 })
59 59
60 w.count += int64(a) 60 w.count += int64(a)
61 return 61 return
62 } 62 }
63 63
64 func (w *prodWriter) Close() error { 64 func (w *prodWriter) Close() error {
65 if w.Writer == nil { 65 if w.Writer == nil {
66 return nil 66 return nil
67 } 67 }
68 68
69 » return retry.Retry(w, retry.TransientOnly(retry.Default), 69 » return retry.Retry(w, transient.Only(retry.Default),
70 w.Writer.Close, 70 w.Writer.Close,
71 func(err error, d time.Duration) { 71 func(err error, d time.Duration) {
72 log.Fields{ 72 log.Fields{
73 log.ErrorKey: err, 73 log.ErrorKey: err,
74 "delay": d, 74 "delay": d,
75 "bucket": w.bucket, 75 "bucket": w.bucket,
76 "path": w.relpath, 76 "path": w.relpath,
77 }.Warningf(w, "Transient error closing GS Writer. Retryi ng...") 77 }.Warningf(w, "Transient error closing GS Writer. Retryi ng...")
78 }) 78 })
79 } 79 }
80 80
81 func (w *prodWriter) Count() int64 { 81 func (w *prodWriter) Count() int64 {
82 return w.count 82 return w.count
83 } 83 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698