Class: AceMQ::AMQP::Patterns::InMemoryClaimCheckStore

Inherits:
Object
  • Object
show all
Defined in:
lib/acemq/amqp/patterns/claim_check.rb

Overview

A claim-check store in a hash.

Not for production, and the reason is the point of the pattern. The payloads are held in the publisher's own memory — which is where they were going to be anyway, so this takes them off the broker and does nothing else. A claim check that does not outlive the process that wrote it is a message nobody else can read, and every consumer in another process gets "the claim check is not in the store". It is lost on restart too, which turns every message still in a queue into one that can never be read.

It is genuinely useful in a test, where the publisher and the consumer are the same process and the thing being proved is the framing rather than the storage. Anything else wants a store the processes share.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ InMemoryClaimCheckStore

Returns a new instance of InMemoryClaimCheckStore.



85
86
87
88
# File 'lib/acemq/amqp/patterns/claim_check.rb', line 85

def initialize
  @contents = {}
  @lock = Mutex.new
end

Instance Method Details

#clear ⇒ Object

Empties the store, which is what a test between cases wants.



124
125
126
127
# File 'lib/acemq/amqp/patterns/claim_check.rb', line 124

def clear
  @lock.synchronize { @contents.clear }
  nil
end

#delete(key) ⇒ Object

Removes a payload.



115
116
117
118
# File 'lib/acemq/amqp/patterns/claim_check.rb', line 115

def delete(key)
  @lock.synchronize { @contents.delete(key.to_s) }
  nil
end

#get(key) ⇒ String?

Redeems a claim check.

Parameters:

  • key (String) —

    what the message carried

Returns:

  • (String, nil) —

    the payload, or nil when the store no longer holds it — which is retention having expired underneath a message that outlived it



110
111
112
# File 'lib/acemq/amqp/patterns/claim_check.rb', line 110

def get(key)
  @lock.synchronize { @contents[key.to_s] }&.dup
end

#put(content) ⇒ String

Stores a payload.

Parameters:

  • content (String) —

    the bytes

Returns:

  • (String) —

    the key the message will carry



94
95
96
97
98
99
100
101
102
# File 'lib/acemq/amqp/patterns/claim_check.rb', line 94

def put(content)
  key = SecureRandom.uuid
  # Copied, because the caller owns the string it handed over and a
  # codec is entitled to reuse a buffer. A store that keeps somebody
  # else's string is a store whose contents change after they were
  # stored. +b+ makes the copy and settles the encoding in one step.
  @lock.synchronize { @contents[key] = content.to_s.b }
  key
end

#size ⇒ Object

How many payloads are held.



121
# File 'lib/acemq/amqp/patterns/claim_check.rb', line 121

def size = @lock.synchronize { @contents.size }