Sha256: 2432cfe2e89626052237e18c01184ebdcf80853246f284af322f88362cde4b24

Contents?: true

Size: 881 Bytes

Versions: 6

Compression:

Stored size: 881 Bytes

Contents

module MusicBrainz
  module ClientModules
    module FailsafeProxy
      def get_contents(url)
        return super unless failsafe?

        response = { body: nil, status: 500 }
        MusicBrainz.config.tries_limit.times do
          wait_util_ready!
          response = super
          break if response[:status] == 200
        end

        response
      end

      def time_passed
        Time.now.to_f - @last_query_time ||= 0.0
      end

      def time_to_wait
        MusicBrainz.config.query_interval - time_passed
      end

      def ready?
        time_passed > MusicBrainz.config.query_interval
      end

      def wait_util_ready!
        sleep(time_to_wait) unless ready?
        @last_query_time = Time.now.to_f
      end

      def failsafe?
        MusicBrainz.config.tries_limit > 1 && MusicBrainz.config.query_interval.to_f > 0
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
musicbrainz-0.8.0 lib/musicbrainz/client_modules/failsafe_proxy.rb
musicbrainz-0.7.7 lib/musicbrainz/client_modules/failsafe_proxy.rb
musicbrainz-0.7.6 lib/musicbrainz/client_modules/failsafe_proxy.rb
musicbrainz-0.7.5 lib/musicbrainz/client_modules/failsafe_proxy.rb
musicbrainz-0.7.4 lib/musicbrainz/client_modules/failsafe_proxy.rb
musicbrainz-0.7.3 lib/musicbrainz/client_modules/failsafe_proxy.rb