Other: Fix user sync leaks/race conditions

This fixes various race conditions and leaks related to the user's sync
and API event stream. It was possible for a sync/stream to begin after a
user was already closed; this change prevents that by managing the
goroutines related to sync/stream within cancellable groups.
This commit is contained in:
James Houlahan
2022-10-24 12:54:01 +02:00
parent 6bbaf03f1f
commit 828385b049
14 changed files with 282 additions and 253 deletions

View File

@ -136,34 +136,58 @@ func (m *Map[Key, Val]) GetDeleteErr(key Key, fn func(Val) error) (bool, error)
return ok, err
}
func (m *Map[Key, Val]) Set(key Key, val Val) {
func (m *Map[Key, Val]) Set(key Key, val Val) bool {
m.lock.Lock()
defer m.lock.Unlock()
var had bool
if _, ok := m.data[key]; ok {
had = true
}
m.data[key] = val
m.order = append(m.order, key)
if idx := xslices.Index(m.order, key); idx >= 0 {
m.order[idx] = key
} else {
m.order = append(m.order, key)
}
if m.sort != nil {
slices.SortFunc(m.order, func(a, b Key) bool {
return m.sort(a, b, m.data)
})
}
return had
}
func (m *Map[Key, Val]) SetFrom(key Key, other Key) {
func (m *Map[Key, Val]) SetFrom(key Key, other Key) bool {
m.lock.Lock()
defer m.lock.Unlock()
var had bool
if _, ok := m.data[key]; ok {
had = true
}
m.data[key] = m.data[other]
m.order = append(m.order, key)
if idx := xslices.Index(m.order, key); idx >= 0 {
m.order[idx] = key
} else {
m.order = append(m.order, key)
}
if m.sort != nil {
slices.SortFunc(m.order, func(a, b Key) bool {
return m.sort(a, b, m.data)
})
}
return had
}
func (m *Map[Key, Val]) Iter(fn func(key Key, val Val)) {