MCPcopy Create free account
hub / github.com/Dispatcharr/Dispatcharr / _stream_follow

Function _stream_follow

apps/output/streaming_chunk_cache.py:141–187  ·  view source on GitHub ↗

Follower: read chunks as the leader writes them.

(redis, base_key, source, cache_ttl, lock_ttl, poll_interval, max_follower_wait)

Source from the content-addressed store, hash-verified

139
140
141def _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
190def stream_cached_response(

Callers 1

stream_cached_responseFunction · 0.85

Calls 10

_chunks_keyFunction · 0.85
_lock_keyFunction · 0.85
_decode_chunkFunction · 0.85
_get_statusFunction · 0.85
_clear_build_keysFunction · 0.85
_try_acquire_lockFunction · 0.85
_stream_buildFunction · 0.85
_poll_waitFunction · 0.85
lindexMethod · 0.80
existsMethod · 0.45

Tested by

no test coverage detected