eventbus

package
v1.0.31 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2026 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultWorkerCount = 16   // Number of worker goroutines
	DefaultQueueSize   = 1024 // Size of event dispatch queue
)

Default worker pool settings

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

Bus is the central event distributor

func New

func New() *Bus

New creates a new event bus with default worker pool settings

func NewWithConfig added in v1.0.22

func NewWithConfig(workers, queueSize int) *Bus

NewWithConfig creates a new event bus with custom worker pool settings

func (*Bus) Close

func (b *Bus) Close()

Close stops the bus from accepting new events and shuts down workers

func (*Bus) Publish

func (b *Bus) Publish(e Event)

Publish sends an event to all matching subscribers Handlers are called asynchronously via the worker pool Non-blocking: if the queue is full, the event dispatch is dropped (backpressure)

func (*Bus) PublishSync

func (b *Bus) PublishSync(e Event)

PublishSync sends an event and waits for all handlers to complete Use sparingly - blocks the caller

func (*Bus) Stats

func (b *Bus) Stats() Stats

Stats returns current bus statistics

func (*Bus) Subscribe

func (b *Bus) Subscribe(eventType EventType, handler Handler) string

Subscribe registers a handler for a specific event type Returns a subscription ID that can be used to unsubscribe

func (*Bus) SubscribeAll

func (b *Bus) SubscribeAll(handler Handler) string

SubscribeAll registers a handler for ALL events Useful for logging, metrics, GUI updates

func (*Bus) SubscribeMultiple

func (b *Bus) SubscribeMultiple(eventTypes []EventType, handler Handler) []string

SubscribeMultiple registers a handler for multiple event types

func (*Bus) Unsubscribe

func (b *Bus) Unsubscribe(id string) bool

Unsubscribe removes a subscription by ID

type Event

type Event struct {
	ID        string         `json:"id"`        // Unique event ID
	Type      EventType      `json:"type"`      // Event type for routing
	Severity  Severity       `json:"severity"`  // Severity level
	Timestamp time.Time      `json:"timestamp"` // When event occurred
	Source    string         `json:"source"`    // Module that generated event
	IP        string         `json:"ip"`        // Related IP address (if any)
	User      string         `json:"user"`      // Related username (if any)
	Message   string         `json:"message"`   // Human-readable message
	Data      map[string]any `json:"data"`      // Additional structured data
}

Event is the universal event structure passed between modules

func NewEvent

func NewEvent(eventType EventType, source string) Event

NewEvent creates a new event with auto-generated ID and timestamp

func (Event) WithData

func (e Event) WithData(key string, value any) Event

WithData adds a key-value pair to Data

func (Event) WithIP

func (e Event) WithIP(ip string) Event

WithIP sets the IP field

func (Event) WithMessage

func (e Event) WithMessage(msg string) Event

WithMessage sets the Message field

func (Event) WithSeverity

func (e Event) WithSeverity(sev Severity) Event

WithSeverity sets the Severity field

func (Event) WithUser

func (e Event) WithUser(user string) Event

WithUser sets the User field

type EventType

type EventType string

EventType categorizes events for routing

const (
	// Ban/Unban events
	EventBan   EventType = "ban"
	EventUnban EventType = "unban"

	// Login events
	EventLoginFail    EventType = "login_fail"
	EventLoginFailed  EventType = "login_failed" // Alias for consistency
	EventLoginSuccess EventType = "login_success"
	EventLoginAlert   EventType = "login_alert"

	// Security detection events
	EventSuricataAlert EventType = "suricata_alert"
	EventDDoSDetected  EventType = "ddos_detected"
	EventPortscan      EventType = "portscan_detected"

	// Feed events
	EventFeedSync   EventType = "feed_sync"
	EventFeedUpdate EventType = "feed_update"

	// System events
	EventHealthCheck EventType = "health_check"
	EventModuleStart EventType = "module_start"
	EventModuleStop  EventType = "module_stop"
	EventError       EventType = "error"

	// GeoIP events
	EventGeoIPUpdate EventType = "geoip_update"
	EventCountryBan  EventType = "country_ban"
)

type Handler

type Handler func(Event)

Handler is a function that receives events

type Severity

type Severity int

Severity levels for events

const (
	SeverityDebug Severity = iota
	SeverityInfo
	SeverityWarning
	SeverityCritical
)

func (Severity) String

func (s Severity) String() string

String returns human-readable severity

type Stats

type Stats struct {
	Subscriptions int   `json:"subscriptions"`
	Workers       int   `json:"workers"`
	QueueSize     int   `json:"queue_size"`
	QueueUsed     int   `json:"queue_used"`
	Published     int64 `json:"published"`
	Delivered     int64 `json:"delivered"`
	Dropped       int64 `json:"dropped"` // Events dropped due to backpressure
	Errors        int64 `json:"errors"`
}

Stats returns bus statistics

type Subscription

type Subscription struct {
	// contains filtered or unexported fields
}

Subscription represents a registered handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL