Class: JetstreamBridge::ConsumerMiddleware::MiddlewareChain

Inherits:
Object
  • Object
show all
Defined in:
lib/jetstream_bridge/consumer/middleware.rb

Overview

Middleware chain for consumer message processing

Allows you to wrap message processing with cross-cutting concerns like logging, error handling, metrics, tracing, etc.

Examples:

Using middleware

consumer = JetstreamBridge::Consumer::Consumer.new(handler)
consumer.use(JetstreamBridge::Consumer::LoggingMiddleware.new)
consumer.use(JetstreamBridge::Consumer::MetricsMiddleware.new)
consumer.run!

Instance Method Summary collapse

Constructor Details

#initializeMiddlewareChain



20
21
22
# File 'lib/jetstream_bridge/consumer/middleware.rb', line 20

def initialize
  @middlewares = []
end

Instance Method Details

#call(event) { ... } ⇒ void

This method returns an undefined value.

Execute the middleware chain

Yields:

  • Final handler to call after all middleware



38
39
40
41
42
43
44
45
# File 'lib/jetstream_bridge/consumer/middleware.rb', line 38

def call(event, &final_handler)
  chain = @middlewares.reverse.reduce(final_handler) do |next_middleware, middleware|
    lambda do |evt|
      middleware.call(evt) { next_middleware.call(evt) }
    end
  end
  chain.call(event)
end

#use(middleware) ⇒ self

Add a middleware to the chain



28
29
30
31
# File 'lib/jetstream_bridge/consumer/middleware.rb', line 28

def use(middleware)
  @middlewares << middleware
  self
end