@@ -131,6 +131,19 @@ def record_completion(self, range_count: int, byte_count: int) -> None:
131131 self .pending_ranges = max (0 , self .pending_ranges - range_count )
132132 self .pending_bytes = max (0 , self .pending_bytes - byte_count )
133133
134+ async def close (self ) -> None :
135+ """Closes the underlying multiplexer and stream cleanly."""
136+ try :
137+ await self .multiplexer .close ()
138+ except Exception :
139+ pass
140+ try :
141+ res = self .stream .close ()
142+ if inspect .isawaitable (res ):
143+ await res
144+ except Exception :
145+ pass
146+
134147
135148class _StreamPool :
136149 """Manages a pool of _ManagedStream instances with dynamic scaling and least-loaded dispatch."""
@@ -196,16 +209,7 @@ async def _scale_up(self) -> None:
196209 new_worker = await self ._stream_factory ()
197210 async with self ._lock :
198211 if self ._closed :
199- try :
200- await new_worker .multiplexer .close ()
201- except Exception :
202- pass
203- try :
204- res = new_worker .stream .close ()
205- if inspect .isawaitable (res ):
206- await res
207- except Exception :
208- pass
212+ await new_worker .close ()
209213 return
210214 self .workers .append (new_worker )
211215 except Exception as e :
@@ -228,16 +232,7 @@ async def close(self) -> None:
228232 for task in list (self ._background_tasks ):
229233 task .cancel ()
230234 for w in workers :
231- try :
232- await w .multiplexer .close ()
233- except Exception :
234- pass
235- try :
236- res = w .stream .close ()
237- if inspect .isawaitable (res ):
238- await res
239- except Exception :
240- pass
235+ await w .close ()
241236
242237
243238class AsyncMultiRangeDownloader :
@@ -392,12 +387,6 @@ def __init__(
392387 self .full_obj_server_crc32c : Optional [int ] = None
393388
394389 self .stream_config = stream_config
395- self .min_connections = stream_config .min_connections if stream_config else 1
396- self .max_connections = stream_config .max_connections if stream_config else 1
397- self .target_io_depth = stream_config .target_io_depth if stream_config else 8
398- self .target_bytes = (
399- stream_config .target_bytes if stream_config else 8 * 1024 * 1024
400- )
401390 self ._pool : Optional [_StreamPool ] = None
402391 self ._primary_worker : Optional [_ManagedStream ] = None
403392 self ._metadata : Optional [List [Tuple [str , str ]]] = None
@@ -494,8 +483,6 @@ async def _do_open():
494483 self .generation = self .read_obj_str .generation_number
495484 if self .read_obj_str .read_handle :
496485 self .read_handle = self .read_obj_str .read_handle
497- if getattr (self .read_obj_str , "routing_token" , None ):
498- self ._routing_token = self .read_obj_str .routing_token
499486 if self .read_obj_str .persisted_size is not None :
500487 self .persisted_size = self .read_obj_str .persisted_size
501488 self .is_finalized = self .read_obj_str .is_finalized
@@ -511,14 +498,14 @@ async def _do_open():
511498 if self .stream_config is not None and self .stream_config .max_connections > 1 :
512499 self ._pool = _StreamPool (
513500 stream_factory = self ._create_new_stream_worker ,
514- min_connections = self .min_connections ,
515- max_connections = self .max_connections ,
516- target_io_depth = self .target_io_depth ,
517- target_bytes = self .target_bytes ,
501+ min_connections = self .stream_config . min_connections ,
502+ max_connections = self .stream_config . max_connections ,
503+ target_io_depth = self .stream_config . target_io_depth ,
504+ target_bytes = self .stream_config . target_bytes ,
518505 )
519506 await self ._pool .add_worker (self ._primary_worker )
520507
521- for _ in range (self .min_connections - 1 ):
508+ for _ in range (self .stream_config . min_connections - 1 ):
522509 worker = await self ._create_new_stream_worker ()
523510 await self ._pool .add_worker (worker )
524511 else :
@@ -545,8 +532,6 @@ async def _create_new_stream_worker(self) -> _ManagedStream:
545532 self .generation = stream .generation_number
546533 if stream .read_handle :
547534 self .read_handle = stream .read_handle
548- if getattr (stream , "routing_token" , None ):
549- self ._routing_token = stream .routing_token
550535
551536 mux = _StreamMultiplexer (stream )
552537 return _ManagedStream (stream , mux )
@@ -582,8 +567,6 @@ async def factory():
582567 self .generation = stream .generation_number
583568 if stream .read_handle :
584569 self .read_handle = stream .read_handle
585- if getattr (stream , "routing_token" , None ):
586- self ._routing_token = stream .routing_token
587570 self .is_finalized = stream .is_finalized
588571 self .full_obj_server_crc32c = stream .full_obj_server_crc32c
589572
@@ -665,13 +648,10 @@ async def _download_ranges_on_worker(
665648 # Heuristic to detect full object reads:
666649 # - Implicit full object read: start offset is 0 and length is 0 (read all).
667650 # - Explicit full object read: start offset is 0 and length matches the exact persisted size.
668- is_full_object_read = self .is_finalized and (
669- (offset == 0 and length == 0 )
670- or (
671- self .persisted_size is not None
672- and offset == 0
673- and length == self .persisted_size
674- )
651+ is_full_object_read = (offset == 0 and length == 0 ) or (
652+ self .persisted_size is not None
653+ and offset == 0
654+ and length == self .persisted_size
675655 )
676656 download_states [read_id ] = _DownloadState (
677657 initial_offset = offset ,
@@ -685,9 +665,7 @@ async def _download_ranges_on_worker(
685665 "read_handle" : self .read_handle ,
686666 "routing_token" : None ,
687667 "enable_checksum" : enable_checksum ,
688- "full_obj_server_crc32c" : self .full_obj_server_crc32c
689- if self .is_finalized
690- else None ,
668+ "full_obj_server_crc32c" : self .full_obj_server_crc32c ,
691669 }
692670
693671 read_ids = set (download_states .keys ())
@@ -787,8 +765,6 @@ async def generator():
787765 if initial_state .get ("read_handle" ):
788766 self .read_handle = initial_state ["read_handle" ]
789767 finally :
790- if self ._multiplexer is not None :
791- self ._multiplexer .unregister (read_ids )
792768 if worker .multiplexer is not None :
793769 worker .multiplexer .unregister (read_ids )
794770
0 commit comments