From 009755c98188875e8777ba00c84f8b003b2e203f Mon Sep 17 00:00:00 2001 From: Sudo-Ivan Date: Tue, 30 Dec 2025 21:13:42 -0600 Subject: [PATCH] refactor: implement read-write locking for knownDestinations to improve concurrency --- pkg/identity/identity.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/identity/identity.go b/pkg/identity/identity.go index 541d8c3..a5b99ca 100644 --- a/pkg/identity/identity.go +++ b/pkg/identity/identity.go @@ -56,9 +56,10 @@ type Identity struct { } var ( - knownDestinations = make(map[string][]interface{}) - knownRatchets = make(map[string][]byte) - ratchetPersistLock sync.Mutex + knownDestinations = make(map[string][]interface{}) + knownDestinationsLock sync.RWMutex + knownRatchets = make(map[string][]byte) + ratchetPersistLock sync.Mutex ) func New() (*Identity, error) { @@ -189,12 +190,14 @@ func Remember(packet []byte, destHash []byte, publicKey []byte, appData []byte) // Store destination data as [packet, destHash, identity, appData] id := FromPublicKey(publicKey) + knownDestinationsLock.Lock() knownDestinations[hashStr] = []interface{}{ packet, destHash, id, appData, } + knownDestinationsLock.Unlock() } func ValidateAnnounce(packet []byte, destHash []byte, publicKey []byte, signature []byte, appData []byte) bool { @@ -251,7 +254,11 @@ func (i *Identity) String() string { func Recall(hash []byte) (*Identity, error) { hashStr := hex.EncodeToString(hash) - if data, exists := knownDestinations[hashStr]; exists { + knownDestinationsLock.RLock() + data, exists := knownDestinations[hashStr] + knownDestinationsLock.RUnlock() + + if exists { // data is [packet, destHash, identity, appData] if len(data) >= 3 { if id, ok := data[2].(*Identity); ok { @@ -636,7 +643,6 @@ func (i *Identity) loadPrivateKey(privateKey, signingSeed []byte) error { signingKey := ed25519.NewKeyFromSeed(i.signingSeed) i.verificationKey = signingKey.Public().(ed25519.PublicKey) - // Update hash publicKeyBytes := make([]byte, 0, len(i.publicKey)+len(i.verificationKey)) publicKeyBytes = append(publicKeyBytes, i.publicKey...) publicKeyBytes = append(publicKeyBytes, i.verificationKey...) @@ -854,7 +860,10 @@ func (i *Identity) GetRatchetID(ratchetPubBytes []byte) []byte { } func GetKnownDestination(hash string) ([]interface{}, bool) { - if data, exists := knownDestinations[hash]; exists { + knownDestinationsLock.RLock() + data, exists := knownDestinations[hash] + knownDestinationsLock.RUnlock() + if exists { return data, true } return nil, false