Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate original callstack, save a fiber allocation if unnecessary #503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/granite/connection_management.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ module Granite::ConnectionManagement
end

def self.schedule_adapter_switch
return if @@writer_adapter == @@reader_adapter
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not making a spawn call here saves ~8mb of memory allocation due to a fiber stack allocation, and saves me from my problem (and I'm hoping saves others from unnecessary memory allocation as well).


spawn do
sleep @@connection_switch_wait_period.milliseconds
switch_to_reader_adapter
Expand Down
10 changes: 5 additions & 5 deletions src/granite/transactions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module Granite::Transactions
end
{% end %}
rescue err
raise DB::Error.new(err.message)
raise DB::Error.new(err.message, cause: err)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were originally swallowing the underlying callstacks and only propagating the cannot allocate memory error message. Adding cause let me find where the root of the problem is (not the why, though, hrm).

end

# Runs an INSERT statement for all records in *model_array*, with options to
Expand All @@ -92,7 +92,7 @@ module Granite::Transactions
end
{% end %}
rescue err
raise DB::Error.new(err.message)
raise DB::Error.new(err.message, cause: err)
end

def import(model_array : Array(self) | Granite::Collection(self), ignore_on_duplicate : Bool, batch_size : Int32 = model_array.size)
Expand All @@ -114,7 +114,7 @@ module Granite::Transactions
end
{% end %}
rescue err
raise DB::Error.new(err.message)
raise DB::Error.new(err.message, cause: err)
end
end

Expand Down Expand Up @@ -176,7 +176,7 @@ module Granite::Transactions
rescue err : DB::Error
raise err
rescue err
raise DB::Error.new(err.message)
raise DB::Error.new(err.message, cause: err)
else
self.new_record = false
end
Expand All @@ -199,7 +199,7 @@ module Granite::Transactions
begin
self.class.adapter.update(self.class.table_name, self.class.primary_name, fields, params)
rescue err
raise DB::Error.new(err.message)
raise DB::Error.new(err.message, cause: err)
end
{% end %}
end
Expand Down