futex 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8cbb61dfb861233a3f0883cd1ce83858bbbad9ec2b71f17f286edbbe2d0c57b
4
- data.tar.gz: 6ef08599e9100ae73d0706270ce3c7aa6cb1b58f9c64ceb9f54026f07ac547f1
3
+ metadata.gz: 53e0b207143da0366545baa93f2801b8e79a5947682954191b9817e8d3c6fc9b
4
+ data.tar.gz: 5dac5eaab880bf15c6da9e7300fe2b7722d16c8e6c308cabe96372e8db32cce5
5
5
  SHA512:
6
- metadata.gz: bc33d6417050639fd839530b820de078406b112d8d6ffc914c9956ce6b84b511be82678a2b438f9b9fa13311487a3033d6ac3e0efbe6262836b3c2c06443824d
7
- data.tar.gz: 383ced7c5df759d14d494a183562771cc0cf3904b33bc9152c0870cea24e2683d966b660e11751a01aa32ab0137f538aaef7bc2eac9a53fa94149e46aa06dc84
6
+ metadata.gz: ec18ba0e63dd2584f9294e1b60d37a1c338029b6d1fba0f88c4e4ba1dd0a920442522b81ba307688340413038c5e01d2bc007a9360999b82d6fe8a1830df45ba
7
+ data.tar.gz: c1b855e96480add275103776b65e0bad32853292f5c1ee7cb21bb7c5be9b1c220f927b8e7ebb12d499922e296fd5baae87703cb97c37e5c27499bfadb377bcc4
data/.rubocop.yml CHANGED
@@ -12,5 +12,7 @@ Metrics/CyclomaticComplexity:
12
12
  Max: 10
13
13
  Metrics/PerceivedComplexity:
14
14
  Max: 10
15
+ Metrics/ParameterLists:
16
+ Max: 10
15
17
  Layout/AlignParameters:
16
18
  Enabled: false
data/README.md CHANGED
@@ -34,12 +34,16 @@ constructor of the `Futex` class, including:
34
34
  * `log`: an object that implements `debug()` method, which will
35
35
  receive supplementary messages from the locking mechanism;
36
36
 
37
+ * `logging`: set it to `false` if you don't want to see any logs;
38
+
37
39
  * `timeout`: the number of seconds to wait for the lock availability
38
40
  (an exception is raised when the wait is expired);
39
41
 
40
42
  * `sleep`: the number of seconds to wait between attempts to acquire
41
43
  the lock file (the smaller the number, the more reactive is the software,
42
- but the higher the load for the file system and the CPU).
44
+ but the higher the load for the file system and the CPU);
45
+
46
+ * `lock`: the absolute path of the lock file;
43
47
 
44
48
  That's it.
45
49
 
data/futex.gemspec CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.rubygems_version = '2.3.3'
32
32
  s.required_ruby_version = '>=2.3'
33
33
  s.name = 'futex'
34
- s.version = '0.1.0'
34
+ s.version = '0.2.0'
35
35
  s.license = 'MIT'
36
36
  s.summary = 'File-based mutex'
37
37
  s.description = 'Ruby Gem for file-based locking'
data/lib/futex.rb CHANGED
@@ -31,11 +31,13 @@ require 'time'
31
31
  # License:: MIT
32
32
  class Futex
33
33
  def initialize(path, log: STDOUT, timeout: 16, sleep: 0.005,
34
- lock: path + '.lock')
34
+ lock: path + '.lock', logging: true)
35
35
  raise "File path can't be nil" if path.nil?
36
36
  @path = path
37
37
  raise "Log can't be nil" if log.nil?
38
38
  @log = log
39
+ raise "Logging can't be nil" if logging.nil?
40
+ @logging = logging
39
41
  raise "Timeout can't be nil" if timeout.nil?
40
42
  raise "Timeout must be positive: #{timeout}" unless timeout.positive?
41
43
  @timeout = timeout
@@ -75,7 +77,7 @@ exclusive access to #{@path}, #{age(start)} already: #{IO.read(@lock)}")
75
77
  acq = Time.now
76
78
  res = yield(@path)
77
79
  FileUtils.rm(@lock)
78
- puts("Unlocked by \"#{Thread.current.name}\" in #{age(acq)}: #{@path}")
80
+ debug("Unlocked by \"#{Thread.current.name}\" in #{age(acq)}: #{@path}")
79
81
  res
80
82
  end
81
83
 
@@ -86,6 +88,7 @@ exclusive access to #{@path}, #{age(start)} already: #{IO.read(@lock)}")
86
88
  end
87
89
 
88
90
  def debug(msg)
91
+ return unless @logging
89
92
  if @log.respond_to?(:debug)
90
93
  @log.debug(msg)
91
94
  elsif @log.respond_to?(:puts)
data/test/test_futex.rb CHANGED
@@ -36,7 +36,7 @@ class FutexTest < Minitest::Test
36
36
  Dir.mktmpdir do |dir|
37
37
  path = File.join(dir, 'a/b/c/file.txt')
38
38
  Threads.new(5).assert(100) do |_, r|
39
- Futex.new(path).open do |f|
39
+ Futex.new(path, logging: false).open do |f|
40
40
  text = "op no.#{r}"
41
41
  IO.write(f, text)
42
42
  assert_equal(text, IO.read(f))
@@ -48,11 +48,11 @@ class FutexTest < Minitest::Test
48
48
  def test_syncs_access_to_file_in_slow_motion
49
49
  Dir.mktmpdir do |dir|
50
50
  path = File.join(dir, 'a/b/c/file.txt')
51
- Threads.new(2).assert(4) do |_, r|
52
- Futex.new(path).open do |f|
51
+ Threads.new(5).assert(50) do |_, r|
52
+ Futex.new(path, logging: false).open do |f|
53
53
  text = "op no.#{r}"
54
54
  IO.write(f, text)
55
- sleep 0.2
55
+ sleep 0.01
56
56
  assert_equal(text, IO.read(f))
57
57
  end
58
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: futex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko