60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / core / bus.go
3.9 KB
1package core 2 3import ( 4 "sync" 5 "sync/atomic" 6 "time" 7 8 "github.com/rs/zerolog/log" 9 "github.com/xonecas/zoea-nova/internal/constants" 10) 11 12const dropLogEvery = 100 13 14type subscriber struct { 15 mu sync.RWMutex 16 ch chan Event 17 dropped atomic.Uint64 18 closed bool 19} 20 21func (s *subscriber) send(event Event, timeout time.Duration) (bool, bool) { 22 s.mu.RLock() 23 defer s.mu.RUnlock() 24 25 if s.closed { 26 return false, false 27 } 28 29 if timeout <= 0 { 30 select { 31 case s.ch <- event: 32 return true, false 33 default: 34 return false, false 35 } 36 } 37 38 timer := time.NewTimer(timeout) 39 select { 40 case s.ch <- event: 41 if !timer.Stop() { 42 <-timer.C 43 } 44 return true, false 45 case <-timer.C: 46 return false, true 47 } 48} 49 50func (s *subscriber) close() { 51 s.mu.Lock() 52 if s.closed { 53 s.mu.Unlock() 54 return 55 } 56 s.closed = true 57 close(s.ch) 58 s.mu.Unlock() 59} 60 61// EventBus distributes events to subscribers. 62type EventBus struct { 63 mu sync.RWMutex 64 subscribers []*subscriber 65 bufferSize int 66} 67 68// NewEventBus creates a new event bus. 69func NewEventBus(bufferSize int) *EventBus { 70 if bufferSize < constants.MinEventBusBufferSize { 71 bufferSize = constants.MinEventBusBufferSize 72 } 73 return &EventBus{ 74 bufferSize: bufferSize, 75 } 76} 77 78// Subscribe returns a channel that receives events. 79// The caller is responsible for reading from the channel to avoid blocking. 80func (b *EventBus) Subscribe() <-chan Event { 81 b.mu.Lock() 82 defer b.mu.Unlock() 83 84 sub := &subscriber{ch: make(chan Event, b.bufferSize)} 85 b.subscribers = append(b.subscribers, sub) 86 return sub.ch 87} 88 89// Unsubscribe removes a subscriber channel. 90func (b *EventBus) Unsubscribe(ch <-chan Event) { 91 b.mu.Lock() 92 defer b.mu.Unlock() 93 94 for i, sub := range b.subscribers { 95 if sub.ch == ch { 96 b.subscribers = append(b.subscribers[:i], b.subscribers[i+1:]...) 97 sub.close() 98 return 99 } 100 } 101} 102 103// Publish sends an event to all subscribers. 104// Non-blocking: drops events if a subscriber's buffer is full. 105func (b *EventBus) Publish(event Event) { 106 b.mu.RLock() 107 subscribers := append([]*subscriber(nil), b.subscribers...) 108 b.mu.RUnlock() 109 110 for i, sub := range subscribers { 111 if delivered, _ := sub.send(event, 0); !delivered { 112 dropped := sub.dropped.Add(1) 113 if dropped%dropLogEvery == 0 { 114 log.Warn(). 115 Str("event_type", string(event.Type)). 116 Uint64("dropped", dropped). 117 Int("subscriber_index", i). 118 Msg("event bus subscriber dropped events") 119 } 120 } 121 } 122} 123 124// PublishBlocking sends an event to all subscribers, waiting up to timeout per subscriber. 125// Returns true if all subscribers received the event. 126func (b *EventBus) PublishBlocking(event Event, timeout time.Duration) bool { 127 if timeout <= 0 { 128 b.mu.RLock() 129 subscribers := append([]*subscriber(nil), b.subscribers...) 130 b.mu.RUnlock() 131 132 allDelivered := true 133 for i, sub := range subscribers { 134 if delivered, _ := sub.send(event, 0); !delivered { 135 allDelivered = false 136 dropped := sub.dropped.Add(1) 137 if dropped%dropLogEvery == 0 { 138 log.Warn(). 139 Str("event_type", string(event.Type)). 140 Uint64("dropped", dropped). 141 Int("subscriber_index", i). 142 Msg("event bus subscriber dropped events") 143 } 144 } 145 } 146 147 return allDelivered 148 } 149 150 b.mu.RLock() 151 subscribers := append([]*subscriber(nil), b.subscribers...) 152 b.mu.RUnlock() 153 154 allDelivered := true 155 for i, sub := range subscribers { 156 delivered, timedOut := sub.send(event, timeout) 157 if delivered { 158 continue 159 } 160 allDelivered = false 161 dropped := sub.dropped.Add(1) 162 if dropped%dropLogEvery == 0 { 163 msg := "event bus subscriber dropped events" 164 if timedOut { 165 msg = "event bus subscriber timed out" 166 } 167 log.Warn(). 168 Str("event_type", string(event.Type)). 169 Uint64("dropped", dropped). 170 Int("subscriber_index", i). 171 Msg(msg) 172 } 173 } 174 175 return allDelivered 176} 177 178// Close closes all subscriber channels. 179func (b *EventBus) Close() { 180 b.mu.Lock() 181 defer b.mu.Unlock() 182 183 for _, ch := range b.subscribers { 184 ch.close() 185 } 186 b.subscribers = nil 187}