Skip to content

Commit 8fe0bd6

Browse files
committed
Applies http-timeout to download stream
1 parent e6a5838 commit 8fe0bd6

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

ProjectPlugins/CodexPlugin/CodexNode.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,27 @@ private string[] GetPeerMultiAddresses(CodexNode peer, DebugInfo peerInfo)
264264
private void DownloadToFile(string contentId, TrackedFile file, Action<Failure> onFailure)
265265
{
266266
using var fileStream = File.OpenWrite(file.Filename);
267+
var timeout = tools.TimeSet.HttpCallTimeout();
267268
try
268269
{
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)}");
271288
}
272289
catch
273290
{

0 commit comments

Comments
 (0)