Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filedef listener add ability to reset #457

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions profile/filedef/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,44 @@ var _ decoder.MesgListener = (*Listener)(nil)
// This will handle message conversion from proto.Message received from Decoder into
// mesgdef's structure and group it by its correspoding defined file types.
func NewListener(opts ...Option) *Listener {
l := &Listener{
options: defaultOptions(),
active: true,
}
l := new(Listener)
l.Reset(opts...)
return l
}

// Reset resets the Listener for reuse. It resets options to default options so any
// options needs to be inputed again. It is similar to NewListener() but it retains
// the underlying storage for use by future decode to reduce memory allocs.
func (l *Listener) Reset(opts ...Option) {
l.Close()
prevChannelBuffer := l.options.channelBuffer
l.options = defaultOptions()
for i := range opts {
opts[i](&l.options)
}

l.reset()

l.poolc = make(chan []proto.Field, l.options.channelBuffer)
for i := uint(0); i < l.options.channelBuffer; i++ {
l.poolc <- nil // fill pool with nil slice, alloc as needed.
if prevChannelBuffer != l.options.channelBuffer {
prevPoolc := l.poolc
l.poolc = make(chan []proto.Field, l.options.channelBuffer)
for i := uint(0); i < l.options.channelBuffer; i++ {
select {
case v := <-prevPoolc:
l.poolc <- v // fill with previously allocated slice.
default:
l.poolc <- nil // fill pool with nil slice, alloc as needed.
}
}
}
l.reset()
l.active = true

go l.loop()
}

return l
func (l *Listener) reset() {
l.file = nil
l.mesgc = make(chan proto.Message, l.options.channelBuffer)
l.done = make(chan struct{})
}

func (l *Listener) loop() {
Expand Down Expand Up @@ -185,9 +205,3 @@ func (l *Listener) File() File {
l.Close()
return l.file
}

func (l *Listener) reset() {
l.file = nil
l.mesgc = make(chan proto.Message, l.options.channelBuffer)
l.done = make(chan struct{})
}
5 changes: 5 additions & 0 deletions profile/filedef/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,8 @@ func TestClose(t *testing.T) {
l.Close()
l.Close() // already closed, should not panic
}

func TestReset(t *testing.T) {
l := filedef.NewListener()
l.Reset(filedef.WithChannelBuffer(256))
}