Skip to content

auto_paginate does not gracefully handle being rate limited.  #40

Description

@BobbyMcWho

We were writing a script to pull error events from the api, to parse some associated information so that we could backfill some data that needed fixed. Upon using the ruby gem, all was going well until I realized that Bugsnag::Api.error_events does not gracefully handle being rate-limited, which ultimately came down to the paginate method.

Our solution was to override that method on Bugsnag::Api::Client, but it's something that I would expect the client library to handle, or at least call out in the docs.

module Bugsnag::Client::GracefulRateLimit
  def paginate(url, options = {}, &block)
    opts = parse_query_and_convenience_headers(options.dup)
    if configuration.auto_paginate || configuration.per_page
      opts[:query][:per_page] ||=  configuration.per_page || (configuration.auto_paginate ? 100 : nil)
    end

    data = request(:get, url, opts)

    if configuration.auto_paginate
      while @last_response.rels[:next]
        begin
          @last_response = @last_response.rels[:next].get
        rescue Bugsnag::Api::RateLimitExceeded => e
          sleep 60 and redo
        else
          if block_given?
            yield(data, @last_response)
          else
            data.concat(@last_response.data) if @last_response.data.is_a?(Array)
          end
        end
      end
    end

    data
  end
end

Bugsnag::Api::Client.prepend Bugsnag::Client::GracefulRateLimit

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogWe hope to fix this feature/bug in the futurefeature requestRequest for a new feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions