File: README.md

package info (click to toggle)
golang-github-matryer-try 1%2Bgit20161228.6.9ac251b-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 88 kB
  • sloc: makefile: 2
file content (89 lines) | stat: -rw-r--r-- 2,485 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# try [![GoDoc](https://godoc.org/github.com/matryer/try?status.svg)](https://godoc.org/github.com/matryer/try) [![Go Report Card](https://goreportcard.com/badge/github.com/matryer/try)](https://goreportcard.com/report/github.com/matryer/try)

Idiomatic Go retry package. Thanks to [@rowland](https://github.com/rowland) for code review.

```
go get gopkg.in/matryer/try.v1
or
drop gopkg.in/matryer/try.v1
```

* Learn more about [Drop](https://github.com/matryer/drop)

### Usage

Just call `try.Do` with the function you want to retry in the event of an error:

  * Call `try.Do` that returns a `bool` indicating whether to retry or not, and an `error` 
  * The `attempt` argument will start at 1 and count up
  * `try.Do` blocks until you return `false`, or a `nil` error
  * `try.Do` returns the last error or `nil` if it was successful

```
var value string
err := try.Do(func(attempt int) (bool, error) {
  var err error
  value, err = SomeFunction()
  return attempt < 5, err // try 5 times
})
if err != nil {
  log.Fatalln("error:", err)
}
```

In the above example the function will be called repeatedly until error is `nil`, while `attempt < 5` (i.e. try 5 times)

#### Retrying panics

Try supports retrying in the event of a panic.

  * Use named return parameters
  * Set `retry` first
  * Defer the recovery code, and set `err` manually in the case of a panic
  * Use empty `return` statement at the end

```
var value string
err := try.Do(func(attempt int) (retry bool, err error) {
  retry = attempt < 5 // try 5 times
  defer func() {
    if r := recover(); r != nil {
      err = errors.New(fmt.Sprintf("panic: %v", r))
    }
  }()
  value, err = SomeFunction()
  return
})
if err != nil {
  log.Fatalln("error:", err)
}
```

#### Delay between retries

To introduce a delay between retries, just make a `time.Sleep` call before you return from the function if you are returning an error.

```
var value string
err := try.Do(func(attempt int) (bool, error) {
  var err error
  value, err = SomeFunction()
  if err != nil {
    time.Sleep(1 * time.Minute) // wait a minute
  }
  return attempt < 5, err
})
if err != nil {
  log.Fatalln("error:", err)
}
```

#### Maximum retry limit

To avoid infinite loops, Try will ensure it only makes `try.MaxRetries` attempts. By default, this value is `10`, but you can change it:

```
try.MaxRetries = 20
```

To see if a `Do` operation failed due to reaching the limit, you can check the `error` with `try.IsMaxRetries(err)`.