-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
94 lines (79 loc) · 2.46 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
// Copyright 2019 Yegor Myskin. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tailor
import (
"io"
"time"
)
// options is the main options store.
// To see what's going on, watch for With* descriptions below.
type options struct {
runOffset int64
runWhence int
reopenOffset int64
reopenWhence int
bufioReaderPoolSize int
pollerTimeout time.Duration
updateLagInterval time.Duration
rateLimiter RateLimiter
leakyBucket bool
}
// withDefaultOptions sets the initial options.
func withDefaultOptions() []Option {
return []Option{
WithPollerTimeout(10 * time.Millisecond),
WithReaderInitialPoolSize(4096),
WithSeekOnStartup(0, io.SeekEnd),
WithSeekOnReopen(0, io.SeekStart),
WithUpdateLagInterval(5 * time.Second),
}
}
type Option func(options *options)
// WithPollerTimeout is used to timeout when file is fully read, to check changes.
func WithPollerTimeout(duration time.Duration) Option {
return func(options *options) {
options.pollerTimeout = duration
}
}
// WithReaderInitialPoolSize is used to set the internal initial size of bufio.Reader buffer.
func WithReaderInitialPoolSize(size int) Option {
return func(options *options) {
options.bufioReaderPoolSize = size
}
}
// WithSeekOnStartup is used to set file.Seek() options, when the file is opened on startup.
// Use io.Seek* constants to set whence.
func WithSeekOnStartup(offset int64, whence int) Option {
return func(options *options) {
options.runOffset = offset
options.runWhence = whence
}
}
// WithSeekOnRun is used to set file.Seek() options, when the file is opened on startup.
// Use io.Seek* constants to set whence.
func WithSeekOnReopen(offset int64, whence int) Option {
return func(options *options) {
options.reopenOffset = offset
options.reopenWhence = whence
}
}
// WithUpdateLagInterval is used to know how often update the file lag.
// Frequent update time increasing Seek syscall calls.
func WithUpdateLagInterval(duration time.Duration) Option {
return func(options *options) {
options.updateLagInterval = duration
}
}
// WithRateLimiter is used to rate limit output lines. Watch RateLimiter interface.
func WithRateLimiter(rl RateLimiter) Option {
return func(options *options) {
options.rateLimiter = rl
}
}
// WithLeakyBucket is used to skip a read lines, when a listener is full.
func WithLeakyBucket() Option {
return func(options *options) {
options.leakyBucket = true
}
}