-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix concurrent map writes #1732
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package useraccountmanager | |
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
gethlog "github.com/ethereum/go-ethereum/log" | ||
|
@@ -18,6 +19,7 @@ type UserAccountManager struct { | |
storage storage.Storage | ||
hostRPCBinAddr string | ||
logger gethlog.Logger | ||
mu sync.Mutex | ||
} | ||
|
||
func NewUserAccountManager(unauthenticatedClient rpc.Client, logger gethlog.Logger, storage storage.Storage, hostRPCBindAddr string) UserAccountManager { | ||
|
@@ -32,6 +34,8 @@ func NewUserAccountManager(unauthenticatedClient rpc.Client, logger gethlog.Logg | |
|
||
// AddAndReturnAccountManager adds new UserAccountManager if it doesn't exist and returns it, if UserAccountManager already exists for that user just return it | ||
func (m *UserAccountManager) AddAndReturnAccountManager(userID string) *accountmanager.AccountManager { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
existingUserAccountManager, exists := m.userAccountManager[userID] | ||
if exists { | ||
return existingUserAccountManager | ||
|
@@ -47,6 +51,8 @@ func (m *UserAccountManager) AddAndReturnAccountManager(userID string) *accountm | |
// (we are not loading all of them at startup to limit the number of established connections) | ||
// If a UserAccountManager does not exist for the userID, it returns nil and an error. | ||
func (m *UserAccountManager) GetUserAccountManager(userID string) (*accountmanager.AccountManager, error) { | ||
m.mu.Lock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth using RWMutex and just doing read lock for this read op? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, using |
||
defer m.mu.Unlock() | ||
userAccManager, exists := m.userAccountManager[userID] | ||
if !exists { | ||
return nil, fmt.Errorf("UserAccountManager doesn't exist for user: %s", userID) | ||
|
@@ -103,6 +109,8 @@ func (m *UserAccountManager) GetUserAccountManager(userID string) (*accountmanag | |
// DeleteUserAccountManager removes the UserAccountManager associated with the given userID. | ||
// It returns an error if no UserAccountManager exists for that userID. | ||
func (m *UserAccountManager) DeleteUserAccountManager(userID string) error { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
_, exists := m.userAccountManager[userID] | ||
if !exists { | ||
return fmt.Errorf("no UserAccountManager exists for userID %s", userID) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ensure that the PR checklist, as outlined in the project's development operations documentation, is completed and confirmed.