Follower: read chunks as the leader writes them.
(redis, base_key, source, cache_ttl, lock_ttl, poll_interval, max_follower_wait)
| 139 | |
| 140 | |
| 141 | def _stream_follow(redis, base_key, source, cache_ttl, lock_ttl, poll_interval, max_follower_wait): |
| 142 | """Follower: read chunks as the leader writes them.""" |
| 143 | offset = 0 |
| 144 | deadline = time.monotonic() + max_follower_wait |
| 145 | idle_polls = 0 |
| 146 | chunks_key = _chunks_key(base_key) |
| 147 | lock_key = _lock_key(base_key) |
| 148 | |
| 149 | while True: |
| 150 | chunk = redis.lindex(chunks_key, offset) |
| 151 | if chunk is not None: |
| 152 | idle_polls = 0 |
| 153 | yield _decode_chunk(chunk) |
| 154 | offset += 1 |
| 155 | continue |
| 156 | |
| 157 | status = _get_status(redis, base_key) |
| 158 | if status == STATUS_READY: |
| 159 | break |
| 160 | |
| 161 | if status == STATUS_ERROR: |
| 162 | _clear_build_keys(redis, base_key) |
| 163 | if offset == 0 and _try_acquire_lock(redis, base_key, lock_ttl): |
| 164 | yield from _stream_build(redis, base_key, source, cache_ttl, lock_ttl) |
| 165 | return |
| 166 | raise RuntimeError("Chunk cache build failed") |
| 167 | |
| 168 | if time.monotonic() >= deadline: |
| 169 | if offset == 0 and _try_acquire_lock(redis, base_key, lock_ttl): |
| 170 | logger.warning("Chunk cache follower timed out; rebuilding %s", base_key) |
| 171 | yield from _stream_build(redis, base_key, source, cache_ttl, lock_ttl) |
| 172 | return |
| 173 | logger.warning("Chunk cache follower timed out after partial read for %s", base_key) |
| 174 | break |
| 175 | |
| 176 | lock_active = bool(redis.exists(lock_key)) |
| 177 | if status != STATUS_BUILDING and not lock_active: |
| 178 | idle_polls += 1 |
| 179 | if offset == 0 and idle_polls >= max(1, int(1.0 / poll_interval)): |
| 180 | if _try_acquire_lock(redis, base_key, lock_ttl): |
| 181 | logger.warning("Chunk cache leader lost; rebuilding %s", base_key) |
| 182 | yield from _stream_build(redis, base_key, source, cache_ttl, lock_ttl) |
| 183 | return |
| 184 | else: |
| 185 | idle_polls = 0 |
| 186 | |
| 187 | _poll_wait(poll_interval) |
| 188 | |
| 189 | |
| 190 | def stream_cached_response( |
no test coverage detected