package transport import ( "context" "time" ) // MessageType represents the type of message being sent type MessageType string const ( // Core message types MessageTypeEvent MessageType = "event" MessageTypeCommand MessageType = "command" MessageTypeResponse MessageType = "response" MessageTypeHeartbeat MessageType = "heartbeat" MessageTypeStatus MessageType = "status" MessageTypeError MessageType = "error" // Business-specific message types MessageTypeArbitrage MessageType = "arbitrage" MessageTypeMarketData MessageType = "market_data" MessageTypeExecution MessageType = "execution" MessageTypeRiskCheck MessageType = "risk_check" ) // Priority levels for message routing type Priority uint8 const ( PriorityLow Priority = iota PriorityNormal PriorityHigh PriorityCritical PriorityEmergency ) // Message represents a universal message in the system type Message struct { ID string `json:"id"` Type MessageType `json:"type"` Topic string `json:"topic"` Source string `json:"source"` Destination string `json:"destination"` Priority Priority `json:"priority"` Timestamp time.Time `json:"timestamp"` TTL time.Duration `json:"ttl"` Headers map[string]string `json:"headers"` Payload []byte `json:"payload"` Metadata map[string]interface{} `json:"metadata"` } // MessageHandler processes incoming messages type MessageHandler func(ctx context.Context, msg *Message) error // Transport defines the interface for different transport mechanisms type Transport interface { // Start initializes the transport Start(ctx context.Context) error // Stop gracefully shuts down the transport Stop(ctx context.Context) error // Send publishes a message Send(ctx context.Context, msg *Message) error // Subscribe registers a handler for messages on a topic Subscribe(ctx context.Context, topic string, handler MessageHandler) error // Unsubscribe removes a handler for a topic Unsubscribe(ctx context.Context, topic string) error // GetStats returns transport statistics GetStats() TransportStats // GetType returns the transport type GetType() TransportType // IsHealthy checks if the transport is functioning properly IsHealthy() bool } // TransportType identifies different transport implementations type TransportType string const ( TransportTypeSharedMemory TransportType = "shared_memory" TransportTypeUnixSocket TransportType = "unix_socket" TransportTypeTCP TransportType = "tcp" TransportTypeWebSocket TransportType = "websocket" TransportTypeGRPC TransportType = "grpc" ) // TransportStats provides metrics about transport performance type TransportStats struct { MessagesSent uint64 `json:"messages_sent"` MessagesReceived uint64 `json:"messages_received"` MessagesDropped uint64 `json:"messages_dropped"` BytesSent uint64 `json:"bytes_sent"` BytesReceived uint64 `json:"bytes_received"` Latency time.Duration `json:"latency"` ErrorCount uint64 `json:"error_count"` ConnectedPeers int `json:"connected_peers"` Uptime time.Duration `json:"uptime"` } // MessageBus coordinates message routing across multiple transports type MessageBus interface { // Start initializes the message bus Start(ctx context.Context) error // Stop gracefully shuts down the message bus Stop(ctx context.Context) error // RegisterTransport adds a transport to the bus RegisterTransport(transport Transport) error // UnregisterTransport removes a transport from the bus UnregisterTransport(transportType TransportType) error // Publish sends a message through the optimal transport Publish(ctx context.Context, msg *Message) error // Subscribe registers a handler for messages on a topic Subscribe(ctx context.Context, topic string, handler MessageHandler) error // Unsubscribe removes a handler for a topic Unsubscribe(ctx context.Context, topic string) error // GetTransport returns a specific transport GetTransport(transportType TransportType) (Transport, error) // GetStats returns aggregated statistics GetStats() MessageBusStats } // MessageBusStats provides comprehensive metrics type MessageBusStats struct { TotalMessages uint64 `json:"total_messages"` MessagesByType map[MessageType]uint64 `json:"messages_by_type"` TransportStats map[TransportType]TransportStats `json:"transport_stats"` ActiveTopics []string `json:"active_topics"` Subscribers int `json:"subscribers"` AverageLatency time.Duration `json:"average_latency"` ThroughputMPS float64 `json:"throughput_mps"` // Messages per second } // Router determines the best transport for a message type Router interface { // Route selects the optimal transport for a message Route(msg *Message) (TransportType, error) // AddRule adds a routing rule AddRule(rule RoutingRule) error // RemoveRule removes a routing rule RemoveRule(ruleID string) error // GetRules returns all routing rules GetRules() []RoutingRule } // RoutingRule defines how messages should be routed type RoutingRule struct { ID string `json:"id"` Priority int `json:"priority"` Condition Condition `json:"condition"` Transport TransportType `json:"transport"` Fallback TransportType `json:"fallback,omitempty"` Description string `json:"description"` } // Condition defines when a routing rule applies type Condition struct { MessageType *MessageType `json:"message_type,omitempty"` Topic *string `json:"topic,omitempty"` Priority *Priority `json:"priority,omitempty"` Source *string `json:"source,omitempty"` Destination *string `json:"destination,omitempty"` PayloadSize *int `json:"payload_size,omitempty"` LatencyReq *time.Duration `json:"latency_requirement,omitempty"` } // DeadLetterQueue handles failed messages type DeadLetterQueue interface { // Add puts a failed message in the queue Add(ctx context.Context, msg *Message, reason error) error // Retry attempts to resend failed messages Retry(ctx context.Context, maxRetries int) error // Get retrieves failed messages Get(ctx context.Context, limit int) ([]*FailedMessage, error) // Remove deletes a failed message Remove(ctx context.Context, messageID string) error // GetStats returns dead letter queue statistics GetStats() DLQStats } // FailedMessage represents a message that couldn't be delivered type FailedMessage struct { Message *Message `json:"message"` Reason string `json:"reason"` Attempts int `json:"attempts"` FirstFailed time.Time `json:"first_failed"` LastAttempt time.Time `json:"last_attempt"` } // DLQStats provides dead letter queue metrics type DLQStats struct { TotalMessages uint64 `json:"total_messages"` RetryableMessages uint64 `json:"retryable_messages"` PermanentFailures uint64 `json:"permanent_failures"` OldestMessage time.Time `json:"oldest_message"` AverageRetries float64 `json:"average_retries"` } // Serializer handles message encoding/decoding type Serializer interface { // Serialize converts a message to bytes Serialize(msg *Message) ([]byte, error) // Deserialize converts bytes to a message Deserialize(data []byte) (*Message, error) // GetFormat returns the serialization format GetFormat() SerializationFormat } // SerializationFormat defines encoding types type SerializationFormat string const ( FormatJSON SerializationFormat = "json" FormatProtobuf SerializationFormat = "protobuf" FormatMsgPack SerializationFormat = "msgpack" FormatAvro SerializationFormat = "avro" ) // Persistence handles message storage type Persistence interface { // Store saves a message for persistence Store(ctx context.Context, msg *Message) error // Retrieve gets a stored message Retrieve(ctx context.Context, messageID string) (*Message, error) // Delete removes a stored message Delete(ctx context.Context, messageID string) error // List returns stored messages matching criteria List(ctx context.Context, criteria PersistenceCriteria) ([]*Message, error) // GetStats returns persistence statistics GetStats() PersistenceStats } // PersistenceCriteria defines search parameters type PersistenceCriteria struct { Topic *string `json:"topic,omitempty"` MessageType *MessageType `json:"message_type,omitempty"` Source *string `json:"source,omitempty"` FromTime *time.Time `json:"from_time,omitempty"` ToTime *time.Time `json:"to_time,omitempty"` Limit int `json:"limit"` Offset int `json:"offset"` } // PersistenceStats provides storage metrics type PersistenceStats struct { StoredMessages uint64 `json:"stored_messages"` StorageSize uint64 `json:"storage_size_bytes"` OldestMessage time.Time `json:"oldest_message"` NewestMessage time.Time `json:"newest_message"` }