Class: Freddy::Producers::SendAndWaitResponseProducer

Inherits:
Object
  • Object
show all
Defined in:
lib/freddy/producers/send_and_wait_response_producer.rb

Constant Summary collapse

CONTENT_TYPE =
'application/json'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(channel, logger) ⇒ SendAndWaitResponseProducer

Returns a new instance of SendAndWaitResponseProducer.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/freddy/producers/send_and_wait_response_producer.rb', line 6

def initialize(channel, logger)
  @logger = logger
  @channel = channel

  @request_manager = RequestManager.new(@logger)

  @exchange = @channel.default_exchange
  @topic_exchange = @channel.topic Freddy::FREDDY_TOPIC_EXCHANGE_NAME

  @channel.on_no_route do |correlation_id|
    @request_manager.no_route(correlation_id)
  end

  @response_queue = @channel.queue("", exclusive: true)

  @response_consumer = Consumers::ResponseConsumer.new(@logger)
  @response_consumer.consume(@channel, @response_queue, &method(:handle_response))
end

Instance Method Details

#handle_response(delivery) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/freddy/producers/send_and_wait_response_producer.rb', line 74

def handle_response(delivery)
  correlation_id = delivery.correlation_id

  if request = @request_manager.delete(correlation_id)
    process_response(request, delivery)
  else
    message = "Got rpc response for correlation_id #{correlation_id} "\
              "but there is no requester"
    @logger.warn message
  end
end

#on_timeout(correlation_id, destination, timeout_in_seconds, trace) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/freddy/producers/send_and_wait_response_producer.rb', line 94

def on_timeout(correlation_id, destination, timeout_in_seconds, trace)
  Proc.new do
    @logger.warn "Request timed out waiting response from #{destination}"\
                 ", correlation id #{correlation_id}"

    @request_manager.delete(correlation_id)
    trace.finish
  end
end

#process_response(request, delivery) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/freddy/producers/send_and_wait_response_producer.rb', line 86

def process_response(request, delivery)
  @logger.debug "Got response for request to #{request[:destination]} "\
                "with correlation_id #{delivery.correlation_id}"
  request[:callback].call(delivery.payload, delivery)
ensure
  request[:trace].finish
end

#produce(destination, payload, timeout_in_seconds:, delete_on_timeout:, **properties) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/freddy/producers/send_and_wait_response_producer.rb', line 25

def produce(destination, payload, timeout_in_seconds:, delete_on_timeout:, **properties)
  span = OpenTracing.start_span("freddy:request:#{destination}",
    child_of: Freddy.trace,
    tags: {
      'component': 'freddy',
      'span.kind': 'client', # RPC
      'payload.type': payload[:type]
    }
  )

  correlation_id = SecureRandom.uuid

  container = SyncResponseContainer.new(
    on_timeout(correlation_id, destination, timeout_in_seconds, span)
  )

  @request_manager.store(correlation_id,
    callback: container,
    trace: span,
    destination: destination
  )

  if delete_on_timeout
    properties[:expiration] = (timeout_in_seconds * 1000).to_i
  end

  properties = properties.merge(
    routing_key: destination, content_type: CONTENT_TYPE,
    correlation_id: correlation_id, reply_to: @response_queue.name,
    mandatory: true, type: 'request'
  )
  OpenTracing.global_tracer.inject(span.context, OpenTracing::FORMAT_TEXT_MAP, TraceCarrier.new(properties))
  json_payload = Payload.dump(payload)

  span.log(
    event: 'Publishing request',
    payload: payload,
    response_queue: @response_queue.name,
    correlation_id: correlation_id
  )

  # Connection adapters handle thread safety for #publish themselves. No
  # need to lock these.
  @topic_exchange.publish json_payload, properties.dup
  @exchange.publish json_payload, properties.dup

  container.wait_for_response(timeout_in_seconds)
end