Skip to content

Commit

Permalink
remove finilisier
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Aug 19, 2024
1 parent 3615feb commit d8cb51f
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions share/eds/cache/accessor_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"runtime"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -238,7 +237,7 @@ func (bc *AccessorCache) EnableMetrics() (CloseMetricsFn, error) {
// Close is called
type refCloser struct {
*accessorWithBlockstore
closed atomic.Bool
closeFn func()
removeRef func()
}

Expand All @@ -248,31 +247,17 @@ func newRefCloser(abs *accessorWithBlockstore) (*refCloser, error) {
return nil, err
}

rf := &refCloser{
var closeOnce sync.Once
return &refCloser{
accessorWithBlockstore: abs,
removeRef: abs.removeRef,
}
// Set finalizer to ensure that accessor is closed when refCloser is garbage collected.
// We expect that refCloser will be closed explicitly by the caller. If it is not closed,
// we log an error.
runtime.SetFinalizer(rf, func(rf *refCloser) {
if rf.close() {
log.Error("refCloser for accessor was garbage collected before Close was called")
}
})
return rf, nil
}

func (c *refCloser) close() bool {
if c.closed.CompareAndSwap(false, true) {
c.removeRef()
return true
}
return false
closeFn: func() {
closeOnce.Do(abs.removeRef)
},
}, nil
}

func (c *refCloser) Close() error {
c.close()
c.closeFn()
return nil
}

Expand Down

0 comments on commit d8cb51f

Please sign in to comment.