-
Notifications
You must be signed in to change notification settings - Fork 15
/
remotecall.go
118 lines (106 loc) · 3.84 KB
/
remotecall.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2018-2023, CS Systemes d'Information, http://csgroup.eu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package stacks
import (
"context"
"github.com/CS-SI/SafeScale/v22/lib/utils/fail"
netutils "github.com/CS-SI/SafeScale/v22/lib/utils/net"
"github.com/CS-SI/SafeScale/v22/lib/utils/retry"
"github.com/CS-SI/SafeScale/v22/lib/utils/temporal"
)
// RetryableRemoteCall calls a remote API with tolerance to communication failures
// Remote API is done inside 'callback' parameter and returns remote error if necessary that 'convertError' function convert to SafeScale error
func RetryableRemoteCall(inctx context.Context, callback func() error, convertError func(error) fail.Error, options ...retry.Option) fail.Error {
ctx, cancel := context.WithCancel(inctx)
defer cancel()
type result struct {
rErr fail.Error
}
chRes := make(chan result)
go func() {
defer close(chRes)
gerr := func() (ferr fail.Error) {
defer fail.OnPanic(&ferr)
if callback == nil {
return fail.InvalidParameterCannotBeNilError("callback")
}
var normalizeError func(err error) fail.Error
if convertError != nil {
normalizeError = func(err error) fail.Error { return convertError(err) }
} else {
normalizeError = fail.ConvertError
}
plannedAction := retry.NewAction(
retry.Fibonacci(temporal.MinDelay()), // waiting time between retries follows Fibonacci numbers x MinDelay()
nil,
func() (nested error) {
defer fail.OnPanic(&nested)
select {
case <-ctx.Done():
return retry.StopRetryError(ctx.Err())
default:
}
if innerErr := callback(); innerErr != nil {
captured := normalizeError(innerErr)
switch captured.(type) { // nolint
case *fail.ErrNotFound, *fail.ErrDuplicate, *fail.ErrInvalidRequest, *fail.ErrNotAuthenticated, *fail.ErrForbidden, *fail.ErrOverflow, *fail.ErrSyntax, *fail.ErrInconsistent, *fail.ErrInvalidInstance, *fail.ErrInvalidInstanceContent, *fail.ErrInvalidParameter, *fail.ErrRuntimePanic: // Do not retry if it's going to fail anyway
return retry.StopRetryError(captured)
case *fail.ErrOverload:
return retry.StopRetryError(captured)
default:
return captured
}
}
return nil
},
nil,
temporal.CommunicationTimeout(),
)
for _, opt := range options { // now we can override the actions without changing every function that invokes RetryableRemoteCall, only callers interested in such thing will add options parameters in their invocation
err := opt(plannedAction)
if err != nil {
return fail.ConvertError(err)
}
}
// Execute the remote call with tolerance for transient communication failure
xerr := netutils.WhileUnsuccessfulButRetryable(
plannedAction.Run,
plannedAction.Officer,
plannedAction.Timeout,
)
if xerr != nil {
switch xerr.(type) {
case *retry.ErrStopRetry: // On StopRetry, the real error is the cause
return fail.Wrap(fail.Cause(xerr), "stopping retries")
case *retry.ErrTimeout: // On timeout, we keep the last error as cause
return fail.Wrap(fail.Cause(xerr), "timeout")
default:
return xerr
}
}
return nil
}()
chRes <- result{gerr}
}()
select {
case res := <-chRes:
return res.rErr
case <-ctx.Done():
return fail.ConvertError(ctx.Err())
case <-inctx.Done():
return fail.ConvertError(inctx.Err())
}
}