Using atomic bool

This commit is contained in:
Michal Horejsek
2020-11-04 13:40:39 +01:00
committed by Jakub Cuth
parent b9740e1b7d
commit 11a0dec047

View File

@ -21,6 +21,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings" "strings"
"sync/atomic"
"github.com/ProtonMail/proton-bridge/pkg/pmapi" "github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -39,7 +40,7 @@ type Mailbox struct {
log *logrus.Entry log *logrus.Entry
isDeleting bool isDeleting atomic.Value
} }
func newMailbox(storeAddress *Address, labelID, labelPrefix, labelName, color string) (mb *Mailbox, err error) { func newMailbox(storeAddress *Address, labelID, labelPrefix, labelName, color string) (mb *Mailbox, err error) {
@ -61,6 +62,7 @@ func txNewMailbox(tx *bolt.Tx, storeAddress *Address, labelID, labelPrefix, labe
color: color, color: color,
log: l, log: l,
} }
mb.isDeleting.Store(false)
err := initMailboxBucket(tx, mb.getBucketName()) err := initMailboxBucket(tx, mb.getBucketName())
if err != nil { if err != nil {
@ -217,7 +219,7 @@ func (storeMailbox *Mailbox) Rename(newName string) error {
// Deletion has to be propagated to all the same mailboxes in all addresses. // Deletion has to be propagated to all the same mailboxes in all addresses.
// The propagation is processed by the event loop. // The propagation is processed by the event loop.
func (storeMailbox *Mailbox) Delete() error { func (storeMailbox *Mailbox) Delete() error {
storeMailbox.isDeleting = true storeMailbox.isDeleting.Store(true)
return storeMailbox.storeAddress.deleteMailbox(storeMailbox.labelID) return storeMailbox.storeAddress.deleteMailbox(storeMailbox.labelID)
} }
@ -229,7 +231,7 @@ func (storeMailbox *Mailbox) GetDelimiter() string {
// deleteMailboxEvent deletes the mailbox bucket. // deleteMailboxEvent deletes the mailbox bucket.
// This is called from the event loop. // This is called from the event loop.
func (storeMailbox *Mailbox) deleteMailboxEvent() error { func (storeMailbox *Mailbox) deleteMailboxEvent() error {
if !storeMailbox.isDeleting { if !storeMailbox.isDeleting.Load().(bool) {
// Deleting label removes bucket. Any ongoing connection selected // Deleting label removes bucket. Any ongoing connection selected
// in such mailbox then might panic because of non-existing bucket. // in such mailbox then might panic because of non-existing bucket.
// Closing connetions prevents that panic but if the connection // Closing connetions prevents that panic but if the connection