This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
/
visitor.go
66 lines (53 loc) · 1.5 KB
/
visitor.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
package options
import (
"runtime"
"time"
"github.com/jmalloc/twelf/src/twelf"
opentracing "github.com/opentracing/opentracing-go"
)
// visitor handles the application of options.
type visitor interface {
applyDefaultTimeout(time.Duration) error
applyLogger(twelf.Logger) error
applyCommandWorkers(uint) error
applySessionWorkers(uint) error
applyPruneInterval(time.Duration) error
applyProduct(string) error
applyTracer(opentracing.Tracer) error
}
// Apply applies the default options, then a sequence of additional options to v.
func Apply(v visitor, opts ...Option) error {
if err := v.applyDefaultTimeout(5 * time.Second); err != nil {
return err
}
procs := uint(runtime.GOMAXPROCS(0))
if err := v.applyCommandWorkers(procs); err != nil {
return err
}
if err := v.applySessionWorkers(procs * 10); err != nil {
return err
}
if err := v.applyLogger(defaultLogger); err != nil {
return err
}
if err := v.applyPruneInterval(3 * time.Minute); err != nil {
return err
}
if err := v.applyTracer(opentracing.NoopTracer{}); err != nil {
return err
}
for _, o := range opts {
if err := o(v); err != nil {
return err
}
}
return nil
}
var defaultLogger twelf.Logger
func init() {
// Initialize the default logger before any testing framework can redirect
// stdout. This lets us use standard "Output:" checks in example tests
// without having to match the log output, while still printing the log
// output in case of a test failure.
defaultLogger = &twelf.StandardLogger{}
}