bool -> atomic.Bool to prevent "race condition"
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Sarah Jamie Lewis 2023-02-28 10:33:00 -08:00
parent a6a196a1c1
commit 243b827522
1 changed files with 5 additions and 5 deletions

View File

@ -50,7 +50,7 @@ type engine struct {
authorizations sync.Map // string(onion) => model.Authorization authorizations sync.Map // string(onion) => model.Authorization
// Block Unknown Contacts // Block Unknown Contacts
blockUnknownContacts bool blockUnknownContacts atomic.Bool
// Pointer to the Global Event Manager // Pointer to the Global Event Manager
eventManager event.Manager eventManager event.Manager
@ -240,10 +240,10 @@ func (e *engine) eventHandler() {
} }
case event.AllowUnknownPeers: case event.AllowUnknownPeers:
log.Debugf("%v now allows unknown connections", e.identity.Hostname()) log.Debugf("%v now allows unknown connections", e.identity.Hostname())
e.blockUnknownContacts = false e.blockUnknownContacts.Store(false)
case event.BlockUnknownPeers: case event.BlockUnknownPeers:
log.Debugf("%v now forbids unknown connections", e.identity.Hostname()) log.Debugf("%v now forbids unknown connections", e.identity.Hostname())
e.blockUnknownContacts = true e.blockUnknownContacts.Store(true)
case event.ProtocolEngineStartListen: case event.ProtocolEngineStartListen:
go e.listenFn() go e.listenFn()
case event.ShareManifest: case event.ShareManifest:
@ -285,7 +285,7 @@ func (e *engine) isBlocked(onion string) bool {
authorization, known := e.authorizations.Load(onion) authorization, known := e.authorizations.Load(onion)
if !known { if !known {
// if we block unknown peers we will block this contact // if we block unknown peers we will block this contact
return e.blockUnknownContacts return e.blockUnknownContacts.Load()
} }
return authorization.(model.Authorization) == model.AuthBlocked return authorization.(model.Authorization) == model.AuthBlocked
} }
@ -296,7 +296,7 @@ func (e *engine) isAllowed(onion string) bool {
log.Errorf("attempted to lookup authorization of onion not in map...that should never happen") log.Errorf("attempted to lookup authorization of onion not in map...that should never happen")
return false return false
} }
if e.blockUnknownContacts { if e.blockUnknownContacts.Load() {
return authorization.(model.Authorization) == model.AuthApproved return authorization.(model.Authorization) == model.AuthApproved
} }
return authorization.(model.Authorization) != model.AuthBlocked return authorization.(model.Authorization) != model.AuthBlocked