Module: Coolhand::NetHttpInterceptor::ResponseInterceptor

Defined in:
lib/coolhand/net_http_interceptor.rb

Overview

Response streaming interceptor nested under NetHttpInterceptor

Instance Method Summary collapse

Instance Method Details

#read_body(dest = nil, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/coolhand/net_http_interceptor.rb', line 11

def read_body(dest = nil, &block)
  # Only buffer while a #request call below is actively capturing —
  # otherwise every block-form read_body in the process (including
  # ones on responses this gem never intercepted) accumulates into a
  # thread-local that's never freed, which is an unbounded memory
  # leak on any thread that streams large non-LLM responses.
  return super unless block && Thread.current[:coolhand_capturing_stream]

  super do |chunk|
    Thread.current[:coolhand_stream_buffer] ||= +""
    Thread.current[:coolhand_stream_buffer] << chunk
    yield(chunk)
  end
end