-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: PRT: Fix all sync.Map uses (#1688)
* Create and use SafeSyncMap * Change from Load and then Store to LoadOrStore * fixed elad * fix elad 2 * Fix chaintracker test * Add more missing StartAndServe * Small fix to rpcconsumer * CR Fix: Add description to why we do Load and then Store * CR Fix: Log error instead of Fatal when erroring on Load * tidy code * push --------- Co-authored-by: Ran Mishael <ran@lavanet.xyz> Co-authored-by: Omer <100387053+omerlavanet@users.noreply.github.com>
- Loading branch information
1 parent
28a53a3
commit aae38b9
Showing
12 changed files
with
183 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package common | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/lavanet/lava/v3/utils" | ||
) | ||
|
||
type SafeSyncMap[K, V any] struct { | ||
localMap sync.Map | ||
} | ||
|
||
func (ssm *SafeSyncMap[K, V]) Store(key K, toSet V) { | ||
ssm.localMap.Store(key, toSet) | ||
} | ||
|
||
func (ssm *SafeSyncMap[K, V]) Load(key K) (ret V, ok bool, err error) { | ||
value, ok := ssm.localMap.Load(key) | ||
if !ok { | ||
return ret, ok, nil | ||
} | ||
ret, ok = value.(V) | ||
if !ok { | ||
return ret, false, utils.LavaFormatError("invalid usage of syncmap, could not cast result into a PolicyUpdater", nil) | ||
} | ||
return ret, true, nil | ||
} | ||
|
||
// LoadOrStore returns the existing value for the key if present. | ||
// Otherwise, it stores and returns the given value. | ||
// The loaded result is true if the value was loaded, false if stored. | ||
// The function returns the value that was loaded or stored. | ||
func (ssm *SafeSyncMap[K, V]) LoadOrStore(key K, value V) (ret V, loaded bool, err error) { | ||
actual, loaded := ssm.localMap.LoadOrStore(key, value) | ||
if loaded { | ||
// loaded from map | ||
var ok bool | ||
ret, ok = actual.(V) | ||
if !ok { | ||
return ret, false, utils.LavaFormatError("invalid usage of sync map, could not cast result into a PolicyUpdater", nil) | ||
} | ||
return ret, true, nil | ||
} | ||
|
||
// stored in map | ||
return value, false, nil | ||
} | ||
|
||
func (ssm *SafeSyncMap[K, V]) Range(f func(key, value any) bool) { | ||
ssm.localMap.Range(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.