/
common.go
49 lines (40 loc) · 1.17 KB
/
common.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
package protocol
import (
"context"
"fmt"
"time"
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/internal/loghelper"
)
const ReportingPluginTimeoutWarningGracePeriod = 100 * time.Millisecond
func callPlugin[T any](
ctx context.Context,
logger loghelper.LoggerWithContext,
logFields commontypes.LogFields,
name string,
maxDuration time.Duration,
f func(context.Context) (T, error),
) (T, bool) {
pluginCtx, cancel := context.WithTimeout(ctx, maxDuration)
defer cancel()
ins := loghelper.NewIfNotStopped(
maxDuration+ReportingPluginTimeoutWarningGracePeriod,
func() {
logger.MakeChild(logFields).Error(fmt.Sprintf("call to ReportingPlugin.%s is taking too long", name), commontypes.LogFields{
"maxDuration": maxDuration.String(),
"gracePeriod": ReportingPluginTimeoutWarningGracePeriod.String(),
})
},
)
result, err := f(pluginCtx)
ins.Stop()
if err != nil {
logger.MakeChild(logFields).ErrorIfNotCanceled(fmt.Sprintf("call to ReportingPlugin.%s errored", name), ctx, commontypes.LogFields{
"error": err,
})
// failed to get data, nothing to be done
var zero T
return zero, false
}
return result, true
}