Module: Cutoff::Patch::NetHttp

Defined in:
lib/cutoff/patch/net_http.rb

Overview

Set checkpoints for Ruby HTTP requests. Also sets the Net::HTTP timeouts to the remaining cutoff time. You can select this patch with exclude or only using the checkpoint name :net_http.

Instance Method Summary collapse

Instance Method Details

#begin_transportObject

Same as the original start, but adds a checkpoint for starting HTTP requests and sets network timeouts to the remaining time.

Also applies the same logic to begin_transport, which is called on every HTTP attempt including internal retries. Net::HTTP#transport_request silently retries idempotent requests (GET, HEAD, PUT, DELETE, OPTIONS, TRACE) up to max_retries (default 1) on transient errors like Net::ReadTimeout. Without re-applying the cutoff in begin_transport, the retry path goes through #connect (not #start), reuses the original read_timeout, and effectively doubles the deadline you set.

See Also:

  • Net::HTTP#start
  • Net::HTTP#begin_transport


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cutoff/patch/net_http.rb', line 42

module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
  def start
    _cutoff_apply_to_net_http
    super
  end

  def begin_transport(req)
    _cutoff_apply_to_net_http
    super
  end

  private

  def _cutoff_apply_to_net_http
    return unless (cutoff = Cutoff.current) && cutoff.selected?(:net_http)

    remaining = cutoff.seconds_remaining
    #{gen_timeout_method('open_timeout')}
    #{gen_timeout_method('read_timeout')}
    #{gen_timeout_method('write_timeout') if use_write_timeout?}
    #{gen_timeout_method('continue_timeout')}
    Cutoff.checkpoint!(:net_http)
  end
RUBY

#startObject

Same as the original start, but adds a checkpoint for starting HTTP requests and sets network timeouts to the remaining time.

Also applies the same logic to begin_transport, which is called on every HTTP attempt including internal retries. Net::HTTP#transport_request silently retries idempotent requests (GET, HEAD, PUT, DELETE, OPTIONS, TRACE) up to max_retries (default 1) on transient errors like Net::ReadTimeout. Without re-applying the cutoff in begin_transport, the retry path goes through #connect (not #start), reuses the original read_timeout, and effectively doubles the deadline you set.

See Also:

  • Net::HTTP#start
  • Net::HTTP#begin_transport


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cutoff/patch/net_http.rb', line 42

module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
  def start
    _cutoff_apply_to_net_http
    super
  end

  def begin_transport(req)
    _cutoff_apply_to_net_http
    super
  end

  private

  def _cutoff_apply_to_net_http
    return unless (cutoff = Cutoff.current) && cutoff.selected?(:net_http)

    remaining = cutoff.seconds_remaining
    #{gen_timeout_method('open_timeout')}
    #{gen_timeout_method('read_timeout')}
    #{gen_timeout_method('write_timeout') if use_write_timeout?}
    #{gen_timeout_method('continue_timeout')}
    Cutoff.checkpoint!(:net_http)
  end
RUBY