-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathoptions.go
184 lines (158 loc) · 5.06 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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package buffer
import (
"errors"
"time"
"github.com/m3db/m3x/instrument"
"github.com/m3db/m3x/retry"
)
const (
defaultMaxBufferSize = 100 * 1024 * 1024 // 100MB.
defaultMaxMessageSize = 1 * 1024 * 1024 // 1MB.
defaultCloseCheckInterval = time.Second
defaultDropOldestInterval = time.Second
defaultScanBatchSize = 16
defaultCleanupInitialBackoff = 10 * time.Second
defaultAllowedSpilloverRatio = 0.2
defaultCleanupMaxBackoff = time.Minute
)
var (
errInvalidScanBatchSize = errors.New("invalid scan batch size")
errInvalidMaxMessageSize = errors.New("invalid max message size")
errNegativeMaxBufferSize = errors.New("negative max buffer size")
errNegativeMaxMessageSize = errors.New("negative max message size")
)
type bufferOptions struct {
strategy OnFullStrategy
maxBufferSize int
maxMessageSize int
closeCheckInterval time.Duration
dropOldestInterval time.Duration
scanBatchSize int
allowedSpilloverRatio float64
rOpts retry.Options
iOpts instrument.Options
}
// NewOptions creates Options.
func NewOptions() Options {
return &bufferOptions{
strategy: DropOldest,
maxBufferSize: defaultMaxBufferSize,
maxMessageSize: defaultMaxMessageSize,
closeCheckInterval: defaultCloseCheckInterval,
dropOldestInterval: defaultDropOldestInterval,
scanBatchSize: defaultScanBatchSize,
allowedSpilloverRatio: defaultAllowedSpilloverRatio,
rOpts: retry.NewOptions().
SetInitialBackoff(defaultCleanupInitialBackoff).
SetMaxBackoff(defaultCleanupMaxBackoff).
SetForever(true),
iOpts: instrument.NewOptions(),
}
}
func (opts *bufferOptions) OnFullStrategy() OnFullStrategy {
return opts.strategy
}
func (opts *bufferOptions) SetOnFullStrategy(value OnFullStrategy) Options {
o := *opts
o.strategy = value
return &o
}
func (opts *bufferOptions) MaxMessageSize() int {
return opts.maxMessageSize
}
func (opts *bufferOptions) SetMaxMessageSize(value int) Options {
o := *opts
o.maxMessageSize = value
return &o
}
func (opts *bufferOptions) MaxBufferSize() int {
return opts.maxBufferSize
}
func (opts *bufferOptions) SetMaxBufferSize(value int) Options {
o := *opts
o.maxBufferSize = value
return &o
}
func (opts *bufferOptions) CloseCheckInterval() time.Duration {
return opts.closeCheckInterval
}
func (opts *bufferOptions) SetCloseCheckInterval(value time.Duration) Options {
o := *opts
o.closeCheckInterval = value
return &o
}
func (opts *bufferOptions) DropOldestInterval() time.Duration {
return opts.dropOldestInterval
}
func (opts *bufferOptions) SetDropOldestInterval(value time.Duration) Options {
o := *opts
o.dropOldestInterval = value
return &o
}
func (opts *bufferOptions) ScanBatchSize() int {
return opts.scanBatchSize
}
func (opts *bufferOptions) SetScanBatchSize(value int) Options {
o := *opts
o.scanBatchSize = value
return &o
}
func (opts *bufferOptions) AllowedSpilloverRatio() float64 {
return opts.allowedSpilloverRatio
}
func (opts *bufferOptions) SetAllowedSpilloverRatio(value float64) Options {
o := *opts
o.allowedSpilloverRatio = value
return &o
}
func (opts *bufferOptions) CleanupRetryOptions() retry.Options {
return opts.rOpts
}
func (opts *bufferOptions) SetCleanupRetryOptions(value retry.Options) Options {
o := *opts
o.rOpts = value
return &o
}
func (opts *bufferOptions) InstrumentOptions() instrument.Options {
return opts.iOpts
}
func (opts *bufferOptions) SetInstrumentOptions(value instrument.Options) Options {
o := *opts
o.iOpts = value
return &o
}
func (opts *bufferOptions) Validate() error {
if opts.ScanBatchSize() <= 0 {
return errInvalidScanBatchSize
}
if opts.MaxBufferSize() <= 0 {
return errNegativeMaxBufferSize
}
if opts.MaxMessageSize() <= 0 {
return errNegativeMaxMessageSize
}
if opts.MaxMessageSize() > opts.MaxBufferSize() {
// Max message size can only be as large as max buffer size.
return errInvalidMaxMessageSize
}
return nil
}