-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compress large payloads in all Action Cable subscription adapters wit…
…h ActiveSupport::Gzip
- Loading branch information
1 parent
68b20b6
commit 195e0a0
Showing
8 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
actioncable/lib/action_cable/subscription_adapter/compressor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/gzip" | ||
require "active_support/core_ext/numeric/bytes" | ||
require "active_support/core_ext/string/access" | ||
|
||
module ActionCable | ||
module SubscriptionAdapter | ||
class Compressor | ||
SCHEMA_VERSION = "v1" | ||
PREFIX_SIZE = "#{SCHEMA_VERSION}/./".size | ||
|
||
def initialize(threshold: 1.kilobyte, compressor: ActiveSupport::Gzip) | ||
@threshold = threshold | ||
@compressor = compressor | ||
end | ||
|
||
def compress(data) | ||
return "#{SCHEMA_VERSION}/0/#{data}" if data.size < @threshold | ||
|
||
"#{SCHEMA_VERSION}/1/#{@compressor.compress(data)}" | ||
end | ||
|
||
def decompress(data) | ||
return data unless valid_schema?(data) | ||
|
||
return data.from(PREFIX_SIZE) unless compressed?(data) | ||
|
||
@compressor.decompress(data.from(PREFIX_SIZE)) | ||
end | ||
|
||
def compressed?(data) | ||
data.start_with?("#{SCHEMA_VERSION}/1/") | ||
end | ||
|
||
private | ||
def valid_schema?(data) | ||
data.start_with?(/#{SCHEMA_VERSION}\/(0|1)\//) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "active_support/core_ext/numeric/bytes" | ||
require "active_support/core_ext/string/access" | ||
|
||
class ActionCable::SubscriptionAdapter::CompressorTest < ActionCable::TestCase | ||
SCHEMA_VERSION = ActionCable::SubscriptionAdapter::Compressor::SCHEMA_VERSION | ||
|
||
setup do | ||
@compressor = ActionCable::SubscriptionAdapter::Compressor.new | ||
@uncompressed_data = "hello" * 1000 | ||
@compressed_data = @compressor.compress(@uncompressed_data) | ||
end | ||
|
||
test "#compress returns uncompressed data if it's smaller than the threshold" do | ||
assert @compressor.compress("hello").start_with?("#{SCHEMA_VERSION}/0/") | ||
end | ||
|
||
test "#compress returns compressed data if it's larger than the threshold" do | ||
assert @compressor.compress(@uncompressed_data).start_with?("#{SCHEMA_VERSION}/1/") | ||
end | ||
|
||
test "#decompress returns uncompressed data if it's not compressed" do | ||
assert_equal "hello", @compressor.decompress("#{SCHEMA_VERSION}/0/hello") | ||
end | ||
|
||
test "#decompress returns uncompressed data if it's compressed" do | ||
assert_equal @uncompressed_data, @compressor.decompress(@compressed_data) | ||
end | ||
|
||
test "#decompress returns original data if schema is not valid" do | ||
assert_equal "hello", @compressor.decompress("hello") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters