-
Notifications
You must be signed in to change notification settings - Fork 75
Implement thread safe interface for the OrderedMap #63
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
base: master
Are you sure you want to change the base?
Conversation
|
Thanks for this! I'm happy to accept this, with one small change:
I think it's reasonable to note in the docs for
Please create separate diffs v1 and v2 as they will be released separately as new minor versions. |
|
@elliotchance thanks for the quick feedback I tried to add this test to Running the test using We can think about some scenario where the
You mean a separate PR for |
|
@nem0z I think what you have already is good. Yes, if the caller directly accesses the As for safe iterating, I think the best way is to provide a read-locked iterator and so all items can be processed or extracted safely. But this can be done in a separate PR, if you wish. Are you ready for me to accept this? |
|
Okay so what about exposing methods to take and release a read lock? For example with I can update my PR to add these 2 methods and it should be good for me |
|
@elliotchance I updated the PR with what we mentioned above + some extra tests So basically it's possible to manually use the This is tested with I added an extra test which also serves as example for safe iteration. On thing to note and it's very important, if you manually take a read-lock, you cannot use sync methods before in the same goroutine before releasing the lock. This is because 1 single lock can be hold by each goroutine, this is an example:
So a solution would either be to use the non-sync method And for this reason I don't know if it's really sensible to implement thread-safe version of the iterators, because this will abstract the lock taken by the iterator so users can be tempted to call a thread-safe method in the iterator scope and that would not work (the goroutine is blocked). It's still possible to use the iterator by manually managing the lock as in the examples above. If you're good with the current implemented we can go ahead and merge, then I can start working on the integration to |
This PR add a struct
SyncOrderedMapwhich is basically a Thread safe wrapped toOrderedMapAs mentioned in this comment: #62 (comment)
I understand implementing thread safe operation directly in the
OrderedMapisn't what we want as it would add an extra complexity to scenarios where thread safe isn't required, adding another structSyncOrderedMapgive the consumer the choice to use or not the thread safe feature.I assumed that methods such as
GetElementand the iterators are not really compatible with thread safe given that it will provide access to the inner component of theOrderedMapwhich could then be used a non thread safe manner.Example we could totally return a
*Elementsafely but using this Element to read/write the key or the value would kind of "corrupt" theSyncOrderedMapFor the same reason I didn't support
NewOrderedMapWithElementsforSyncOrderedMap, however I think a method onOrderedMapor another constructor forSyncOrderedMapwhich would create a newSyncOrderedMapfrom an existingOrderedMapcould be interesting, this would need to copy the value though, as we don't want values to be still accessible from a non thread safeOrderedMap.Test:
I added a unit test verifying that the
SyncOrderedMapis race condition free, not sure if it's enough for you?I think we could reimplement some of the test for this new struct to make sure it doesn't introduce any regression?
Question:
Should I also put my changes on
v1andv2given that it doesn't introduce any breaking chance?@elliotchance
This change is