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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the size of usage of the session store to ~1KB #1

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/analytical/session_command_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ def initialize(session, module_key, initial_list=nil)
@session = session
@module_key = module_key
@session_key = ('analytical_'+module_key.to_s).to_sym
ensure_session_setup!(initial_list)
assign(initial_list || [])
end

def assign(v)
self.commands = v

Choose a reason for hiding this comment

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

wouldn't this call trim_commands! given we define commands=?

Copy link
Author

Choose a reason for hiding this comment

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

Good point :P

trim_commands!
end

def commands
trim_commands!
@session[@session_key]
end
def commands=(v)
@session[@session_key] = v
trim_commands!
end

def flush
Expand All @@ -30,13 +33,17 @@ def remove(processed_commands)

# Pass any array methods on to the internal array
def method_missing(method, *args, &block)
commands.send(method, *args, &block)
commands.send(method, *args, &block).tap { trim_commands! }
end

private

def ensure_session_setup!(initial_list=nil)
self.commands ||= (initial_list || [])
# Make sure to not exceed ~1KB of session storage use (max cookie is 4KB)
def trim_commands!
serialized_length = @session[@session_key].inspect.length
return unless serialized_length > 1000
@session[@session_key] = @session[@session_key][0..-2]
trim_commands!
end

end
Expand Down
22 changes: 18 additions & 4 deletions spec/analytical/session_command_store_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'spec_helper'

describe Analytical::SessionCommandStore do

describe 'with a session hash' do
before(:each) do
@session = {}
end

it 'should add elements' do
@store = Analytical::SessionCommandStore.new @session, :some_module, ['a']
@store << 'b'
Expand All @@ -30,7 +30,21 @@

it 'should set up the :analytical session hash' do
@store = Analytical::SessionCommandStore.new @session, :some_module, ['a', 'b']
@session[:analytical_some_module].should_not be_nil
@session[:analytical_some_module].should_not be_nil
end

it 'should discard older items when the store is full' do
large_item = [
"set",
{
"I have some key" => "and a long param",
"and more key" => "and some more value",
"so much key" => "such such value",
},
]
@store = Analytical::SessionCommandStore.new @session, :some_module, [large_item] * 100
@session[:analytical_some_module].should_not be_nil
@session[:analytical_some_module].inspect.length.should be < 1000
end

describe 'when flushing' do
Expand All @@ -55,5 +69,5 @@
end
end
end

end