From c77b16c48d3448c76b2b0e766b2a624cec43db24 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 27 Mar 2020 12:24:56 +1300 Subject: [PATCH] Update kinesis.rb Ensure all data access is protected by mutex, and that the mutex is not lazy initialized. --- lib/fog/aws/kinesis.rb | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/fog/aws/kinesis.rb b/lib/fog/aws/kinesis.rb index 805815fce..e0c2b01b8 100644 --- a/lib/fog/aws/kinesis.rb +++ b/lib/fog/aws/kinesis.rb @@ -125,23 +125,26 @@ module Fog end class Mock - def self.mutex - @mutex ||= Mutex.new - end - def mutex; self.class.mutex; end + @mutex = Mutex.new def self.data - @data ||= Hash.new do |hash, region| - hash[region] = Hash.new do |region_hash, key| - region_hash[key] = { - :kinesis_streams => {} - } + @mutex.synchronize do + @data ||= Hash.new do |hash, region| + hash[region] = Hash.new do |region_hash, key| + region_hash[key] = { + :kinesis_streams => {} + } + end end + + yield @data if block_given? end end def self.reset - @data = nil + @mutex.synchronize do + @data = nil + end end def initialize(options={}) @@ -153,31 +156,36 @@ module Fog end def data - self.class.data[@region][@aws_access_key_id] + self.class.data do |data| + data[@region][@aws_access_key_id] + end end def reset_data - self.class.data[@region].delete(@aws_access_key_id) + self.class.data do |data| + data[@region].delete(@aws_access_key_id) + end end def self.next_sequence_number - mutex.synchronize do + @mutex.synchronize do @sequence_number ||= -1 @sequence_number += 1 @sequence_number.to_s end end + def next_sequence_number; self.class.next_sequence_number; end def self.next_shard_id - mutex.synchronize do + @mutex.synchronize do @shard_id ||= -1 @shard_id += 1 "shardId-#{@shard_id.to_s.rjust(12, "0")}" end end + def next_shard_id; self.class.next_shard_id; end - end end