File tree Expand file tree Collapse file tree 1 file changed +19
-2
lines changed
ProjectPlugins/CodexPlugin Expand file tree Collapse file tree 1 file changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -264,10 +264,27 @@ private string[] GetPeerMultiAddresses(CodexNode peer, DebugInfo peerInfo)
264
264
private void DownloadToFile ( string contentId , TrackedFile file , Action < Failure > onFailure )
265
265
{
266
266
using var fileStream = File . OpenWrite ( file . Filename ) ;
267
+ var timeout = tools . TimeSet . HttpCallTimeout ( ) ;
267
268
try
268
269
{
269
- using var downloadStream = CodexAccess . DownloadFile ( contentId , onFailure ) ;
270
- downloadStream . CopyTo ( fileStream ) ;
270
+ // Type of stream generated by openAPI client does not support timeouts.
271
+ var start = DateTime . UtcNow ;
272
+ var cts = new CancellationTokenSource ( ) ;
273
+ var downloadTask = Task . Run ( ( ) =>
274
+ {
275
+ using var downloadStream = CodexAccess . DownloadFile ( contentId , onFailure ) ;
276
+ downloadStream . CopyTo ( fileStream ) ;
277
+ } , cts . Token ) ;
278
+
279
+ while ( DateTime . UtcNow - start < timeout )
280
+ {
281
+ if ( downloadTask . IsFaulted ) throw downloadTask . Exception ;
282
+ if ( downloadTask . IsCompletedSuccessfully ) return ;
283
+ Thread . Sleep ( 100 ) ;
284
+ }
285
+
286
+ cts . Cancel ( ) ;
287
+ throw new TimeoutException ( $ "Download of '{ contentId } ' timed out after { Time . FormatDuration ( timeout ) } ") ;
271
288
}
272
289
catch
273
290
{
You can’t perform that action at this time.
0 commit comments