Commit 992ab16
authored
Make Sparoid::Instance public-IP resolution thread-safe and self-healing (#25)
* Make Sparoid::Instance public-IP resolution thread-safe and empty-aware
Sparoid::Instance memoized with `@public_ips ||= super`, which is neither
thread-safe nor empty-aware: `[]` is truthy, so a transient empty resolution
(icanhazip down or empty body) was pinned for the process lifetime and every
later knock then sent zero SPA packets. Under a shared instance hit by a thread
pool, the first wave of knocks also resolved concurrently and one slow/empty
response clobbered a good value via last-writer-wins.
Move the caching into Instance#cached_public_ips: resolve behind a per-instance
mutex with a double-check (kills the thundering herd and the clobber) and cache
only a non-empty result, so a transient empty resolution self-heals on the next
knock instead of pinning the instance.
Also raise when generate_messages would otherwise produce no messages: an empty
public-IP resolution with no IPv6 fallback used to let auth report success
while sending zero packets. It now raises Sparoid::PublicIPError (a sibling of
ResolvError under Error) so the caller can tell 'I cannot resolve my own public
IP' apart from 'the target host is unreachable', instead of a silent no-op.
* Stub icanhazip in tests instead of allowing real network
Drop WebMock.allow_net_connect! and stub icanhazip in test_it_resolves_public_ip
so the suite no longer depends on a working internet connection or icanhazip
being up (which would make CI flaky and let mistyped stubs fall through to the
network).
* Initialize @public_ips to [] and drop safe-navigation checks
@public_ips is now always an array, so the empty/cached check is a plain
.any? instead of &.any?.1 parent edc79cb commit 992ab16
4 files changed
Lines changed: 56 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
106 | 108 | | |
107 | 109 | | |
108 | 110 | | |
| |||
253 | 255 | | |
254 | 256 | | |
255 | 257 | | |
256 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
257 | 261 | | |
258 | 262 | | |
259 | 263 | | |
260 | | - | |
261 | | - | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
262 | 267 | | |
263 | 268 | | |
264 | 269 | | |
265 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
266 | 283 | | |
267 | 284 | | |
268 | 285 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
228 | 229 | | |
229 | 230 | | |
230 | 231 | | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
231 | 264 | | |
232 | 265 | | |
233 | 266 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
0 commit comments