Skip to content

Commit

Permalink
Applies http-timeout to download stream
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed Oct 25, 2024
1 parent e6a5838 commit 8fe0bd6
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions ProjectPlugins/CodexPlugin/CodexNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,27 @@ private string[] GetPeerMultiAddresses(CodexNode peer, DebugInfo peerInfo)
private void DownloadToFile(string contentId, TrackedFile file, Action<Failure> onFailure)
{
using var fileStream = File.OpenWrite(file.Filename);
var timeout = tools.TimeSet.HttpCallTimeout();
try
{
using var downloadStream = CodexAccess.DownloadFile(contentId, onFailure);
downloadStream.CopyTo(fileStream);
// Type of stream generated by openAPI client does not support timeouts.
var start = DateTime.UtcNow;
var cts = new CancellationTokenSource();
var downloadTask = Task.Run(() =>
{
using var downloadStream = CodexAccess.DownloadFile(contentId, onFailure);
downloadStream.CopyTo(fileStream);
}, cts.Token);

while (DateTime.UtcNow - start < timeout)
{
if (downloadTask.IsFaulted) throw downloadTask.Exception;
if (downloadTask.IsCompletedSuccessfully) return;
Thread.Sleep(100);
}

cts.Cancel();
throw new TimeoutException($"Download of '{contentId}' timed out after {Time.FormatDuration(timeout)}");
}
catch
{
Expand Down

0 comments on commit 8fe0bd6

Please sign in to comment.