Skip to content

Commit 0cc2830

Browse files
committed
refactor initialization of option
1 parent ee91747 commit 0cc2830

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
@@ -110,29 +110,12 @@ func newTicker(interval time.Duration) (<-chan time.Time, func()) {
110110

111111
// New creates a new buffer instance with the provided options.
112112
func New(opts ...Option) *Buffer {
113-
options := &Options{
114-
Size: 0,
115-
Flusher: nil,
116-
FlushInterval: 0,
117-
PushTimeout: time.Second,
118-
FlushTimeout: time.Second,
119-
CloseTimeout: time.Second,
120-
}
121-
122-
for _, opt := range opts {
123-
opt(options)
124-
}
125-
126-
if err := validateOptions(options); err != nil {
127-
panic(err.Error())
128-
}
129-
130113
buffer := &Buffer{
131114
dataCh: make(chan interface{}),
132115
flushCh: make(chan struct{}),
133116
closeCh: make(chan struct{}),
134117
doneCh: make(chan struct{}),
135-
options: options,
118+
options: resolveOptions(opts...),
136119
}
137120
go buffer.consume()
138121

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)