44 "context"
55 "log/slog"
66 "sync"
7+ "sync/atomic"
78 "time"
89
910 "github.com/google/uuid"
@@ -19,7 +20,7 @@ type CustomMemoryEventBus struct {
1920 topicMutex sync.RWMutex
2021 ctx context.Context
2122 cancel context.CancelFunc
22- isStarted bool
23+ isStarted atomic. Bool
2324 eventMetrics * EventMetrics
2425 eventFilters []EventFilter
2526}
@@ -189,7 +190,7 @@ func NewCustomMemoryEventBus(config map[string]interface{}) (EventBus, error) {
189190
190191// Start initializes the custom memory event bus
191192func (c * CustomMemoryEventBus ) Start (ctx context.Context ) error {
192- if c .isStarted {
193+ if c .isStarted . Load () {
193194 return nil
194195 }
195196
@@ -200,7 +201,7 @@ func (c *CustomMemoryEventBus) Start(ctx context.Context) error {
200201 go c .metricsCollector ()
201202 }
202203
203- c .isStarted = true
204+ c .isStarted . Store ( true )
204205 slog .Info ("Custom memory event bus started with enhanced features" ,
205206 "metricsEnabled" , c .config .EnableMetrics ,
206207 "filterCount" , len (c .eventFilters ))
@@ -209,7 +210,7 @@ func (c *CustomMemoryEventBus) Start(ctx context.Context) error {
209210
210211// Stop shuts down the custom memory event bus
211212func (c * CustomMemoryEventBus ) Stop (ctx context.Context ) error {
212- if ! c .isStarted {
213+ if ! c .isStarted . Load () {
213214 return nil
214215 }
215216
@@ -227,7 +228,7 @@ func (c *CustomMemoryEventBus) Stop(ctx context.Context) error {
227228 }
228229 c .topicMutex .Unlock ()
229230
230- c .isStarted = false
231+ c .isStarted . Store ( false )
231232 slog .Info ("Custom memory event bus stopped" ,
232233 "totalEvents" , c .eventMetrics .TotalEvents ,
233234 "topics" , len (c .eventMetrics .EventsPerTopic ))
@@ -236,7 +237,7 @@ func (c *CustomMemoryEventBus) Stop(ctx context.Context) error {
236237
237238// Publish sends an event to the specified topic with custom filtering and metrics
238239func (c * CustomMemoryEventBus ) Publish (ctx context.Context , event Event ) error {
239- if ! c .isStarted {
240+ if ! c .isStarted . Load () {
240241 return ErrEventBusNotStarted
241242 }
242243
@@ -309,7 +310,7 @@ func (c *CustomMemoryEventBus) SubscribeAsync(ctx context.Context, topic string,
309310
310311// subscribe is the internal implementation for both Subscribe and SubscribeAsync
311312func (c * CustomMemoryEventBus ) subscribe (ctx context.Context , topic string , handler EventHandler , isAsync bool ) (Subscription , error ) {
312- if ! c .isStarted {
313+ if ! c .isStarted . Load () {
313314 return nil , ErrEventBusNotStarted
314315 }
315316
@@ -347,7 +348,7 @@ func (c *CustomMemoryEventBus) subscribe(ctx context.Context, topic string, hand
347348
348349// Unsubscribe removes a subscription
349350func (c * CustomMemoryEventBus ) Unsubscribe (ctx context.Context , subscription Subscription ) error {
350- if ! c .isStarted {
351+ if ! c .isStarted . Load () {
351352 return ErrEventBusNotStarted
352353 }
353354
0 commit comments