Skip to content

Commit

Permalink
Support custom headers in freddy.deliver
Browse files Browse the repository at this point in the history
Headers are the main point for including message metadata in AMQP.
Headers are already in use to propagate traces.
Allow library users to add arbitrary headers themselves.
  • Loading branch information
urmastalimaa committed Sep 7, 2023
1 parent 6f854e9 commit 47bdf62
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ The message heading specify the `content-encoding` indicating the compression al
freddy.deliver(destination, message, compress: 'zlib')
```

#### Metadata through headers

Send a message with arbitrary message metadata included in the message headers
freddy.deliver(destination, message, headers: {'my-key' => 'my-value'})

### Request delivery

#### Expiring messages
Expand Down
3 changes: 3 additions & 0 deletions lib/freddy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def tap_into(pattern_or_patterns, options = {}, &callback)
# won't be discarded if timeout it set to 0 (default).
# @option options [String] :compress (nil)
# - 'zlib' - compresses the payload with zlib
# @option options [Hash] :headers (nil)
# Arbitrary headers to add as message metadata
# @return [void]
#
# @example
Expand All @@ -176,6 +178,7 @@ def deliver(destination, payload, options = {})
opts = {}
opts[:expiration] = (timeout * 1000).to_i if timeout.positive?
opts[:content_encoding] = compression_algorithm if compression_algorithm
opts[:headers] = options[:headers] if options[:headers]

@send_and_forget_producer.produce(destination, payload, opts)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/freddy/freddy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
let(:destination2) { random_destination }
let(:payload) { { pay: 'load' } }

before do
@bunny = Bunny.new(config)
@bunny.start()
end

after { @bunny.close }

after { freddy.close }

def respond_to(&block)
Expand Down Expand Up @@ -72,6 +79,23 @@ def respond_to(&block)
expect(@tapped_message).to eq(payload)
end
end

it 'accepts custom headers' do
queue = exclusive_subscribe do |_info, metadata, _payload|
@metadata = metadata
end
freddy.deliver(queue, payload, headers: {'foo' => 'bar'})

wait_for { @metadata }
expect(@metadata[:headers]).to include({'foo' => 'bar'})
end
end

def exclusive_subscribe(&block)
channel = @bunny.create_channel
queue = channel.queue('', exclusive: true)
queue.subscribe(&block)
queue.name
end

context 'when making a synchronized request' do
Expand Down

0 comments on commit 47bdf62

Please sign in to comment.