-
Notifications
You must be signed in to change notification settings - Fork 338
/
options.go
186 lines (157 loc) · 4.43 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package rxgo
import (
"context"
"runtime"
"github.com/teivah/onecontext"
)
var emptyContext context.Context
// Option handles configurable options.
type Option interface {
apply(*funcOption)
toPropagate() bool
isEagerObservation() bool
getPool() (bool, int)
buildChannel() chan Item
buildContext(parent context.Context) context.Context
getBackPressureStrategy() BackpressureStrategy
getErrorStrategy() OnErrorStrategy
isConnectable() bool
isConnectOperation() bool
isSerialized() (bool, func(interface{}) int)
}
type funcOption struct {
f func(*funcOption)
isBuffer bool
buffer int
ctx context.Context
observation ObservationStrategy
pool int
backPressureStrategy BackpressureStrategy
onErrorStrategy OnErrorStrategy
propagate bool
connectable bool
connectOperation bool
serialized func(interface{}) int
}
func (fdo *funcOption) toPropagate() bool {
return fdo.propagate
}
func (fdo *funcOption) isEagerObservation() bool {
return fdo.observation == Eager
}
func (fdo *funcOption) getPool() (bool, int) {
return fdo.pool > 0, fdo.pool
}
func (fdo *funcOption) buildChannel() chan Item {
if fdo.isBuffer {
return make(chan Item, fdo.buffer)
}
return make(chan Item)
}
func (fdo *funcOption) buildContext(parent context.Context) context.Context {
if fdo.ctx != nil && parent != nil {
ctx, _ := onecontext.Merge(fdo.ctx, parent)
return ctx
}
if fdo.ctx != nil {
return fdo.ctx
}
if parent != nil {
return parent
}
return context.Background()
}
func (fdo *funcOption) getBackPressureStrategy() BackpressureStrategy {
return fdo.backPressureStrategy
}
func (fdo *funcOption) getErrorStrategy() OnErrorStrategy {
return fdo.onErrorStrategy
}
func (fdo *funcOption) isConnectable() bool {
return fdo.connectable
}
func (fdo *funcOption) isConnectOperation() bool {
return fdo.connectOperation
}
func (fdo *funcOption) apply(do *funcOption) {
fdo.f(do)
}
func (fdo *funcOption) isSerialized() (bool, func(interface{}) int) {
if fdo.serialized == nil {
return false, nil
}
return true, fdo.serialized
}
func newFuncOption(f func(*funcOption)) *funcOption {
return &funcOption{
f: f,
}
}
func parseOptions(opts ...Option) Option {
o := new(funcOption)
for _, opt := range opts {
opt.apply(o)
}
return o
}
// WithBufferedChannel allows to configure the capacity of a buffered channel.
func WithBufferedChannel(capacity int) Option {
return newFuncOption(func(options *funcOption) {
options.isBuffer = true
options.buffer = capacity
})
}
// WithContext allows to pass a context.
func WithContext(ctx context.Context) Option {
return newFuncOption(func(options *funcOption) {
options.ctx = ctx
})
}
// WithObservationStrategy uses the eager observation mode meaning consuming the items even without subscription.
func WithObservationStrategy(strategy ObservationStrategy) Option {
return newFuncOption(func(options *funcOption) {
options.observation = strategy
})
}
// WithPool allows to specify an execution pool.
func WithPool(pool int) Option {
return newFuncOption(func(options *funcOption) {
options.pool = pool
})
}
// WithCPUPool allows to specify an execution pool based on the number of logical CPUs.
func WithCPUPool() Option {
return newFuncOption(func(options *funcOption) {
options.pool = runtime.NumCPU()
})
}
// WithBackPressureStrategy sets the back pressure strategy: drop or block.
func WithBackPressureStrategy(strategy BackpressureStrategy) Option {
return newFuncOption(func(options *funcOption) {
options.backPressureStrategy = strategy
})
}
// WithErrorStrategy defines how an observable should deal with error.
// This strategy is propagated to the parent observable.
func WithErrorStrategy(strategy OnErrorStrategy) Option {
return newFuncOption(func(options *funcOption) {
options.onErrorStrategy = strategy
})
}
// WithPublishStrategy converts an ordinary Observable into a connectable Observable.
func WithPublishStrategy() Option {
return newFuncOption(func(options *funcOption) {
options.connectable = true
})
}
// Serialize forces an Observable to make serialized calls and to be well-behaved.
func Serialize(identifier func(interface{}) int) Option {
return newFuncOption(func(options *funcOption) {
options.serialized = identifier
})
}
func connect() Option {
return newFuncOption(func(options *funcOption) {
options.connectOperation = true
})
}