-
Notifications
You must be signed in to change notification settings - Fork 455
/
options.go
167 lines (142 loc) · 5.03 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
// Copyright (c) 2016 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 retention
import (
"errors"
"time"
)
const (
// defaultRetentionPeriod is how long we keep data in memory by default.
defaultRetentionPeriod = 2 * 24 * time.Hour
// defaultBlockSize is the default block size
defaultBlockSize = 2 * time.Hour
// defaultBufferFuture is the default buffer future limit
defaultBufferFuture = 2 * time.Minute
// defaultBufferPast is the default buffer past limit
defaultBufferPast = 10 * time.Minute
// defaultDataExpiry is the default bool for whether data expiry is on
defaultDataExpiry = true
// defaultDataExpiryAfterNotAccessedPeriod is the default data expiry after not accessed period
defaultDataExpiryAfterNotAccessedPeriod = 5 * time.Minute
)
var (
errBufferFutureNonNegative = errors.New("buffer future must be non-negative")
errBufferPastNonNegative = errors.New("buffer past must be non-negative")
errBlockSizePositive = errors.New("block size must positive")
errBufferFutureTooLarge = errors.New("buffer future must be smaller than block size")
errBufferPastTooLarge = errors.New("buffer past must be smaller than block size")
errRetentionPeriodTooSmall = errors.New("retention period must not be smaller than block size")
)
type options struct {
retentionPeriod time.Duration
blockSize time.Duration
bufferFuture time.Duration
bufferPast time.Duration
dataExpiry bool
dataExpiryAfterNotAccessedPeriod time.Duration
}
// NewOptions creates new retention options
func NewOptions() Options {
return &options{
retentionPeriod: defaultRetentionPeriod,
blockSize: defaultBlockSize,
bufferFuture: defaultBufferFuture,
bufferPast: defaultBufferPast,
dataExpiry: defaultDataExpiry,
dataExpiryAfterNotAccessedPeriod: defaultDataExpiryAfterNotAccessedPeriod,
}
}
func (o *options) Validate() error {
if o.bufferFuture < 0 {
return errBufferFutureNonNegative
}
if o.bufferPast < 0 {
return errBufferPastNonNegative
}
if o.blockSize <= 0 {
return errBlockSizePositive
}
if o.bufferFuture >= o.blockSize {
return errBufferFutureTooLarge
}
if o.bufferPast >= o.blockSize {
return errBufferPastTooLarge
}
if o.retentionPeriod < o.blockSize {
return errRetentionPeriodTooSmall
}
return nil
}
func (o *options) Equal(value Options) bool {
return o.retentionPeriod == value.RetentionPeriod() &&
o.blockSize == value.BlockSize() &&
o.bufferFuture == value.BufferFuture() &&
o.bufferPast == value.BufferPast() &&
o.dataExpiry == value.BlockDataExpiry() &&
o.dataExpiryAfterNotAccessedPeriod == value.BlockDataExpiryAfterNotAccessedPeriod()
}
func (o *options) SetRetentionPeriod(value time.Duration) Options {
opts := *o
opts.retentionPeriod = value
return &opts
}
func (o *options) RetentionPeriod() time.Duration {
return o.retentionPeriod
}
func (o *options) SetBlockSize(value time.Duration) Options {
opts := *o
opts.blockSize = value
return &opts
}
func (o *options) BlockSize() time.Duration {
return o.blockSize
}
func (o *options) SetBufferFuture(value time.Duration) Options {
opts := *o
opts.bufferFuture = value
return &opts
}
func (o *options) BufferFuture() time.Duration {
return o.bufferFuture
}
func (o *options) SetBufferPast(value time.Duration) Options {
opts := *o
opts.bufferPast = value
return &opts
}
func (o *options) BufferPast() time.Duration {
return o.bufferPast
}
func (o *options) SetBlockDataExpiry(value bool) Options {
opts := *o
opts.dataExpiry = value
return &opts
}
func (o *options) BlockDataExpiry() bool {
return o.dataExpiry
}
func (o *options) SetBlockDataExpiryAfterNotAccessedPeriod(value time.Duration) Options {
opts := *o
opts.dataExpiryAfterNotAccessedPeriod = value
return &opts
}
func (o *options) BlockDataExpiryAfterNotAccessedPeriod() time.Duration {
return o.dataExpiryAfterNotAccessedPeriod
}