refactor: implement read-write locking for knownDestinations to improve concurrency
This commit is contained in:
@@ -57,6 +57,7 @@ type Identity struct {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
knownDestinations = make(map[string][]interface{})
|
knownDestinations = make(map[string][]interface{})
|
||||||
|
knownDestinationsLock sync.RWMutex
|
||||||
knownRatchets = make(map[string][]byte)
|
knownRatchets = make(map[string][]byte)
|
||||||
ratchetPersistLock sync.Mutex
|
ratchetPersistLock sync.Mutex
|
||||||
)
|
)
|
||||||
@@ -189,12 +190,14 @@ func Remember(packet []byte, destHash []byte, publicKey []byte, appData []byte)
|
|||||||
|
|
||||||
// Store destination data as [packet, destHash, identity, appData]
|
// Store destination data as [packet, destHash, identity, appData]
|
||||||
id := FromPublicKey(publicKey)
|
id := FromPublicKey(publicKey)
|
||||||
|
knownDestinationsLock.Lock()
|
||||||
knownDestinations[hashStr] = []interface{}{
|
knownDestinations[hashStr] = []interface{}{
|
||||||
packet,
|
packet,
|
||||||
destHash,
|
destHash,
|
||||||
id,
|
id,
|
||||||
appData,
|
appData,
|
||||||
}
|
}
|
||||||
|
knownDestinationsLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateAnnounce(packet []byte, destHash []byte, publicKey []byte, signature []byte, appData []byte) bool {
|
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) {
|
func Recall(hash []byte) (*Identity, error) {
|
||||||
hashStr := hex.EncodeToString(hash)
|
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]
|
// data is [packet, destHash, identity, appData]
|
||||||
if len(data) >= 3 {
|
if len(data) >= 3 {
|
||||||
if id, ok := data[2].(*Identity); ok {
|
if id, ok := data[2].(*Identity); ok {
|
||||||
@@ -636,7 +643,6 @@ func (i *Identity) loadPrivateKey(privateKey, signingSeed []byte) error {
|
|||||||
signingKey := ed25519.NewKeyFromSeed(i.signingSeed)
|
signingKey := ed25519.NewKeyFromSeed(i.signingSeed)
|
||||||
i.verificationKey = signingKey.Public().(ed25519.PublicKey)
|
i.verificationKey = signingKey.Public().(ed25519.PublicKey)
|
||||||
|
|
||||||
// Update hash
|
|
||||||
publicKeyBytes := make([]byte, 0, len(i.publicKey)+len(i.verificationKey))
|
publicKeyBytes := make([]byte, 0, len(i.publicKey)+len(i.verificationKey))
|
||||||
publicKeyBytes = append(publicKeyBytes, i.publicKey...)
|
publicKeyBytes = append(publicKeyBytes, i.publicKey...)
|
||||||
publicKeyBytes = append(publicKeyBytes, i.verificationKey...)
|
publicKeyBytes = append(publicKeyBytes, i.verificationKey...)
|
||||||
@@ -854,7 +860,10 @@ func (i *Identity) GetRatchetID(ratchetPubBytes []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetKnownDestination(hash string) ([]interface{}, bool) {
|
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 data, true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
|
|||||||
Reference in New Issue
Block a user