/
result.go
90 lines (71 loc) · 1.74 KB
/
result.go
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
90
// Copyright (c) 2017 Opsidian Ltd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package conflow
import "time"
// Retryable is a common interface for a retryable result or error
type Retryable interface {
Retry() bool
RetryAfter() time.Duration
}
// Result is an interface for returning the result of a block run
type Result interface {
Retryable
RetryReason() string
}
// Retry is a block run result which indicates we need to retry
type Retry string
func (r Retry) Retry() bool {
return true
}
func (r Retry) RetryAfter() time.Duration {
return 0
}
func (r Retry) RetryReason() string {
return string(r)
}
// RetryAfter is a block run result which indicates we need to retry after the given time
func RetryAfter(duration time.Duration, reason string) Result {
return retryAfter{
Duration: duration,
Reason: reason,
}
}
type retryAfter struct {
Duration time.Duration
Reason string
}
func (r retryAfter) Retry() bool {
return true
}
func (r retryAfter) RetryAfter() time.Duration {
return r.Duration
}
func (r retryAfter) RetryReason() string {
return r.Reason
}
// RetryableError wraps the given error and makes it retryable
func RetryableError(err error, duration time.Duration) error {
return retryableError{
err: err,
duration: duration,
}
}
type retryableError struct {
err error
duration time.Duration
}
func (r retryableError) Retry() bool {
return true
}
func (r retryableError) RetryAfter() time.Duration {
return r.duration
}
func (r retryableError) Error() string {
return r.err.Error()
}
func (r retryableError) UnWrap() error {
return r.err
}