Documentation
¶
Index ¶
- Constants
- Variables
- func RenderConfig(ctx context.Context, path string) ([]byte, error)
- func RenderConfigTo(ctx context.Context, path string, w io.Writer) error
- func Run(ctx context.Context) error
- func RunCLI(ctx context.Context) error
- type App
- type CLI
- type CircuitBreaker
- type CircuitBreakerConfig
- type CommandResult
- type Config
- type Handler
- type HandlerConfig
- type Metrics
- type PublishCmd
- type QueueClient
- type QueueMessage
- type RMQRequestConfig
- type RMQResponseConfig
- type RabbitMQConfig
- type RabbitMQPublisher
- func (p *RabbitMQPublisher) Ack(_ context.Context, _ *QueueMessage) error
- func (p *RabbitMQPublisher) Close() error
- func (p *RabbitMQPublisher) Nack(_ context.Context, _ *QueueMessage) error
- func (p *RabbitMQPublisher) Publish(ctx context.Context, msg *mqbridge.Message) error
- func (p *RabbitMQPublisher) Receive(_ context.Context) (*QueueMessage, error)
- type RabbitMQReceiver
- func (r *RabbitMQReceiver) Ack(_ context.Context, qmsg *QueueMessage) error
- func (r *RabbitMQReceiver) Close() error
- func (r *RabbitMQReceiver) Nack(_ context.Context, qmsg *QueueMessage) error
- func (r *RabbitMQReceiver) Publish(_ context.Context, _ *mqbridge.Message) error
- func (r *RabbitMQReceiver) Receive(ctx context.Context) (*QueueMessage, error)
- type RenderCmd
- type ResponseIgnoreConfig
- type RoutingKeys
- type RunCmd
- type SMQRequestConfig
- type SMQResponseConfig
- type SimpleMQConfig
- type SimpleMQPublisher
- func (p *SimpleMQPublisher) Ack(_ context.Context, _ *QueueMessage) error
- func (p *SimpleMQPublisher) Close() error
- func (p *SimpleMQPublisher) Nack(_ context.Context, _ *QueueMessage) error
- func (p *SimpleMQPublisher) Publish(ctx context.Context, msg *mqbridge.Message) error
- func (p *SimpleMQPublisher) Receive(_ context.Context) (*QueueMessage, error)
- type SimpleMQReceiver
- func (r *SimpleMQReceiver) Ack(ctx context.Context, qmsg *QueueMessage) error
- func (r *SimpleMQReceiver) Close() error
- func (r *SimpleMQReceiver) Nack(_ context.Context, _ *QueueMessage) error
- func (r *SimpleMQReceiver) Publish(_ context.Context, _ *mqbridge.Message) error
- func (r *SimpleMQReceiver) Receive(ctx context.Context) (*QueueMessage, error)
- type ValidateCmd
Constants ¶
const ( // DefaultCircuitBreakerMaxEntries is the maximum number of message keys tracked // by a single circuit breaker instance (LRU eviction when exceeded). DefaultCircuitBreakerMaxEntries = 1024 // DefaultCircuitBreakerTTL is the default TTL for circuit breaker entries. DefaultCircuitBreakerTTL = 10 * time.Minute )
const ( // DefaultPollingInterval is the default interval for polling the request queue. DefaultPollingInterval = time.Second // DefaultCommandTimeout is the default timeout for command execution. DefaultCommandTimeout = 30 * time.Second // DefaultMaxConcurrency is the default max concurrency for non-blocking handlers. DefaultMaxConcurrency = 1 // DefaultMaxResponseChain is the default maximum number of allowed response chain hops. // A value of 0 means responses that arrive back as requests are dropped (no chaining). DefaultMaxResponseChain = 0 // DefaultQueueTimeout is the default timeout for queue operations (publish, ack, nack, receive). DefaultQueueTimeout = 30 * time.Second )
const ( BackendSimpleMQ = "simplemq" BackendRabbitMQ = "rabbitmq" )
Backend type constants.
const HeaderResponseChain = "mqsubscriber.responded"
HeaderResponseChain is the header key used to track response chain depth. When a response is published, this value is incremented. On receive, if the value reaches max_response_chain, the message is dropped to prevent loops.
Variables ¶
var ErrSlotFull = errors.New("handler at max_concurrency")
ErrSlotFull is returned by Acquire when the handler is at max_concurrency and reject_on_full is enabled. The caller must dispose of the message (ack or nack) instead of waiting.
var Version = "v0.7.7"
Functions ¶
func RenderConfig ¶
RenderConfig evaluates a Jsonnet config file and returns the resulting JSON.
func RenderConfigTo ¶
RenderConfigTo evaluates a config file and writes pretty-printed JSON to w.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App holds the application state.
type CLI ¶
type CLI struct {
EnvFile string `kong:"short='e',env='MQSUBSCRIBER_ENVFILE',help='Environment file to load'" `
Config string `kong:"required,short='c',env='MQSUBSCRIBER_CONFIG',help='Config file path (Jsonnet/JSON)'" `
LogFormat string `kong:"default='text',enum='text,json',env='MQSUBSCRIBER_LOG_FORMAT',help='Log format (text or json)'" `
LogLevel string `kong:"default='info',enum='debug,info,warn,error',env='MQSUBSCRIBER_LOG_LEVEL',help='Log level (debug, info, warn, error)'" `
Run RunCmd `cmd:"" default:"1" help:"Run the subscriber"`
Validate ValidateCmd `cmd:"" help:"Validate config"`
Render RenderCmd `cmd:"" help:"Render config as JSON to stdout"`
Publish PublishCmd `cmd:"" help:"Publish a message to the request or response queue"`
Version kong.VersionFlag `help:"Show version"`
}
CLI defines the command-line interface.
type CircuitBreaker ¶ added in v0.7.3
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker tracks per-message error counts and drops messages after a configured threshold is reached. Thread-safe via expirable.LRU (internally synchronized).
func NewCircuitBreaker ¶ added in v0.7.3
func NewCircuitBreaker(threshold int, ttl time.Duration) *CircuitBreaker
NewCircuitBreaker creates a CircuitBreaker with the given error threshold and TTL.
func (*CircuitBreaker) Clear ¶ added in v0.7.3
func (cb *CircuitBreaker) Clear(key string)
Clear removes the error count for the given key (called on success).
func (*CircuitBreaker) RecordError ¶ added in v0.7.3
func (cb *CircuitBreaker) RecordError(key string) bool
RecordError increments the error count for the given key. Returns true if the threshold has been reached (message should be dropped).
type CircuitBreakerConfig ¶ added in v0.7.3
CircuitBreakerConfig defines circuit breaker settings for a handler. When a message causes repeated errors, it is dropped (acked) after max_errors failures.
func (*CircuitBreakerConfig) GetTTL ¶ added in v0.7.3
func (c *CircuitBreakerConfig) GetTTL() time.Duration
GetTTL returns the TTL as a time.Duration.
type CommandResult ¶
type CommandResult struct {
Stdout []byte
Stderr []byte
ExitCode int
Err error // non-nil if command failed (non-zero exit or execution error)
Elapsed time.Duration
TimedOut bool // true if the command was terminated due to timeout
}
CommandResult holds the output of a command execution.
type Config ¶
type Config struct {
SimpleMQ *SimpleMQConfig
RabbitMQ *RabbitMQConfig
RequestQueue string
ResponseQueue string
SMQRequest *SMQRequestConfig
SMQResponse *SMQResponseConfig
RMQRequest *RMQRequestConfig
RMQResponse *RMQResponseConfig
Handlers []HandlerConfig
MaxResponseChain int
DropUnmatched bool
}
Config is the resolved top-level configuration. Backend-specific fields are in SMQ*/RMQ* structs (exactly one pair is non-nil).
func LoadConfig ¶
LoadConfig loads and parses a configuration file (Jsonnet or JSON).
func (*Config) BackendType ¶
BackendType returns the MQ backend type based on configuration.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler matches messages by headers and executes a command.
func NewHandler ¶
NewHandler creates a Handler from config.
func (*Handler) Acquire ¶
Acquire acquires a semaphore slot for non-blocking handlers. Blocks until a slot is free, unless reject_on_full is enabled — in which case it returns ErrSlotFull immediately when the semaphore is full.
func (*Handler) Execute ¶
Execute runs the command with the message body as stdin and returns the result.
type HandlerConfig ¶
type HandlerConfig struct {
Name string `json:"name"`
Match map[string]string `json:"match"`
MatchPattern bool `json:"match_pattern"`
Command []string `json:"command"`
Timeout string `json:"timeout"`
Blocking bool `json:"blocking"`
MaxConcurrency int `json:"max_concurrency"`
RejectOnFull bool `json:"reject_on_full"`
Response bool `json:"response"`
ResponseIgnore *ResponseIgnoreConfig `json:"response_ignore"`
CircuitBreaker *CircuitBreakerConfig `json:"circuit_breaker"`
Env map[string]string `json:"env"`
LogMessage string `json:"log_message"`
LogHeaderFields []string `json:"log_header_fields"`
LogBodyFields []string `json:"log_body_fields"`
}
HandlerConfig defines a handler that matches messages and executes a command.
func (*HandlerConfig) GetMaxConcurrency ¶
func (c *HandlerConfig) GetMaxConcurrency() int
GetMaxConcurrency returns the max concurrency for non-blocking handlers.
func (*HandlerConfig) GetTimeout ¶
func (c *HandlerConfig) GetTimeout() time.Duration
GetTimeout returns the command timeout as a time.Duration.
type Metrics ¶
type Metrics struct {
// contains filtered or unexported fields
}
Metrics holds OpenTelemetry metric instruments.
type PublishCmd ¶ added in v0.5.0
type PublishCmd struct {
Header map[string]string `short:"H" help:"Message header as key=value (repeatable)"`
Body string `help:"Message body string"`
BodyFile string `help:"Read message body from file" type:"existingfile"`
Request bool `help:"Publish to the request queue (default)" xor:"queue"`
Response bool `help:"Publish to the response queue" xor:"queue"`
}
PublishCmd is the "publish" subcommand.
type QueueClient ¶
type QueueClient interface {
// Receive returns a single message from the queue.
// Returns (nil, nil) when no messages are available.
Receive(ctx context.Context) (*QueueMessage, error)
// Publish sends a message to the response queue.
Publish(ctx context.Context, msg *mqbridge.Message) error
// Ack acknowledges a message (SimpleMQ: DELETE, RabbitMQ: Ack).
Ack(ctx context.Context, qmsg *QueueMessage) error
// Nack negatively acknowledges a message.
// SimpleMQ: no-op (visibility timeout handles redelivery).
// RabbitMQ: Nack without requeue (message routed to dead-letter exchange if configured).
Nack(ctx context.Context, qmsg *QueueMessage) error
// Close releases resources held by the client.
Close() error
}
QueueClient abstracts queue operations for different MQ backends.
type QueueMessage ¶
type QueueMessage struct {
ID string
Message *mqbridge.Message
// contains filtered or unexported fields
}
QueueMessage represents a message received from a queue.
type RMQRequestConfig ¶
type RMQRequestConfig struct {
Queue string `json:"queue"`
Exchange string `json:"exchange"`
ExchangeType string `json:"exchange_type"`
RoutingKey RoutingKeys `json:"routing_key"`
ExchangePassive bool `json:"exchange_passive"`
}
RMQRequestConfig defines the RabbitMQ request (inbound) queue.
type RMQResponseConfig ¶
type RMQResponseConfig struct {
Queue string `json:"queue"`
ReplyTo bool `json:"reply_to"`
Exchange string `json:"exchange"`
RoutingKey string `json:"routing_key"`
}
RMQResponseConfig defines the RabbitMQ response (outbound) queue.
type RabbitMQConfig ¶
RabbitMQConfig holds the global RabbitMQ settings.
func (*RabbitMQConfig) GetTimeout ¶ added in v0.5.0
func (c *RabbitMQConfig) GetTimeout() time.Duration
GetTimeout returns the timeout as a time.Duration.
type RabbitMQPublisher ¶
type RabbitMQPublisher struct {
// contains filtered or unexported fields
}
RabbitMQPublisher implements QueueClient for publishing to RabbitMQ.
func NewRabbitMQPublisher ¶
func NewRabbitMQPublisher(cfg *Config) *RabbitMQPublisher
NewRabbitMQPublisher creates a new RabbitMQPublisher.
func (*RabbitMQPublisher) Ack ¶
func (p *RabbitMQPublisher) Ack(_ context.Context, _ *QueueMessage) error
Ack is not supported on RabbitMQPublisher.
func (*RabbitMQPublisher) Close ¶
func (p *RabbitMQPublisher) Close() error
Close closes the RabbitMQ publisher connection.
func (*RabbitMQPublisher) Nack ¶
func (p *RabbitMQPublisher) Nack(_ context.Context, _ *QueueMessage) error
Nack is not supported on RabbitMQPublisher.
func (*RabbitMQPublisher) Publish ¶
Publish sends a message to RabbitMQ. Uses rabbitmq.exchange and rabbitmq.routing_key from message headers, or falls back to config-level exchange/routing_key if set.
func (*RabbitMQPublisher) Receive ¶
func (p *RabbitMQPublisher) Receive(_ context.Context) (*QueueMessage, error)
Receive is not supported on RabbitMQPublisher.
type RabbitMQReceiver ¶
type RabbitMQReceiver struct {
// contains filtered or unexported fields
}
RabbitMQReceiver implements QueueClient for receiving from RabbitMQ.
func NewRabbitMQReceiver ¶
func NewRabbitMQReceiver(cfg *Config, prefetch int) *RabbitMQReceiver
NewRabbitMQReceiver creates a new RabbitMQReceiver.
func (*RabbitMQReceiver) Ack ¶
func (r *RabbitMQReceiver) Ack(_ context.Context, qmsg *QueueMessage) error
Ack acknowledges the message.
func (*RabbitMQReceiver) Close ¶
func (r *RabbitMQReceiver) Close() error
Close closes the RabbitMQ connection.
func (*RabbitMQReceiver) Nack ¶
func (r *RabbitMQReceiver) Nack(_ context.Context, qmsg *QueueMessage) error
Nack negatively acknowledges the message without requeue. The message is routed to the dead-letter exchange if configured.
func (*RabbitMQReceiver) Publish ¶
Publish is not supported on RabbitMQReceiver. Use RabbitMQPublisher instead.
func (*RabbitMQReceiver) Receive ¶
func (r *RabbitMQReceiver) Receive(ctx context.Context) (*QueueMessage, error)
Receive returns a single message from the RabbitMQ queue. Blocks until a message is available or context is cancelled.
type ResponseIgnoreConfig ¶
type ResponseIgnoreConfig struct {
ExitCode *int `json:"exit_code"`
}
ResponseIgnoreConfig defines conditions under which a response is suppressed.
type RoutingKeys ¶ added in v0.3.1
type RoutingKeys []string
RoutingKeys is a []string that accepts both a single string and an array of strings in JSON.
func (*RoutingKeys) UnmarshalJSON ¶ added in v0.3.1
func (r *RoutingKeys) UnmarshalJSON(data []byte) error
UnmarshalJSON implements custom unmarshalling to accept both a string and an array of strings.
type SMQRequestConfig ¶
type SMQRequestConfig struct {
Queue string `json:"queue"`
APIKey string `json:"api_key"`
APIURL string `json:"api_url"`
PollingInterval string `json:"polling_interval"`
}
SMQRequestConfig defines the SimpleMQ request (inbound) queue.
func (*SMQRequestConfig) GetPollingInterval ¶
func (c *SMQRequestConfig) GetPollingInterval() time.Duration
GetPollingInterval returns the polling interval as a time.Duration.
type SMQResponseConfig ¶
type SMQResponseConfig struct {
Queue string `json:"queue"`
APIKey string `json:"api_key"`
APIURL string `json:"api_url"`
}
SMQResponseConfig defines the SimpleMQ response (outbound) queue.
type SimpleMQConfig ¶
SimpleMQConfig holds the global SimpleMQ settings.
func (*SimpleMQConfig) GetTimeout ¶ added in v0.5.0
func (c *SimpleMQConfig) GetTimeout() time.Duration
GetTimeout returns the timeout as a time.Duration.
type SimpleMQPublisher ¶
type SimpleMQPublisher struct {
// contains filtered or unexported fields
}
SimpleMQPublisher implements QueueClient for publishing to SimpleMQ.
func NewSimpleMQPublisher ¶
func NewSimpleMQPublisher(apiURL, apiKey, queue string, timeout time.Duration) (*SimpleMQPublisher, error)
NewSimpleMQPublisher creates a new SimpleMQPublisher.
func (*SimpleMQPublisher) Ack ¶
func (p *SimpleMQPublisher) Ack(_ context.Context, _ *QueueMessage) error
Ack is not supported on SimpleMQPublisher.
func (*SimpleMQPublisher) Close ¶
func (p *SimpleMQPublisher) Close() error
Close is a no-op for SimpleMQ (HTTP client, no persistent connection).
func (*SimpleMQPublisher) Nack ¶
func (p *SimpleMQPublisher) Nack(_ context.Context, _ *QueueMessage) error
Nack is not supported on SimpleMQPublisher.
func (*SimpleMQPublisher) Receive ¶
func (p *SimpleMQPublisher) Receive(_ context.Context) (*QueueMessage, error)
Receive is not supported on SimpleMQPublisher.
type SimpleMQReceiver ¶
type SimpleMQReceiver struct {
// contains filtered or unexported fields
}
SimpleMQReceiver implements QueueClient for receiving from SimpleMQ.
func NewSimpleMQReceiver ¶
func NewSimpleMQReceiver(apiURL, apiKey, queue string, timeout time.Duration) (*SimpleMQReceiver, error)
NewSimpleMQReceiver creates a new SimpleMQReceiver.
func (*SimpleMQReceiver) Ack ¶
func (r *SimpleMQReceiver) Ack(ctx context.Context, qmsg *QueueMessage) error
Ack deletes the message from the SimpleMQ queue.
func (*SimpleMQReceiver) Close ¶
func (r *SimpleMQReceiver) Close() error
Close is a no-op for SimpleMQ (HTTP client, no persistent connection).
func (*SimpleMQReceiver) Nack ¶
func (r *SimpleMQReceiver) Nack(_ context.Context, _ *QueueMessage) error
Nack is a no-op for SimpleMQ (messages are redelivered after visibility timeout).
func (*SimpleMQReceiver) Publish ¶
Publish is not supported on SimpleMQReceiver. Use SimpleMQPublisher instead.
func (*SimpleMQReceiver) Receive ¶
func (r *SimpleMQReceiver) Receive(ctx context.Context) (*QueueMessage, error)
Receive returns a single message from the queue. Internally buffers multiple messages from a single poll and returns them one at a time.