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.



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/maimai_net/client.rb', line 492

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