-
Notifications
You must be signed in to change notification settings - Fork 30
Upgrade to Rack 3 #33
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
Changes from all commits
8826905
c3f2194
19e175c
4618999
af9d8b3
f0ecb0e
2dfa52a
10cb346
38cbc9e
8969995
aa8526e
67f68e7
9fad25a
8ebe9a2
789cf9d
b807243
5ecb1e0
1caddc3
018e580
df4b777
9f6dafc
238476f
02c4bb0
555cdb8
791ead7
fdc3806
2ad11f4
899f68e
b6eb684
336c2d5
51aa400
b712196
3126844
8cbf185
bfbe733
af58c09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ def process!(config, sub_batch) | |
opts = {} | ||
opts[:request_items] = {config.table_name => sub_batch} | ||
begin | ||
response = config.dynamo_db_client.batch_write_item(opts) | ||
response = config.dynamo_db_client.batch_write_item(**opts) | ||
opts[:request_items] = response[:unprocessed_items] | ||
end until opts[:request_items].empty? | ||
end | ||
|
@@ -101,7 +101,7 @@ def table_opts(config) | |
# @api private | ||
def oldest_date(sec) | ||
hash = {} | ||
hash[:attribute_value_list] = [:n => "#{((Time.now - sec).to_f)}"] | ||
hash[:attribute_value_list] = [(Time.now - sec).to_f] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might not work, I think it depends on if the SDK is configured with "simple_attributes" - I believe it defaults to true in the SDK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current version of the gem already does not work without the simple_attributes plugin. I have aligned coding style.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth adding a check for invalid client configuration during initialization? (@mullermp - should we also update the metrics/user agent info?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's worth considering. It might be better to make it a separate issue because it is not caused by this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think assuming that it defaults to true is fine. |
||
hash[:comparison_operator] = 'LT' | ||
hash | ||
end | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,14 @@ def set_session_data(env, sid, session, options = {}) | |
return false if session.empty? | ||
packed_session = pack_data(session) | ||
handle_error(env) do | ||
save_opts = update_opts(env, sid, packed_session, options) | ||
@config.dynamo_db_client.update_item(save_opts) | ||
if env['dynamo_db.new_session'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're removing pessimistic locking, does base/null locking as classes even make sense? We can possibly refactor away anything related to locking. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I need that feature, but others might. I cannot make the decision, but I don't have much time to implement and test for the feature. Please rename files to a suitable names if we could remove the feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine to remove pessimistic locking. I am saying that |
||
save_options = save_new_opts(env, sid, packed_session) | ||
@config.dynamo_db_client.put_item(save_options) | ||
env.delete('dynamo_db.new_session') | ||
else | ||
save_options = save_exists_opts(env, sid, packed_session, options) | ||
@config.dynamo_db_client.update_item(save_options) | ||
end | ||
sid | ||
end | ||
end | ||
|
@@ -51,109 +57,82 @@ def handle_error(env = nil, &block) | |
|
||
# @return [Hash] Options for deleting session. | ||
def delete_opts(sid) | ||
table_opts(sid) | ||
end | ||
|
||
# @return [Hash] Options for updating item in Session table. | ||
def update_opts(env, sid, session, options = {}) | ||
if env['dynamo_db.new_session'] | ||
updt_options = save_new_opts(env, sid, session) | ||
else | ||
updt_options = save_exists_opts(env, sid, session, options) | ||
end | ||
updt_options | ||
{ | ||
table_name: @config.table_name, | ||
key: { | ||
@config.table_key => sid | ||
} | ||
} | ||
end | ||
|
||
# @return [Hash] Options for saving a new session in database. | ||
def save_new_opts(env, sid, session) | ||
attribute_opts = attr_updts(env, session, created_attr) | ||
merge_all(table_opts(sid), attribute_opts) | ||
{ | ||
table_name: @config.table_name, | ||
item: { | ||
@config.table_key => sid, | ||
data: session.to_s, | ||
created_at: created_at, | ||
updated_at: updated_at, | ||
expire_at: expire_at | ||
}, | ||
condition_expression: "attribute_not_exists(#{@config.table_key})" | ||
} | ||
end | ||
|
||
# @return [Hash] Options for saving an existing sesison in the database. | ||
def save_exists_opts(env, sid, session, options = {}) | ||
add_attr = options[:add_attrs] || {} | ||
expected = options[:expect_attr] || {} | ||
attribute_opts = merge_all(attr_updts(env, session, add_attr), expected) | ||
merge_all(table_opts(sid), attribute_opts) | ||
data = if data_unchanged?(env, session) | ||
{} | ||
else | ||
{ | ||
data: { | ||
value: session.to_s, | ||
action: 'PUT' | ||
} | ||
} | ||
end | ||
{ | ||
table_name: @config.table_name, | ||
key: { | ||
@config.table_key => sid | ||
}, | ||
attribute_updates: { | ||
updated_at: { | ||
value: updated_at, | ||
action: 'PUT' | ||
}, | ||
expire_at: { | ||
value: expire_at, | ||
action: 'PUT' | ||
} | ||
}.merge(data), | ||
return_values: 'UPDATED_NEW' | ||
} | ||
end | ||
|
||
# Unmarshal the data. | ||
def unpack_data(packed_data) | ||
Marshal.load(packed_data.unpack("m*").first) | ||
end | ||
|
||
# Table options for client. | ||
def table_opts(sid) | ||
{ | ||
:table_name => @config.table_name, | ||
:key => { @config.table_key => sid } | ||
} | ||
end | ||
|
||
# Attributes to update via client. | ||
def attr_updts(env, session, add_attrs = {}) | ||
data = data_unchanged?(env, session) ? {} : data_attr(session) | ||
{ | ||
attribute_updates: merge_all(updated_attr, data, add_attrs, expire_attr), | ||
return_values: 'UPDATED_NEW' | ||
} | ||
end | ||
|
||
# Update client with current time attribute. | ||
def updated_at | ||
{ :value => "#{(Time.now).to_f}", :action => "PUT" } | ||
Time.now.to_f | ||
end | ||
|
||
# Attribute for creation of session. | ||
def created_attr | ||
{ "created_at" => updated_at } | ||
def created_at | ||
updated_at | ||
end | ||
|
||
# Update client with current time + max_stale. | ||
def expire_at | ||
max_stale = @config.max_stale || 0 | ||
{ value: (Time.now + max_stale).to_i, action: 'PUT' } | ||
end | ||
|
||
# Attribute for TTL expiration of session. | ||
def expire_attr | ||
{ 'expire_at' => expire_at } | ||
end | ||
|
||
# Attribute for updating session. | ||
def updated_attr | ||
{ | ||
"updated_at" => updated_at | ||
} | ||
end | ||
|
||
def data_attr(session) | ||
{ "data" => {:value => session, :action => "PUT"} } | ||
(Time.now + max_stale).to_i | ||
end | ||
|
||
# Determine if data has been manipulated | ||
def data_unchanged?(env, session) | ||
return false unless env['rack.initial_data'] | ||
env['rack.initial_data'] == session | ||
end | ||
|
||
# Expected attributes | ||
def expected_attributes(sid) | ||
{ :expected => { @config.table_key => {:value => sid, :exists => true} } } | ||
end | ||
|
||
# Attributes to be retrieved via client | ||
def attr_opts | ||
{:attributes_to_get => ["data"], | ||
:consistent_read => @config.consistent_read} | ||
end | ||
|
||
# @return [Hash] merged hash of all hashes passed in. | ||
def merge_all(*hashes) | ||
new_hash = {} | ||
hashes.each{|hash| new_hash.merge!(hash)} | ||
new_hash | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the "older version" (may help users down the road to be more specific).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd appreciate it if someone else could revise the README and other texts because I'm not very good at English and I might make misunderstandings. Sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries~! I can do that for you when we're done.