Skip to content

Commit

Permalink
Merge pull request #484 from airbrake/483-nomethoderror-fix
Browse files Browse the repository at this point in the history
async_sender: return a rejected promise when capacity is maxed out
  • Loading branch information
kyrylo committed Jun 5, 2019
2 parents 05e0b70 + f550a9c commit 738162b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Airbrake Ruby Changelog

### master

* Fixed `AsyncSender` returning `nil` instead of a rejected promise when it
reaches its capacity
([#484](https://github.com/airbrake/airbrake-ruby/pull/484))

### [v4.4.0][v4.4.0] (May 9, 2019)

* Added `Airbrake::Query#stash`, `Airbrake::Request#stash`,
Expand Down
31 changes: 21 additions & 10 deletions lib/airbrake-ruby/async_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ module Airbrake
class AsyncSender
include Loggable

# @return [String]
WILL_NOT_DELIVER_MSG =
"%<log_label>s AsyncSender has reached its capacity of %<capacity>s " \
"and the following notice will not be delivered " \
"Error: %<type>s - %<message>s\nBacktrace: %<backtrace>s\n".freeze

# @return [ThreadGroup] the list of workers
# @note This is exposed for eaiser unit testing
# @since v4.0.0
Expand Down Expand Up @@ -36,7 +42,7 @@ def initialize
# library
# @return [Airbrake::Promise]
def send(notice, promise)
return will_not_deliver(notice) if @unsent.size >= @unsent.max
return will_not_deliver(notice, promise) if @unsent.size >= @unsent.max

@unsent << [notice, promise]
promise
Expand Down Expand Up @@ -115,17 +121,22 @@ def spawn_worker
end
end

def will_not_deliver(notice)
backtrace = notice[:errors][0][:backtrace].map do |line|
"#{line[:file]}:#{line[:line]} in `#{line[:function]}'"
end
def will_not_deliver(notice, promise)
error = notice[:errors].first

logger.error(
"#{LOG_LABEL} AsyncSender has reached its capacity of " \
"#{@unsent.max} and the following notice will not be delivered " \
"Error: #{notice[:errors][0][:type]} - #{notice[:errors][0][:message]}\n" \
"Backtrace: \n" + backtrace.join("\n")
format(
WILL_NOT_DELIVER_MSG,
log_label: LOG_LABEL,
capacity: @unsent.max,
type: error[:type],
message: error[:message],
backtrace: error[:backtrace].map do |line|
"#{line[:file]}:#{line[:line]} in `#{line[:function]}'"
end.join("\n")
)
)
nil
promise.reject("AsyncSender has reached its capacity of #{@unsent.max}")
end
end
end
8 changes: 8 additions & 0 deletions spec/async_sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
end
subject.close
end

it "returns a rejected promise" do
promise = Airbrake::Promise.new
200.times { subject.send(notice, promise) }
expect(promise.value).to eq(
'error' => "AsyncSender has reached its capacity of #{queue_size}"
)
end
end
end

Expand Down

0 comments on commit 738162b

Please sign in to comment.