-
Notifications
You must be signed in to change notification settings - Fork 50
dynamicstream: add area before add path #3998
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -296,15 +296,37 @@ type memControl[A Area, P Path, T Event, D Dest, H Handler[A, P, T, D]] struct { | |
| // Since this struct is global level, different streams may access it concurrently. | ||
| mutex sync.Mutex | ||
|
|
||
| areaStatMap map[A]*areaMemStat[A, P, T, D, H] | ||
| areaStatMap map[A]*areaMemStat[A, P, T, D, H] | ||
| areaSettingsMap map[A]AreaSettings | ||
| } | ||
|
|
||
| func newMemControl[A Area, P Path, T Event, D Dest, H Handler[A, P, T, D]]() *memControl[A, P, T, D, H] { | ||
| return &memControl[A, P, T, D, H]{ | ||
| areaStatMap: make(map[A]*areaMemStat[A, P, T, D, H]), | ||
| areaStatMap: make(map[A]*areaMemStat[A, P, T, D, H]), | ||
| areaSettingsMap: make(map[A]AreaSettings), | ||
| } | ||
| } | ||
|
|
||
| func (m *memControl[A, P, T, D, H]) addArea(area A, settings AreaSettings) { | ||
| settings.fix() | ||
|
|
||
| m.mutex.Lock() | ||
| defer m.mutex.Unlock() | ||
|
|
||
| m.areaSettingsMap[area] = settings | ||
| if as, ok := m.areaStatMap[area]; ok { | ||
| as.settings.Store(&settings) | ||
| } | ||
| } | ||
|
Comment on lines
+310
to
+320
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. The You should allocate if as, ok := m.areaStatMap[area]; ok {
// The `settings` is a copy on the stack, we can't store a pointer to it.
// So we need to allocate a new one on the heap.
s := new(AreaSettings)
*s = settings
as.settings.Store(s)
} |
||
|
|
||
| func (m *memControl[A, P, T, D, H]) removeArea(area A) { | ||
| m.mutex.Lock() | ||
| defer m.mutex.Unlock() | ||
|
|
||
| delete(m.areaSettingsMap, area) | ||
| // The area stat is still managed by paths' lifecycle. | ||
| } | ||
|
|
||
| func (m *memControl[A, P, T, D, H]) setAreaSettings(area A, settings AreaSettings) { | ||
| m.mutex.Lock() | ||
| defer m.mutex.Unlock() | ||
|
|
@@ -315,21 +337,29 @@ func (m *memControl[A, P, T, D, H]) setAreaSettings(area A, settings AreaSetting | |
| } | ||
| } | ||
|
|
||
| func (m *memControl[A, P, T, D, H]) addPathToArea(path *pathInfo[A, P, T, D, H], settings AreaSettings, feedbackChan chan<- Feedback[A, P, D]) { | ||
| func (m *memControl[A, P, T, D, H]) addPathToArea(path *pathInfo[A, P, T, D, H], feedbackChan chan<- Feedback[A, P, D]) { | ||
| m.mutex.Lock() | ||
| defer m.mutex.Unlock() | ||
|
|
||
| area, ok := m.areaStatMap[path.area] | ||
| areaStat, ok := m.areaStatMap[path.area] | ||
| if !ok { | ||
| area = newAreaMemStat(path.area, m, settings, feedbackChan) | ||
| m.areaStatMap[path.area] = area | ||
| settings, ok := m.areaSettingsMap[path.area] | ||
| if !ok { | ||
| settings = AreaSettings{} | ||
| } | ||
| settings.fix() | ||
|
|
||
| areaStat = newAreaMemStat(path.area, m, settings, feedbackChan) | ||
| m.areaStatMap[path.area] = areaStat | ||
| } else if settings, ok := m.areaSettingsMap[path.area]; ok { | ||
| // Ensure the stat uses the latest settings from AddArea. | ||
| settings.fix() | ||
| areaStat.settings.Store(&settings) | ||
| } | ||
|
|
||
| path.areaMemStat = area | ||
| area.pathMap.Store(path.path, path) | ||
| area.pathCount.Add(1) | ||
| // Update the settings | ||
| area.settings.Store(&settings) | ||
| path.areaMemStat = areaStat | ||
| areaStat.pathMap.Store(path.path, path) | ||
| areaStat.pathCount.Add(1) | ||
| } | ||
|
Comment on lines
+340
to
363
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. This function has a critical bug related to storing pointers to stack variables, which can lead to memory corruption.
To fix this, you need to ensure you are storing pointers to heap-allocated |
||
|
|
||
| // This method is called after the path is removed. | ||
|
|
||
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.
The
AddAreacall is made for every subscription, but since the area is always0and the settings are the same, this is redundant. This will repeatedly overwrite the settings for area0.It would be cleaner to call
AddAreaonce when thesubscriptionClientis initialized, for example inNewSubscriptionClient, and remove this call from theSubscribemethod.