-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1640 from josephschorr/singleflight-recursion
Fix handling of recursive calls via singleflight dispatch
- Loading branch information
Showing
17 changed files
with
394 additions
and
139 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
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
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,50 @@ | ||
package mapz | ||
|
||
import "sync" | ||
|
||
// CountingMultiMap is a multimap that counts the number of distinct values for each | ||
// key, removing the key from the map when the count reaches zero. Safe for concurrent | ||
// use. | ||
type CountingMultiMap[T comparable, Q comparable] struct { | ||
valuesByKey map[T]*Set[Q] | ||
lock sync.Mutex | ||
} | ||
|
||
// NewCountingMultiMap constructs a new counting multimap. | ||
func NewCountingMultiMap[T comparable, Q comparable]() *CountingMultiMap[T, Q] { | ||
return &CountingMultiMap[T, Q]{ | ||
valuesByKey: map[T]*Set[Q]{}, | ||
lock: sync.Mutex{}, | ||
} | ||
} | ||
|
||
// Add adds the given value to the map at the given key. Returns true if the value | ||
// already existed in the map for the given key. | ||
func (cmm *CountingMultiMap[T, Q]) Add(key T, value Q) bool { | ||
cmm.lock.Lock() | ||
defer cmm.lock.Unlock() | ||
|
||
values, ok := cmm.valuesByKey[key] | ||
if !ok { | ||
values = NewSet[Q]() | ||
cmm.valuesByKey[key] = values | ||
} | ||
return !values.Add(value) | ||
} | ||
|
||
// Remove removes the given value for the given key from the map. If, after this removal, | ||
// the key has no additional values, it is removed entirely from the map. | ||
func (cmm *CountingMultiMap[T, Q]) Remove(key T, value Q) { | ||
cmm.lock.Lock() | ||
defer cmm.lock.Unlock() | ||
|
||
values, ok := cmm.valuesByKey[key] | ||
if !ok { | ||
return | ||
} | ||
|
||
values.Remove(value) | ||
if values.IsEmpty() { | ||
delete(cmm.valuesByKey, key) | ||
} | ||
} |
Oops, something went wrong.