Module: Parse::CreateLock
- Defined in:
- lib/parse/model/core/create_lock.rb
Overview
Mutual-exclusion primitive for first_or_create! / create_or_update! to
prevent TOCTOU duplicate creation under concurrency. Backed by a Moneta
cache store (typically Redis) using atomic #create (SETNX semantics).
This is the latency optimization layer; a MongoDB unique index on the constrained tuple is the correctness floor that survives Redis outages, TTL expiry races, and bypassed locks. The lock here is best-effort and fails open by design when the store is unreachable.
Threading model:
- Process-local fallback: when the store is the in-memory Moneta adapter
(or unconfigured), the lock degrades to a per-key
Mutex. Threads in the same Ruby process serialize; cross-process callers do not. This is safe for single-Puma-process tests but does not protect production deployments with multiple dynos / workers. - Cross-process: when the store is Redis-backed Moneta,
#createis atomic and the lock excludes other processes.
Key derivation: HMAC-SHA256 of a canonical payload when a secret is
configured (preferred); plain SHA256 otherwise (deterministic across
processes — required for Redis-backed locking — but key material is
enumerable via Redis MONITOR/snapshots). Operators wanting hardened key
material against snapshot/MONITOR exposure should set
PARSE_STACK_LOCK_SECRET or Parse.synchronize_create_secret.
Constant Summary collapse
- DEFAULT_TTL =
Floor for the lock lease, in seconds. When no
ttl:is given the lease is sized from the client's request budget instead (see default_ttl); this is the minimum it can be. 3- DEFAULT_WAIT =
2.0- DEFAULT_POLL_BASE =
0.05- DEFAULT_POLL_JITTER =
0.015- MAX_TTL =
Ceiling for the lock lease, in seconds. The lease only matters when a holder dies without releasing; a normal exit always releases.
300- LOCKED_REQUESTS =
Requests the locked block is sized for: the find, then the create.
2- MAX_WAIT =
30- MAX_PAYLOAD_BYTES =
8_192- MAX_DEPTH =
4- KEY_PREFIX =
"parse-stack:foc:v1:"- DEGRADED_WARNING_THROTTLE_SECONDS =
60
Class Method Summary collapse
-
.canonical_key(parse_class:, query_attrs:, session_token: nil, master_key: nil) ⇒ String
Canonical lock key for the given inputs.
-
.default_ttl(client = nil) ⇒ Integer
The lease used when no
ttl:is given. -
.synchronize(parse_class:, query_attrs:, options: {}, session_token: nil, master_key: nil, &block) ⇒ Object
Run
blockwhile holding a mutex keyed by the canonical form ofparse_class + auth context + query_attrs.
Class Method Details
.canonical_key(parse_class:, query_attrs:, session_token: nil, master_key: nil) ⇒ String
Canonical lock key for the given inputs. Public for tests.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/parse/model/core/create_lock.rb', line 145 def canonical_key(parse_class:, query_attrs:, session_token: nil, master_key: nil) principal = principal_marker(session_token, master_key) attrs_payload = canonicalize_attrs(query_attrs, parse_class: parse_class) app_id = parse_application_id payload = "#{app_id}|#{parse_class}|#{principal}|#{attrs_payload}" if payload.bytesize > MAX_PAYLOAD_BYTES raise Parse::CreateLockInvalidKey, "synchronize key payload exceeds #{MAX_PAYLOAD_BYTES} bytes (got #{payload.bytesize})" end secret = lock_secret_for(store: LockBackend.lock_store) digest = if secret OpenSSL::HMAC.hexdigest("SHA256", secret, payload) else Digest::SHA256.hexdigest(payload) end "#{KEY_PREFIX}#{digest}" end |
.default_ttl(client = nil) ⇒ Integer
The lease used when no ttl: is given. The lock is not renewed
while held, so the lease must outlast the block it guards: a find
and a create, each of which may run through every retry at the full
request timeout. A lease shorter than that expires mid-block, lets a
second caller in, and produces the duplicate the lock exists to
prevent. The result is clamped to DEFAULT_TTL..MAX_TTL. Pass ttl:
to override it.
135 136 137 138 139 140 141 |
# File 'lib/parse/model/core/create_lock.rb', line 135 def default_ttl(client = nil) client ||= Parse::Client.client budget = client.respond_to?(:request_time_budget) ? client.request_time_budget : 0 clamp((budget * LOCKED_REQUESTS).ceil, DEFAULT_TTL, MAX_TTL) rescue StandardError DEFAULT_TTL end |
.synchronize(parse_class:, query_attrs:, options: {}, session_token: nil, master_key: nil, &block) ⇒ Object
Run block while holding a mutex keyed by the canonical form of
parse_class + auth context + query_attrs. Yields nothing; the
block's return value is returned.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/parse/model/core/create_lock.rb', line 67 def synchronize(parse_class:, query_attrs:, options: {}, session_token: nil, master_key: nil, &block) raise ArgumentError, "block required" unless block_given? ttl = [:ttl] ? clamp(Integer([:ttl]), 1, MAX_TTL) : default_ttl wait = clamp(Float([:wait] || DEFAULT_WAIT), 0.0, MAX_WAIT) on_degraded = [:on_degraded] || :warn key = canonical_key( parse_class: parse_class, query_attrs: query_attrs, session_token: session_token, master_key: master_key, ) store = LockBackend.lock_store if LockBackend.degraded_store?(store) LockBackend.handle_degraded( on_degraded, key, source: "Parse::CreateLock", unavailable_error: Parse::CreateLockUnavailableError, ) return LockBackend.synchronize_process_mutex(key, &block) end owner = SecureRandom.uuid acquired_at = nil start = LockBackend.monotonic_now loop do if LockBackend.try_acquire(store, key, owner, ttl) acquired_at = LockBackend.monotonic_now wait_ms = ((acquired_at - start) * 1000).round instrument("acquired", key, wait_ms: wait_ms) break end elapsed = LockBackend.monotonic_now - start if elapsed >= wait waited_ms = (elapsed * 1000).round instrument("timeout", key, waited_ms: waited_ms) raise Parse::CreateLockTimeoutError, "Could not acquire create-lock for #{parse_class} within #{wait}s" end instrument("contended", key, elapsed_ms: (elapsed * 1000).round) if elapsed > 0 sleep(LockBackend.poll_interval) end begin yield ensure if acquired_at LockBackend.release(store, key, owner) held_ms = ((LockBackend.monotonic_now - acquired_at) * 1000).round instrument("released", key, held_ms: held_ms) end end end |