sparoid 1.0.11 → 1.0.12

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: b933d6729f0ce76259c272550548532b15a206218239b9f824cd58b0deec6ee3
4
- data.tar.gz: 61a1ec401ecebf13674e3b66e684df99ea5007f051e91b5bf98cc783e9f91c15
3
+ metadata.gz: 12dad3dbab45bb38650465dab2b26f356c051569b06d20ba1bcfd6af11eb9701
4
+ data.tar.gz: 0b17fe457b36978f1b682153cb14578192cb657a846fadcbb3abb2cf82e07bb6
5
5
  SHA512:
6
- metadata.gz: 173cb6c71c46ba88153306d5124879ade2f53eedda41da85b58475abc3fddc0e0d5dee00ab459e546ab0b283411044dbaa016369ccb74a791a22126dc5dc99dd
7
- data.tar.gz: 71318184eb58dbcbcc713aebd1c2e22375562af77ae29083b90f0933bc078991f1860fc00fbccad5e55396f37845b098f3c4494cae381c71e79b30a8e6a8124a
6
+ metadata.gz: 1c31f7b1fa30e2d064b0e92e79b2b567bdfa4021995d7f52c25b6b4373d08062ed091aab8187aeb42051c11e0c7c1b994d158878e64311eed28e68d1e3fe4008
7
+ data.tar.gz: cacd2adf45ed9fa8e31b063809e01cf0371c6d13670e24b5bdb17f7c2623c2f9592c7e38b6df18a5f529db9628d4197baf323a9d58124b8eb6ea75762c3a6d78
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [1.0.12] - 2021-06-14
2
+
3
+ - Use file locking to prevent multiple processes/threads to write to the public ip cache file
4
+
1
5
  ## [1.0.11] - 2021-06-14
2
6
 
3
7
  - Log hostname when reporting unhandled exceptions
data/lib/sparoid.rb CHANGED
@@ -76,23 +76,38 @@ module Sparoid
76
76
  end
77
77
 
78
78
  def cached_public_ip
79
- File.open("/tmp/.sparoid_public_ip", "a+") do |f|
80
- if f.size.zero? || (Time.now - f.mtime) > 60 # cache for 1min max
81
- update_cache(f)
82
- else
83
- Resolv::IPv4.create f.read
84
- end
79
+ if up_to_date_cache?
80
+ read_cache
81
+ else
82
+ write_cache
85
83
  end
86
84
  rescue StandardError => e
87
85
  warn "Sparoid: #{e.inspect}"
88
86
  public_ip
89
87
  end
90
88
 
91
- def update_cache(file)
92
- ip = public_ip
93
- file.truncate(0)
94
- file.write ip.to_s
95
- ip
89
+ def up_to_date_cache?
90
+ mtime = File.mtime("/tmp/.sparoid_public_ip")
91
+ (Time.now - mtime) <= 60 # cache is valid for 1 min
92
+ rescue Errno::ENOENT
93
+ false
94
+ end
95
+
96
+ def read_cache
97
+ File.open("/tmp/.sparoid_public_ip", "r") do |f|
98
+ f.flock(File::LOCK_SH)
99
+ Resolv::IPv4.create f.read
100
+ end
101
+ end
102
+
103
+ def write_cache
104
+ File.open("/tmp/.sparoid_public_ip", File::WRONLY | File::CREAT, 0o0644) do |f|
105
+ f.flock(File::LOCK_EX)
106
+ ip = public_ip
107
+ f.truncate(0)
108
+ f.write ip.to_s
109
+ ip
110
+ end
96
111
  end
97
112
 
98
113
  def public_ip
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sparoid
4
- VERSION = "1.0.11"
4
+ VERSION = "1.0.12"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.11
4
+ version: 1.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Hörberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-14 00:00:00.000000000 Z
11
+ date: 2021-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor