Skip to content

Commit 26e4c7f

Browse files
authored
feat: retry strategies can now be limited to network errors only [SIM-104] (#321)
* feat: retry strategies can now be limited to network errors only * feat: add tests * chore: generate docs
1 parent c07ea39 commit 26e4c7f

File tree

10 files changed

+1285
-0
lines changed

10 files changed

+1285
-0
lines changed

checkly/attribute_retry_strategy.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ var retryStrategyAttributeSchema = &schema.Schema{
4949
Optional: true,
5050
Default: true,
5151
},
52+
"only_on": {
53+
Description: "Apply the retry strategy only if the defined conditions match.",
54+
Type: schema.TypeList,
55+
MaxItems: 1,
56+
Optional: true,
57+
Elem: &schema.Resource{
58+
Schema: map[string]*schema.Schema{
59+
"network_error": {
60+
Description: "When `true`, retry only if the cause of the failure is a network error. (Default `false`).",
61+
Type: schema.TypeBool,
62+
Optional: true,
63+
Default: false,
64+
},
65+
},
66+
},
67+
},
5268
},
5369
},
5470
}
@@ -70,6 +86,7 @@ func retryStrategyFromList(s []any) *checkly.RetryStrategy {
7086
Type: res["type"].(string),
7187
BaseBackoffSeconds: res["base_backoff_seconds"].(int),
7288
SameRegion: res["same_region"].(bool),
89+
OnlyOn: retryStrategyOnlyOnFromList(res["only_on"].([]any)),
7390
}
7491
default:
7592
return &checkly.RetryStrategy{
@@ -78,6 +95,7 @@ func retryStrategyFromList(s []any) *checkly.RetryStrategy {
7895
MaxRetries: res["max_retries"].(int),
7996
MaxDurationSeconds: res["max_duration_seconds"].(int),
8097
SameRegion: res["same_region"].(bool),
98+
OnlyOn: retryStrategyOnlyOnFromList(res["only_on"].([]any)),
8199
}
82100
}
83101
}
@@ -98,6 +116,7 @@ func listFromRetryStrategy(rs *checkly.RetryStrategy) []tfMap {
98116
"type": rs.Type,
99117
"base_backoff_seconds": rs.BaseBackoffSeconds,
100118
"same_region": rs.SameRegion,
119+
"only_on": listFromRetryStrategyOnlyOn(rs.OnlyOn),
101120
},
102121
}
103122
default:
@@ -108,11 +127,39 @@ func listFromRetryStrategy(rs *checkly.RetryStrategy) []tfMap {
108127
"max_retries": rs.MaxRetries,
109128
"max_duration_seconds": rs.MaxDurationSeconds,
110129
"same_region": rs.SameRegion,
130+
"only_on": listFromRetryStrategyOnlyOn(rs.OnlyOn),
111131
},
112132
}
113133
}
114134
}
115135

136+
func retryStrategyOnlyOnFromList(s []any) []string {
137+
var conditions []string
138+
139+
for _, item := range s {
140+
res := item.(tfMap)
141+
142+
if v, ok := res["network_error"].(bool); ok && v {
143+
conditions = append(conditions, "NETWORK_ERROR")
144+
}
145+
}
146+
147+
return conditions
148+
}
149+
150+
func listFromRetryStrategyOnlyOn(conditions []string) []tfMap {
151+
switch {
152+
case len(conditions) == 1 && conditions[0] == "NETWORK_ERROR":
153+
return []tfMap{
154+
{
155+
"network_error": true,
156+
},
157+
}
158+
default:
159+
return []tfMap{}
160+
}
161+
}
162+
116163
var doubleCheckEquivalentRetryStrategy = checkly.RetryStrategy{
117164
Type: "FIXED",
118165
MaxRetries: 1,

0 commit comments

Comments
 (0)