Skip to content

Commit

Permalink
change SetFinalizer to pass single ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
dphilla committed Jun 18, 2024
1 parent f70a46d commit 9006330
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ func (m *module[T]) setSignature(signature T) {
}

func (m *module[T]) EnsureSetFinalizer() {
runtime.SetFinalizer(&m, m.close)
runtime.SetFinalizer(m, func(m *module[T]) {
m.close(m)
})
}

func (m *module[T]) Close(_ *module[T]) {
Expand Down
7 changes: 5 additions & 2 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func newModulePool[T interfaces.Signature](ctx context.Context, template *templa
return &modulePool[T]{
maxSize: maxSize,
new: func() (*module[T], error) {
return newModule[T](ctx, template)
m, err := newModule[T](ctx, template)
if m != nil {
m.EnsureSetFinalizer()
}
return m, err
},
close: func(m *module[T]) {
m.Close(m)
Expand Down Expand Up @@ -84,7 +88,6 @@ func (p *modulePool[T]) Get() (*module[T], error) {
if p.maxSize == 0 {
m, ok := p.pool.Get().(*module[T])
if ok && m != nil {
m.EnsureSetFinalizer()
return m, nil
}
return p.new()
Expand Down

0 comments on commit 9006330

Please sign in to comment.