Skip to content

Commit 9bcba1e

Browse files
authored
Merge pull request #3 from globocom/refactor-options
refactor initialization of options
2 parents bfb9996 + 0cc2830 commit 9bcba1e

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

buffer.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,12 @@ func newTicker(interval time.Duration) (<-chan time.Time, func()) {
113113

114114
// New creates a new buffer instance with the provided options.
115115
func New(opts ...Option) *Buffer {
116-
options := &Options{
117-
Size: 0,
118-
Flusher: nil,
119-
FlushInterval: 0,
120-
PushTimeout: time.Second,
121-
FlushTimeout: time.Second,
122-
CloseTimeout: time.Second,
123-
}
124-
125-
for _, opt := range opts {
126-
opt(options)
127-
}
128-
129-
if err := validateOptions(options); err != nil {
130-
panic(err.Error())
131-
}
132-
133116
buffer := &Buffer{
134117
dataCh: make(chan interface{}),
135118
flushCh: make(chan struct{}),
136119
closeCh: make(chan struct{}),
137120
doneCh: make(chan struct{}),
138-
options: options,
121+
options: resolveOptions(opts...),
139122
}
140123
go buffer.consume()
141124

options.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,24 @@ func validateOptions(options *Options) error {
9292

9393
return nil
9494
}
95+
96+
func resolveOptions(opts ...Option) *Options {
97+
options := &Options{
98+
Size: 0,
99+
Flusher: nil,
100+
FlushInterval: 0,
101+
PushTimeout: time.Second,
102+
FlushTimeout: time.Second,
103+
CloseTimeout: time.Second,
104+
}
105+
106+
for _, opt := range opts {
107+
opt(options)
108+
}
109+
110+
if err := validateOptions(options); err != nil {
111+
panic(err)
112+
}
113+
114+
return options
115+
}

0 commit comments

Comments
 (0)