-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
103 lines (87 loc) · 2.75 KB
/
options.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
package zenmodel
import (
"time"
"github.com/zenmodel/zenmodel/internal/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Option configures a BrainLocal in build.
type Option interface {
apply(brain Brain)
}
// optionFunc wraps a func, so it satisfies the Option interface.
type optionFunc func(Brain)
func (f optionFunc) apply(brain Brain) {
f(brain)
}
// WithLocalMaintainer sets the rate limiter options for the BrainLocal maintainer.
func WithLocalMaintainer(rateLimiterBaseDelay, rateLimiterMaxDelay time.Duration) Option {
return optionFunc(func(brain Brain) {
brainLocal := brain.(*BrainLocal)
brainLocal.brainLocalOptions = brainLocalOptions{
rateLimiterBaseDelay: rateLimiterBaseDelay,
rateLimiterMaxDelay: rateLimiterMaxDelay,
}
})
}
// WithWorkerNum sets the worker number for the BrainLocal implementation.
func WithWorkerNum(workerNun int) Option {
return optionFunc(func(brain Brain) {
brainLocal := brain.(*BrainLocal)
brainLocal.brainLocalOptions = brainLocalOptions{
workerNum: workerNun,
}
})
}
// WithMemorySetting sets the memory setting for BrainLocal implementation.
func WithMemorySetting(memoryNumCounters, memoryMaxCost int64) Option {
return optionFunc(func(brain Brain) {
brainLocal := brain.(*BrainLocal)
brainLocal.brainLocalOptions = brainLocalOptions{
memoryNumCounters: memoryNumCounters,
memoryMaxCost: memoryMaxCost,
}
})
}
// WithLoggerLevel sets the default logger with specific level for BrainLocal implementation.
func WithLoggerLevel(level zapcore.Level) Option {
return optionFunc(func(brain Brain) {
brainLocal := brain.(*BrainLocal)
brainLocal.logger = log.NewDefaultLoggerWithLevel(level)
})
}
// WithLogger sets the specific logger for BrainLocal implementation.
func WithLogger(logger *zap.Logger) Option {
return optionFunc(func(brain Brain) {
brainLocal := brain.(*BrainLocal)
brainLocal.logger = logger
})
}
// WithID sets the specific brain ID for BrainLocal implementation.
func WithID(brainID string) Option {
return optionFunc(func(brain Brain) {
brainLocal := brain.(*BrainLocal)
brainLocal.id = brainID
})
}
// NeuronOption configures a neuron.
type NeuronOption interface {
apply(neuron *Neuron)
}
// neuronOptionFunc wraps a func, so it satisfies the NeuronOption interface.
type neuronOptionFunc func(*Neuron)
func (f neuronOptionFunc) apply(neuron *Neuron) {
f(neuron)
}
// WithLabels sets the specific labels for Neuron
func WithLabels(labels map[string]string) NeuronOption {
return neuronOptionFunc(func(neuron *Neuron) {
neuron.labels = labels
})
}
// WithSelectFn sets the specific selectFn for Neuron
func WithSelectFn(selectFn func(brain BrainRuntime) string) NeuronOption {
return neuronOptionFunc(func(neuron *Neuron) {
neuron.selectFn = selectFn
})
}