Module: MaimaiNet::Client::ConnectionProtocol

Defined in:
lib/maimai_net/client.rb

Instance Method Summary collapse

Instance Method Details

#send_request(method, url, data, **opts) ⇒ Object

wraps connection method definition with auto-retry capability

Parameters:

  • opts (Hash)
  • retry_count (Hash)

    a customizable set of options

Raises:

  • (Error::RetryExhausted)

    upon attempting to exceed the allowed amount of attempts for retrying the request.



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/maimai_net/client.rb', line 469

def send_request(method, url, data, **opts)
  fail NotImplementedError, 'connection is not defined' if @conn.nil?

  # skip the wrapping if had been called recently
  # * does not take account on "hooked" calls
  prependers = self.class.ancestors
  prependers.slice! (prependers.index(self.class)..)

  stack = caller_locations(1).select do |trace| __method__.id2name == trace.label end.first(prependers.size)
  if stack.size > 0 then
    prev = stack[prependers.reverse.index(ConnectionProtocol)]
    return super if __method__.id2name == prev.label # do not wrap further if it's a super call
  end

  max_count = retry_count = Integer === opts[:retry_count] ? opts[:retry_count] : 3
  begin
    super
  rescue Error::RequestRetry
    retry_count -= 1
    retry unless retry_count.negative?
    fail Error::RetryExhausted, "attempt exceeds #{max_count} retries"
  end
end