-
Notifications
You must be signed in to change notification settings - Fork 152
/
dependencies.go
72 lines (57 loc) · 1.82 KB
/
dependencies.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
package execute
import (
"context"
"time"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/metadata"
"go.uber.org/zap"
)
type key int
const executionDependenciesKey key = iota
type ExecutionOptions struct {
OperatorProfiler *OperatorProfiler
Profilers []Profiler
}
// ExecutionDependencies represents the dependencies that a function call
// executed by the Interpreter needs in order to trigger the execution of a flux.Program
type ExecutionDependencies struct {
// Must be set
Allocator *memory.Allocator
Now *time.Time
// Allowed to be nil
Logger *zap.Logger
// Metadata is passed up from any invocations of execution up to the parent
// execution, and out through the statistics.
Metadata metadata.Metadata
ExecutionOptions *ExecutionOptions
}
func (d ExecutionDependencies) Inject(ctx context.Context) context.Context {
return context.WithValue(ctx, executionDependenciesKey, d)
}
func HaveExecutionDependencies(ctx context.Context) bool {
return ctx.Value(executionDependenciesKey) != nil
}
func GetExecutionDependencies(ctx context.Context) ExecutionDependencies {
return ctx.Value(executionDependenciesKey).(ExecutionDependencies)
}
// Create some execution dependencies. Any arg may be nil, this will choose
// some suitable defaults.
func NewExecutionDependencies(allocator *memory.Allocator, now *time.Time, logger *zap.Logger) ExecutionDependencies {
if allocator == nil {
allocator = new(memory.Allocator)
}
if now == nil {
nowVar := time.Now()
now = &nowVar
}
return ExecutionDependencies{
Allocator: allocator,
Now: now,
Logger: logger,
Metadata: make(metadata.Metadata),
ExecutionOptions: &ExecutionOptions{},
}
}
func DefaultExecutionDependencies() ExecutionDependencies {
return NewExecutionDependencies(nil, nil, nil)
}