-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoption.go
More file actions
88 lines (78 loc) · 2.11 KB
/
Copy pathoption.go
File metadata and controls
88 lines (78 loc) · 2.11 KB
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
package dsqueue
import (
"time"
)
const (
DefaultBufferSize = 16 * 1024
DefaultIdleWriteTime = time.Minute
DefaultCloseTimeout = 10 * time.Second
)
// config contains all options for DSQueue.
type config struct {
bufferSize int
dedupCacheSize int
idleWriteTime time.Duration
closeTimeout time.Duration
}
// Option is a function that sets a value in a config.
type Option func(*config)
// getOpts creates a config and applies Options to it.
func getOpts(opts []Option) config {
cfg := config{
bufferSize: DefaultBufferSize,
idleWriteTime: DefaultIdleWriteTime,
closeTimeout: DefaultCloseTimeout,
}
for _, opt := range opts {
opt(&cfg)
}
return cfg
}
// WithBufferSize sets the limit on number of items kept in input buffer
// memory, at which they are all written to the datastore. A value of 0 means
// the buffer size is unlimited, and items are only written to the datastore
// when the queue has been idle more then the idle write time or when the queue
// is closed.
func WithBufferSize(n int) Option {
return func(c *config) {
if n < 0 {
n = 0
}
c.bufferSize = n
}
}
// WithDedupCacheSize sets the size of the LRU cache used to deduplicate items
// in the queue.
//
// By default, the deduplication cache is disabled (size = 0).
func WithDedupCacheSize(n int) Option {
return func(c *config) {
if n < 0 {
n = 0
}
c.dedupCacheSize = n
}
}
// WithIdleWriteTime sets the amout of time that the queue must be idle (no
// input or output) before all buffered input items are written to the
// datastore. A value of zero means that buffered input items are not
// automatically flushed to the datastore. A non-zero value must be greater
// than one second.
func WithIdleWriteTime(d time.Duration) Option {
return func(c *config) {
if d != 0 && d < time.Second {
d = time.Second
}
c.idleWriteTime = d
}
}
// WithCloseTimeout sets the duration that Close waits to finish writing items
// to the datastore. A value of 0 means wait until finished with no timeout.
func WithCloseTimeout(d time.Duration) Option {
return func(c *config) {
if d < 0 {
d = 0
}
c.closeTimeout = d
}
}