File tree 9 files changed +83
-3
lines changed
utils/exponential_backoff
9 files changed +83
-3
lines changed Original file line number Diff line number Diff line change 7
7
build_binary :
8
8
name : Build shell-operator binary
9
9
runs-on : ubuntu-latest
10
+ env :
11
+ ACTIONS_ALLOW_UNSECURE_COMMANDS : true
10
12
steps :
11
13
- name : Set up Go 1.15
12
14
uses : actions/setup-go@v2
Original file line number Diff line number Diff line change 54
54
- ubuntu
55
55
- alpine
56
56
runs-on : ubuntu-latest
57
+ env :
58
+ ACTIONS_ALLOW_UNSECURE_COMMANDS : true
57
59
if : github.event_name == 'pull_request' && github.event.label.id == 1838515600 # ':robot: build dev images' label
58
60
steps :
59
61
- uses : actions/checkout@v2
Original file line number Diff line number Diff line change 8
8
run_linter :
9
9
name : Run linter
10
10
runs-on : ubuntu-latest
11
+ env :
12
+ ACTIONS_ALLOW_UNSECURE_COMMANDS : true
11
13
steps :
12
14
- name : Set up Go 1.15
13
15
uses : actions/setup-go@v2
Original file line number Diff line number Diff line change 20
20
- alpine3.11
21
21
- alpine3.12
22
22
runs-on : [ubuntu-latest]
23
+ env :
24
+ ACTIONS_ALLOW_UNSECURE_COMMANDS : true
23
25
steps :
24
26
- uses : actions/checkout@v2
25
27
Original file line number Diff line number Diff line change 46
46
name : Build and unit tests
47
47
if : github.event_name == 'push' && github.event.ref != 'refs/heads/master'
48
48
runs-on : ubuntu-latest
49
+ env :
50
+ ACTIONS_ALLOW_UNSECURE_COMMANDS : true
49
51
steps :
50
52
- name : Set up Go 1.15
51
53
uses : actions/setup-go@v2
@@ -132,6 +134,8 @@ jobs:
132
134
name : Download modules and build libjq
133
135
if : (github.event_name == 'push' && github.event.ref == 'refs/heads/master') || (github.event_name == 'pull_request' && github.event.label.id == 1838578615)
134
136
runs-on : ubuntu-latest
137
+ env :
138
+ ACTIONS_ALLOW_UNSECURE_COMMANDS : true
135
139
steps :
136
140
- name : Set up Go 1.15
137
141
uses : actions/setup-go@v2
@@ -208,6 +212,8 @@ jobs:
208
212
if : (github.event_name == 'push' && github.event.ref == 'refs/heads/master') || (github.event_name == 'pull_request' && github.event.label.id == 1838578615)
209
213
needs : prepare_build_dependencies
210
214
runs-on : ubuntu-latest
215
+ env :
216
+ ACTIONS_ALLOW_UNSECURE_COMMANDS : true
211
217
steps :
212
218
- name : Checkout code
213
219
uses : actions/checkout@v2
@@ -277,6 +283,8 @@ jobs:
277
283
os : [ubuntu-latest]
278
284
k8s_version : [1.16, 1.17]
279
285
runs-on : ${{ matrix.os }}
286
+ env :
287
+ ACTIONS_ALLOW_UNSECURE_COMMANDS : true
280
288
steps :
281
289
- name : Checkout code
282
290
uses : actions/checkout@v2
Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "math/rand"
5
6
"os"
7
+ "time"
6
8
7
9
"github.com/flant/shell-operator/pkg/debug"
8
10
log "github.com/sirupsen/logrus"
@@ -29,8 +31,11 @@ func main() {
29
31
startCmd := kpApp .Command ("start" , "Start shell-operator." ).
30
32
Default ().
31
33
Action (func (c * kingpin.ParseContext ) error {
34
+ // Init logging subsystem.
32
35
app .SetupLogging ()
33
36
log .Infof ("%s %s" , app .AppName , app .Version )
37
+ // Init rand generator.
38
+ rand .Seed (time .Now ().UnixNano ())
34
39
35
40
defaultOperator := shell_operator .DefaultOperator ()
36
41
err := shell_operator .InitAndStart (defaultOperator )
Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ package queue
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "github.com/flant/shell-operator/pkg/utils/measure"
7
6
"os"
8
7
"strings"
9
8
"sync"
@@ -13,6 +12,8 @@ import (
13
12
14
13
"github.com/flant/shell-operator/pkg/metric_storage"
15
14
"github.com/flant/shell-operator/pkg/task"
15
+ "github.com/flant/shell-operator/pkg/utils/exponential_backoff"
16
+ "github.com/flant/shell-operator/pkg/utils/measure"
16
17
)
17
18
18
19
/*
@@ -351,9 +352,9 @@ func (q *TaskQueue) Start() {
351
352
352
353
switch taskRes .Status {
353
354
case "Fail" :
355
+ // Exponential backoff delay before retry.
356
+ nextSleepDelay = exponential_backoff .CalculateDelay (DelayOnFailedTask , t .GetFailureCount ())
354
357
t .IncrementFailureCount ()
355
- // delay before retry
356
- nextSleepDelay = DelayOnFailedTask
357
358
q .Status = fmt .Sprintf ("sleep after fail for %s" , nextSleepDelay .String ())
358
359
case "Success" :
359
360
// add tasks after current task in reverse order
Original file line number Diff line number Diff line change
1
+ package exponential_backoff
2
+
3
+ import (
4
+ "math"
5
+ "math/rand"
6
+ "time"
7
+ )
8
+
9
+ const MaxExponentialBackoffDelay = time .Duration (32 * time .Second )
10
+ const ExponentialDelayFactor float64 = 2.0 // Each delay is twice longer.
11
+ const ExponentialDelayRandomMs = 1000 // Each delay has random additional milliseconds.
12
+
13
+ // CalculateDelay returns delay distributed from initialDelay to default maxDelay (32s)
14
+ //
15
+ // Example:
16
+ // Retry 0: 5s
17
+ // Retry 1: 6s
18
+ // Retry 2: 7.8s
19
+ // Retry 3: 9.8s
20
+ // Retry 4: 13s
21
+ // Retry 5: 21s
22
+ // Retry 6: 32s
23
+ // Retry 7: 32s
24
+ func CalculateDelay (initialDelay time.Duration , retryCount int ) time.Duration {
25
+ return CalculateDelayWithMax (initialDelay , MaxExponentialBackoffDelay , retryCount )
26
+ }
27
+
28
+ func CalculateDelayWithMax (initialDelay time.Duration , maxDelay time.Duration , retryCount int ) time.Duration {
29
+ if retryCount == 0 {
30
+ return initialDelay
31
+ }
32
+ delayNs := int64 (float64 (time .Second ) * math .Pow (ExponentialDelayFactor , float64 (retryCount - 1 )))
33
+ rndDelayMs := rand .Intn (ExponentialDelayRandomMs )
34
+ delay := initialDelay + time .Duration (delayNs ) + time .Duration (rndDelayMs )* time .Millisecond
35
+ delay = delay .Truncate (100 * time .Millisecond )
36
+ if delay .Nanoseconds () > maxDelay .Nanoseconds () {
37
+ return maxDelay
38
+ }
39
+ return delay
40
+ }
Original file line number Diff line number Diff line change
1
+ package exponential_backoff
2
+
3
+ import (
4
+ "math/rand"
5
+ "testing"
6
+ "time"
7
+ )
8
+
9
+ func Test_Delay (t * testing.T ) {
10
+ t .SkipNow ()
11
+ rand .Seed (time .Now ().UnixNano ())
12
+ for i := 0 ; i < 16 ; i ++ {
13
+ t .Logf ("Delay for %02d retry: %s\n " ,
14
+ i ,
15
+ CalculateDelay (5 * time .Second , i ),
16
+ )
17
+ }
18
+ }
You can’t perform that action at this time.
0 commit comments