forked from wrenn/wrenn
- C1: Add sync.RWMutex to vm.Manager to protect concurrent vms map access - H1: Fix IP arithmetic overflow in network slot addressing (byte truncation) - H5: Fix MultiplexedChannel.Fork() TOCTOU race (move exited check inside lock) - H8: Remove snapshot overwrite — return template_name_taken conflict instead - H9: Wrap DeleteAccount DB ops in a transaction, make team deletion fatal - H10: Sanitize serviceErrToHTTP to stop leaking internal error messages - H11: Add deleted_at IS NULL to GetUserByEmail/GetUserByID queries - H12: Add id DESC to audit log composite index for cursor pagination - H15: Delete dead AuthModal.svelte component - H17: Move JWT from WebSocket URL query param to first WS message - H18: Fix $derived to $derived.by in FilesTab breadcrumbs
80 lines
1.2 KiB
Go
80 lines
1.2 KiB
Go
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package handler
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
type MultiplexedChannel[T any] struct {
|
|
Source chan T
|
|
channels []chan T
|
|
mu sync.RWMutex
|
|
exited atomic.Bool
|
|
}
|
|
|
|
func NewMultiplexedChannel[T any](buffer int) *MultiplexedChannel[T] {
|
|
c := &MultiplexedChannel[T]{
|
|
channels: nil,
|
|
Source: make(chan T, buffer),
|
|
}
|
|
|
|
go func() {
|
|
for v := range c.Source {
|
|
c.mu.RLock()
|
|
|
|
for _, cons := range c.channels {
|
|
select {
|
|
case cons <- v:
|
|
default:
|
|
// Consumer not reading — skip to prevent deadlock
|
|
}
|
|
}
|
|
|
|
c.mu.RUnlock()
|
|
}
|
|
|
|
c.mu.Lock()
|
|
c.exited.Store(true)
|
|
for _, cons := range c.channels {
|
|
close(cons)
|
|
}
|
|
c.mu.Unlock()
|
|
}()
|
|
|
|
return c
|
|
}
|
|
|
|
func (m *MultiplexedChannel[T]) Fork() (chan T, func()) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
if m.exited.Load() {
|
|
ch := make(chan T)
|
|
close(ch)
|
|
return ch, func() {}
|
|
}
|
|
|
|
consumer := make(chan T, 4096)
|
|
|
|
m.channels = append(m.channels, consumer)
|
|
|
|
return consumer, func() {
|
|
m.remove(consumer)
|
|
}
|
|
}
|
|
|
|
func (m *MultiplexedChannel[T]) remove(consumer chan T) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
for i, ch := range m.channels {
|
|
if ch == consumer {
|
|
m.channels = append(m.channels[:i], m.channels[i+1:]...)
|
|
|
|
return
|
|
}
|
|
}
|
|
}
|