Make attachment.Close() synchronous

This commit is contained in:
binwiederhier
2026-03-26 09:57:06 -04:00
parent 11a14d8fe7
commit e19ba059b5
+7 -1
View File
@@ -30,6 +30,7 @@ type Store struct {
attachmentsWithSizes func() (map[string]int64, error) // Returns file ID -> size for active attachments attachmentsWithSizes func() (map[string]int64, error) // Returns file ID -> size for active attachments
orphanGracePeriod time.Duration // Don't delete orphaned objects younger than this orphanGracePeriod time.Duration // Don't delete orphaned objects younger than this
closeChan chan struct{} closeChan chan struct{}
doneChan chan struct{}
mu sync.RWMutex // Protects size and sizes mu sync.RWMutex // Protects size and sizes
} }
@@ -61,6 +62,7 @@ func newStore(backend backend, totalSizeLimit int64, orphanGracePeriod time.Dura
attachmentsWithSizes: attachmentsWithSizes, attachmentsWithSizes: attachmentsWithSizes,
orphanGracePeriod: orphanGracePeriod, orphanGracePeriod: orphanGracePeriod,
closeChan: make(chan struct{}), closeChan: make(chan struct{}),
doneChan: make(chan struct{}),
} }
// Hydrate sizes from the database immediately so that Size()/Remaining()/Remove() // Hydrate sizes from the database immediately so that Size()/Remaining()/Remove()
// are accurate from the start, without waiting for the first sync() call. // are accurate from the start, without waiting for the first sync() call.
@@ -74,6 +76,8 @@ func newStore(backend backend, totalSizeLimit int64, orphanGracePeriod time.Dura
c.size += size c.size += size
} }
go c.syncLoop() go c.syncLoop()
} else {
close(c.doneChan)
} }
return c, nil return c, nil
} }
@@ -216,12 +220,14 @@ func (c *Store) Remaining() int64 {
return remaining return remaining
} }
// Close stops the background sync goroutine // Close stops the background sync goroutine and waits for it to finish
func (c *Store) Close() { func (c *Store) Close() {
close(c.closeChan) close(c.closeChan)
<-c.doneChan
} }
func (c *Store) syncLoop() { func (c *Store) syncLoop() {
defer close(c.doneChan)
if err := c.sync(); err != nil { if err := c.sync(); err != nil {
log.Tag(tagStore).Err(err).Warn("Attachment sync failed") log.Tag(tagStore).Err(err).Warn("Attachment sync failed")
} }