Skip to content

Commit 7743488

Browse files
committed
(#122) Dead link remover: use Polly for more robust HTTP requests
1 parent 3a08ed2 commit 7743488

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

check-dead-links.fsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#r "nuget: Markdig, 0.41.0"
2+
#r "nuget: Polly.Core, 8.5.2"
23

34
open System
45
open System.Net
56
open System.Net.Http
7+
open System.Threading.Tasks
68
open Markdig
79
open System.IO
810
open Markdig.Syntax
911
open Markdig.Syntax.Inlines
12+
open Polly
13+
open Polly.Retry
14+
open Polly.Timeout
1015

1116
let rec visitRecursively (action: MarkdownObject -> unit) (node: MarkdownObject) =
1217
match node with
@@ -33,10 +38,16 @@ let collectLinks node =
3338
urls
3439

3540
let printLock = Object()
36-
let checkLinkStatus (client: HttpClient) (url: string) = async {
41+
let retryPipeline =
42+
ResiliencePipelineBuilder()
43+
.AddRetry(RetryStrategyOptions())
44+
.Build()
45+
let checkLinkStatus (client: HttpClient) (url: string) = task {
3746
try
3847
lock printLock (fun () -> printfn $"Verifying link {url}…")
39-
let! response = Async.AwaitTask <| client.GetAsync url
48+
let! response = retryPipeline.ExecuteAsync(
49+
fun _ -> ValueTask<HttpResponseMessage>(client.GetAsync url)
50+
)
4051
if response.StatusCode <> HttpStatusCode.OK then
4152
return Result.Error $"Status code {int response.StatusCode}."
4253
else
@@ -64,7 +75,7 @@ let links = collectLinks document |> Seq.filter isNonLocalLink
6475
let results = Async.RunSynchronously(async {
6576
use client = new HttpClient()
6677
return! links |> Seq.map(fun url -> async {
67-
let! status = checkLinkStatus client url
78+
let! status = Async.AwaitTask <| checkLinkStatus client url
6879
return url, status
6980
}) |> Async.Parallel
7081
})

0 commit comments

Comments
 (0)