Skip to content

Commit c0ceba9

Browse files
committed
🎉 Fixup Timeout Feature. Should bail on first timeout encountered. No fancy error handling.
1 parent 82bf7ec commit c0ceba9

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

ModuleFast.psm1

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ function Install-ModuleFast {
247247
#This will output the resulting modules that were installed.
248248
[Switch]$PassThru,
249249
#Setting this to "CurrentUser" is the same as specifying the destination as 'Current'. This is a usability convenience.
250-
[InstallScope]$Scope
250+
[InstallScope]$Scope,
251+
#The timeout for HTTP requests. This is set to 30 seconds by default. This is generally sufficient for most requests, but you may need to increase this if you are on a slow connection or are downloading large modules.
252+
[int]$Timeout = 30
251253
)
252254
begin {
253255
trap {$PSCmdlet.ThrowTerminatingError($PSItem)}
@@ -305,14 +307,15 @@ function Install-ModuleFast {
305307
#We want to maintain a single HttpClient for the life of the module. This isn't as big of a deal as it used to be but
306308
#it is still a best practice.
307309
if (-not $SCRIPT:__ModuleFastHttpClient -or $Source -ne $SCRIPT:__ModuleFastHttpClient.BaseAddress) {
308-
$SCRIPT:__ModuleFastHttpClient = New-ModuleFastClient -Credential $Credential
310+
$SCRIPT:__ModuleFastHttpClient = New-ModuleFastClient -Credential $Credential -Timeout $Timeout
309311
if (-not $SCRIPT:__ModuleFastHttpClient) {
310312
throw 'Failed to create ModuleFast HTTPClient. This is a bug'
311313
}
312314
}
313315
$httpClient = $SCRIPT:__ModuleFastHttpClient
314316

315317
$cancelSource = [CancellationTokenSource]::new()
318+
$cancelSource.CancelAfter([TimeSpan]::FromSeconds($Timeout))
316319

317320
[HashSet[ModuleFastSpec]]$ModulesToInstall = @()
318321
[List[ModuleFastInfo]]$installPlan = @()
@@ -379,7 +382,7 @@ function Install-ModuleFast {
379382
$ModulesToInstall.ToArray()
380383
} else {
381384
Write-Progress -Id 1 -Activity 'Install-ModuleFast' -Status 'Plan' -PercentComplete 1
382-
Get-ModuleFastPlan -Specification $ModulesToInstall -HttpClient $httpClient -Source $Source -Update:$Update -PreRelease:$Prerelease.IsPresent -DestinationOnly:$DestinationOnly -Destination $Destination
385+
Get-ModuleFastPlan -Specification $ModulesToInstall -HttpClient $httpClient -Source $Source -Update:$Update -PreRelease:$Prerelease.IsPresent -DestinationOnly:$DestinationOnly -Destination $Destination -Timeout $Timeout
383386
}
384387
}
385388

@@ -441,7 +444,8 @@ function Install-ModuleFast {
441444

442445
function New-ModuleFastClient {
443446
param(
444-
[PSCredential]$Credential
447+
[PSCredential]$Credential,
448+
[int]$Timeout = 30
445449
)
446450
Write-Debug 'Creating new ModuleFast HTTP Client. This should only happen once!'
447451
$ErrorActionPreference = 'Stop'
@@ -498,7 +502,8 @@ function Get-ModuleFastPlan {
498502
#By default we use in-place modules if they satisfy the version requirements. This switch will force a search for all latest modules
499503
[Switch]$Update,
500504
[PSCredential]$Credential,
501-
[HttpClient]$HttpClient = $(New-ModuleFastClient -Credential $Credential),
505+
[int]$Timeout = 30,
506+
[HttpClient]$HttpClient = $(New-ModuleFastClient -Credential $Credential -Timeout $Timeout),
502507
[int]$ParentProgress,
503508
[string]$Destination,
504509
[switch]$DestinationOnly,

0 commit comments

Comments
 (0)