-
Notifications
You must be signed in to change notification settings - Fork 15
/
retry_config.go
86 lines (77 loc) · 3.01 KB
/
retry_config.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
package plugin
import (
"context"
"fmt"
"log"
"github.com/turbot/go-kit/helpers"
)
type RetryConfig struct {
ShouldRetryErrorFunc ErrorPredicateWithContext
// deprecated use ShouldRetryErrorFunc
ShouldRetryError ErrorPredicate
}
func (c *RetryConfig) String() interface{} {
if c.ShouldRetryError != nil {
return fmt.Sprintf("ShouldRetryError: %s", helpers.GetFunctionName(c.ShouldRetryError))
}
if c.ShouldRetryErrorFunc != nil {
return fmt.Sprintf("ShouldRetryErrorFunc: %s", helpers.GetFunctionName(c.ShouldRetryErrorFunc))
}
return ""
}
func (c *RetryConfig) Validate(table *Table) []string {
if c.ShouldRetryError != nil && c.ShouldRetryErrorFunc != nil {
log.Printf("[TRACE] RetryConfig validate failed - both ShouldRetryError and ShouldRetryErrorFunc are defined")
var tablePrefix string
if table != nil {
tablePrefix = fmt.Sprintf("table '%s' ", table.Name)
}
return []string{fmt.Sprintf("%sboth ShouldRetryError and ShouldRetryErrorFunc are defined", tablePrefix)}
}
return nil
}
func (c *RetryConfig) DefaultTo(other *RetryConfig) {
// if not other provided, nothing to do
if other == nil {
return
}
// if either ShouldIgnoreError or ShouldIgnoreErrorFunc are set, do not default to other
if c.ShouldRetryError != nil || c.ShouldRetryErrorFunc != nil {
log.Printf("[TRACE] RetryConfig DefaultTo: config defines a should retry function so not defaulting to base")
return
}
// legacy func
if c.ShouldRetryError == nil && other.ShouldRetryError != nil {
log.Printf("[TRACE] RetryConfig DefaultTo: using base ShouldRetryError: %s", helpers.GetFunctionName(other.ShouldRetryError))
c.ShouldRetryError = other.ShouldRetryError
}
if c.ShouldRetryErrorFunc == nil && other.ShouldRetryErrorFunc != nil {
log.Printf("[TRACE] RetryConfig DefaultTo: using base ShouldRetryErrorFunc: %s", helpers.GetFunctionName(other.ShouldRetryErrorFunc))
c.ShouldRetryErrorFunc = other.ShouldRetryErrorFunc
}
}
// GetListRetryConfig wraps the ShouldRetry function with an additional check of the rows streamed
// (as we cannot retry errors in the list hydrate function after streaming has started)
func (c *RetryConfig) GetListRetryConfig() *RetryConfig {
listRetryConfig := &RetryConfig{}
if c.ShouldRetryErrorFunc != nil {
listRetryConfig.ShouldRetryErrorFunc = func(ctx context.Context, d *QueryData, h *HydrateData, err error) bool {
if d.QueryStatus.rowsStreamed != 0 {
log.Printf("[TRACE] shouldRetryError we have started streaming rows (%d) - return false", d.QueryStatus.rowsStreamed)
return false
}
res := c.ShouldRetryErrorFunc(ctx, d, h, err)
return res
}
} else if c.ShouldRetryError != nil {
listRetryConfig.ShouldRetryErrorFunc = func(ctx context.Context, d *QueryData, h *HydrateData, err error) bool {
if d.QueryStatus.rowsStreamed != 0 {
log.Printf("[TRACE] shouldRetryError we have started streaming rows (%d) - return false", d.QueryStatus.rowsStreamed)
return false
}
// call the legacy function
return c.ShouldRetryError(err)
}
}
return listRetryConfig
}