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

Class StreamFetcher

apps/proxy/hls_proxy/server.py:342–595  ·  view source on GitHub ↗

Handles HTTP requests for stream segments with connection pooling. Attributes: manager (StreamManager): Associated stream manager instance buffer (StreamBuffer): Buffer for storing segments session (requests.Session): Persistent HTTP session redirect_cac

Source from the content-addressed store, hash-verified

340 logging.info(f"Started cleanup thread for channel {self.channel_id}")
341
342class StreamFetcher:
343 """
344 Handles HTTP requests for stream segments with connection pooling.
345
346 Attributes:
347 manager (StreamManager): Associated stream manager instance
348 buffer (StreamBuffer): Buffer for storing segments
349 session (requests.Session): Persistent HTTP session
350 redirect_cache (dict): Cache for redirect responses
351
352 Features:
353 - Connection pooling and reuse
354 - Redirect caching
355 - Rate limiting
356 - Automatic retries
357 - Host fallback
358 """
359 def __init__(self, manager: StreamManager, buffer: StreamBuffer):
360 self.manager = manager
361 self.buffer = buffer
362 self.stream_url = manager.current_url
363 self.session = requests.Session()
364
365 # Configure session headers
366 self.session.headers.update({
367 'User-Agent': manager.user_agent,
368 'Connection': 'keep-alive'
369 })
370
371 # Set up connection pooling
372 adapter = requests.adapters.HTTPAdapter(
373 pool_connections=2, # Number of connection pools
374 pool_maxsize=4, # Connections per pool
375 max_retries=3, # Auto-retry failed requests
376 pool_block=False # Don't block when pool is full
377 )
378
379 # Apply adapter to both HTTP and HTTPS
380 self.session.mount('http://', adapter)
381 self.session.mount('https://', adapter)
382
383 # Request optimization
384 self.last_request_time = 0
385 self.min_request_interval = 0.05 # Minimum time between requests
386 self.last_host = None # Cache last successful host
387 self.redirect_cache = {} # Cache redirect responses
388 self.redirect_cache_limit = 1000
389
390 def cleanup_redirect_cache(self):
391 """Remove old redirect cache entries"""
392 if len(self.redirect_cache) > self.redirect_cache_limit:
393 self.redirect_cache.clear()
394
395 def get_base_host(self, url: str) -> str:
396 """
397 Extract base host from URL.
398
399 Args:

Callers 2

_fetch_loopMethod · 0.85
initialize_channelMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected