Skip to content

Commit d489306

Browse files
ianksjonathanhefner
andcommitted
Peek message before attempting to deserialize ActiveSupport::Message::Metadata using JSON
Rescuing an exception here can be wildly expensive here. We noticed 22ms overhead on each request when attempting to decode metadata that was serialized using Marshal. Co-authored-by: Jonathan Hefner <[email protected]>
1 parent ebbf66d commit d489306

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

activesupport/lib/active_support/messages/metadata.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ def pick_expiry(expires_at, expires_in)
3737
end
3838

3939
def extract_metadata(message)
40-
data = JSON.decode(message) rescue nil
40+
begin
41+
data = JSON.decode(message) if message.start_with?("{")
42+
rescue ::JSON::JSONError
43+
end
4144

4245
if data.is_a?(Hash) && data.key?("_rails")
4346
new(decode(data["_rails"]["message"]), data["_rails"]["exp"], data["_rails"]["pur"])

0 commit comments

Comments
 (0)