Module: RaceBlock
- Defined in:
- lib/race_block.rb,
lib/race_block/version.rb
Overview
Block for preventing race conditions across multiple threads and instances
Defined Under Namespace
Classes: Configuration, Error
Constant Summary
collapse
- VERSION =
"0.3.0"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
28
29
30
|
# File 'lib/race_block.rb', line 28
def configuration
@configuration
end
|
Class Method Details
.client ⇒ Object
37
38
39
|
# File 'lib/race_block.rb', line 37
def self.client
config.redis
end
|
.config {|configuration| ... } ⇒ Object
31
32
33
34
35
|
# File 'lib/race_block.rb', line 31
def self.config
self.configuration ||= Configuration.new
yield(configuration) if block_given?
configuration
end
|
.key(key) ⇒ Object
45
46
47
|
# File 'lib/race_block.rb', line 45
def self.key(key)
"race_block_#{key}"
end
|
.logger ⇒ Object
41
42
43
|
# File 'lib/race_block.rb', line 41
def self.logger
@logger ||= Logger.new($stdout)
end
|
.set_token_and_wait(key, sleep_delay: config.sleep_delay, desync_tokens: 0) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/race_block.rb', line 81
def self.set_token_and_wait(key, sleep_delay: config.sleep_delay, desync_tokens: 0)
sleep desync_tokens token = SecureRandom.hex
RaceBlock.client.set(key, token)
RaceBlock.client.expire(key, (sleep_delay + 15).round)
sleep sleep_delay
return true if RaceBlock.client.get(key) == token
logger.debug("Token out of sync")
false
end
|
.start(base_key, expire: config.expire, expiration_delay: config.expiration_delay, **args) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/race_block.rb', line 53
def self.start(base_key, expire: config.expire, expiration_delay: config.expiration_delay, **args)
raise("A key must be provided to start a RaceBlock") if base_key.empty?
key = RaceBlock.key(base_key)
RaceBlock.client.expire(key, 10) if RaceBlock.client.ttl(key) == -1
return logger.debug("Token already exists") if RaceBlock.client.get(key)
return unless set_token_and_wait(key, **args)
RaceBlock.client.expire(key, expire)
logger.debug("Running block")
r = yield
RaceBlock.client.expire(key, expiration_delay)
r
end
|