Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to improve restore behavior #3237

Merged
merged 2 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Paket.Core/Common/NetUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,16 @@ let rec private _safeGetFromUrl (auth:Auth option, url : string, contentType : s
let uri = Uri url
use client = createHttpClient (url,auth)
let! tok = Async.CancellationToken
let tokSource = System.Threading.CancellationTokenSource.CreateLinkedTokenSource(tok)
tokSource.CancelAfter(60000)

if notNullOrEmpty contentType then
addAcceptHeader client contentType

if verbose then
verbosefn "Starting request to '%O'" uri
use _ = Profile.startCategory Profile.Category.NuGetRequest
let! raw = client.DownloadStringTaskAsync(uri, tok) |> Async.AwaitTaskWithoutAggregate
let! raw = client.DownloadStringTaskAsync(uri, tokSource.Token) |> Async.AwaitTaskWithoutAggregate
return SuccessResponse raw
with

Expand Down
39 changes: 33 additions & 6 deletions src/Paket.Core/Dependencies/NuGet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -892,26 +892,53 @@ let private downloadAndExtractPackage(alternativeProjectRoot, root, isLocalOverr
request.UseDefaultCredentials <- true

request.Proxy <- NetUtils.getDefaultProxyFor source.Url
use! httpResponse = request.AsyncGetResponse()

let lastSpeedMeasure = Stopwatch.StartNew()
let mutable readSinceLastMeasure = 0L

let! child = Async.StartChild(request.AsyncGetResponse(), 60000)
use! httpResponse = child
use httpResponseStream = httpResponse.GetResponseStream()

let bufferSize = 4096
let bufferSize = 1024 * 10
let buffer : byte [] = Array.zeroCreate bufferSize
let bytesRead = ref -1

use fileStream = File.Create(targetFileName)
let printProgress = not Console.IsOutputRedirected
let mutable pos = 0L

let length = try Some httpResponseStream.Length with :? System.NotSupportedException -> None
let! tok = Async.CancellationToken
while !bytesRead <> 0 do
let! bytes = httpResponseStream.AsyncRead(buffer, 0, bufferSize)
if printProgress && lastSpeedMeasure.Elapsed > TimeSpan.FromSeconds(10.) then
// report speed and progress
let speed = int (float readSinceLastMeasure * 8. / float lastSpeedMeasure.ElapsedMilliseconds)
let percent =
match length with
| Some l -> sprintf "%d" (int (pos * 100L / l))
| None -> "Unknown"
tracefn "Still downloading from %O to %s (%d kbit/s, %s %%)" !downloadUrl targetFileName speed percent
readSinceLastMeasure <- 0L
lastSpeedMeasure.Restart()
// if there is no response for a minute -> abort

let s = System.Threading.CancellationTokenSource.CreateLinkedTokenSource(tok)
s.CancelAfter (60000)
let! bytes = httpResponseStream.ReadAsync(buffer, 0, bufferSize, s.Token) |> Async.AwaitTaskWithoutAggregate
//let! bytes = httpResponseStream.AsyncRead(buffer, 0, bufferSize)
bytesRead := bytes
do! fileStream.AsyncWrite(buffer, 0, !bytesRead)
do! fileStream.WriteAsync(buffer, 0, !bytesRead, tok) |> Async.AwaitTaskWithoutAggregate
readSinceLastMeasure <- readSinceLastMeasure + int64 bytes
pos <- pos + int64 bytes

match (httpResponse :?> HttpWebResponse).StatusCode with
| HttpStatusCode.OK -> ()
| statusCode -> failwithf "HTTP status code was %d - %O" (int statusCode) statusCode

tracefn "Download of %O %O%s done in %s." packageName version groupString (Utils.TimeSpanToReadableString sw.Elapsed)

let speed = int (float pos * 8. / float lastSpeedMeasure.ElapsedMilliseconds)
let size = pos / (1024L * 1024L)
tracefn "Download of %O %O%s done in %s. (%d kbit/s, %d MB)" packageName version groupString (Utils.TimeSpanToReadableString sw.Elapsed) speed size

try
if downloadLicense && not (String.IsNullOrWhiteSpace nugetPackage.LicenseUrl) then
Expand Down