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

Side by Side Diff: client/internal/retry/retry.go

Issue 1159563002: Move retry and lhttp packages from client/internal to common/. (Closed) Base URL: git@github.com:luci/luci-go@master
Patch Set: move-retry-http Created 5 years, 7 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
« no previous file with comments | « client/internal/retry/doc.go ('k') | client/internal/retry/retry_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package retry
6
7 import (
8 "io"
9 "time"
10 )
11
12 // Default defines the default retry parameters that should be used throughout
13 // the program. It is fine to update this variable on start up.
14 var Default = &Config{
15 10,
16 100 * time.Millisecond,
17 500 * time.Millisecond,
18 5 * time.Second,
19 }
20
21 // Config defines the retry properties.
22 type Config struct {
23 MaxTries int // Maximum number of retries.
24 SleepMax time.Duration // Maximum duration of a single sleep.
25 SleepBase time.Duration // Base sleep duration.
26 SleepMultiplicative time.Duration // Incremental sleep duration for each additional try.
27 }
28
29 // Do runs a Retriable, potentially retrying it multiple times.
30 func (c *Config) Do(r Retriable) (err error) {
31 defer func() {
32 if err2 := r.Close(); err == nil {
33 err = err2
34 }
35 }()
36 for i := 0; i < c.MaxTries; i++ {
37 err = r.Do()
38 if _, ok := err.(Error); !ok {
39 return err
40 }
41 if i != c.MaxTries-1 {
42 s := c.SleepBase + time.Duration(i)*c.SleepMultiplicativ e
43 if s > c.SleepMax {
44 s = c.SleepMax
45 }
46 time.Sleep(s)
47 }
48 }
49 return
50 }
51
52 // Error is an error that can be retried.
53 type Error struct {
54 Err error
55 }
56
57 func (e Error) Error() string {
58 return e.Err.Error()
59 }
60
61 // Retriable is a task that can be retried. It is important that Do be
62 // idempotent.
63 type Retriable interface {
64 io.Closer
65 Do() error
66 }
OLDNEW
« no previous file with comments | « client/internal/retry/doc.go ('k') | client/internal/retry/retry_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698