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

Async cache fix in multi-thread-environment for GitHub downloads #1880

Merged
merged 5 commits into from
Aug 25, 2016
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
14 changes: 4 additions & 10 deletions src/Paket.Core/RemoteDownload.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ open Paket.Domain
open Paket.Git.CommandHelper
open Paket.Git.Handling

let private githubCache = System.Collections.Concurrent.ConcurrentDictionary<_, _>()

let private lookupDocument (auth,url : string) = async {
let key = auth,url
match githubCache.TryGetValue key with
| true, document -> return document
| _ ->
let! document = safeGetFromUrl(auth, url, null)
githubCache.TryAdd(key, document) |> ignore
return document
}
let private safeGetFromUrlCached = memoizeAsync <| safeGetFromUrl

let private lookupDocument (auth,url : string) =
safeGetFromUrlCached(auth,url,null)

let private auth key url =
key
Expand Down
5 changes: 5 additions & 0 deletions src/Paket.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ let internal memoize (f: 'a -> 'b) : 'a -> 'b =
fun (x: 'a) ->
cache.GetOrAdd(x, f)

let internal memoizeAsync f =
let cache = System.Collections.Concurrent.ConcurrentDictionary<'a, System.Threading.Tasks.Task<'b>>()
fun (x: 'a) -> // task.Result serialization to sync after done.
cache.GetOrAdd(x, fun x -> f(x) |> Async.StartAsTask) |> Async.AwaitTask

type Auth =
| Credentials of Username : string * Password : string
| Token of string
Expand Down
32 changes: 31 additions & 1 deletion tests/Paket.Tests/DependenciesFile/ParserSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,4 +1343,34 @@ let ``should read config with caches``() =

(main.Sources |> List.item 0) |> shouldEqual PackageSources.DefaultNuGetSource
(main.Sources |> List.item 1).Url |> shouldEqual "./dependencies"
(main.Sources |> List.item 2).Url |> shouldEqual "//hive/dependencies"
(main.Sources |> List.item 2).Url |> shouldEqual "//hive/dependencies"

[<Test>]
let ``async cache should work``() =
let mutable x = 0
let someSlowFunc mykey = async {
Console.WriteLine "Simulated downloading..."
do! Async.Sleep 400
Console.WriteLine "Simulated downloading Done."
x <- x + 1 // Side effect!
return "" }
let memFunc = memoizeAsync <| someSlowFunc
async {
do! memFunc "a" |> Async.Ignore
do! memFunc "a" |> Async.Ignore
do! memFunc "a" |> Async.Ignore
do! [|1 .. 30|] |> Seq.map(fun _ -> (memFunc "a"))
|> Async.Parallel |> Async.Ignore
for i = 1 to 30 do
Async.Start( memFunc "a" |> Async.Ignore )
Async.Start( memFunc "a" |> Async.Ignore )
do! Async.Sleep 500
do! memFunc "a" |> Async.Ignore
do! memFunc "a" |> Async.Ignore
for i = 1 to 30 do
Async.Start( memFunc "a" |> Async.Ignore )
do! [|1 .. 30|] |> Seq.map(fun _ -> (memFunc "a"))
|> Async.Parallel |> Async.Ignore
} |> Async.RunSynchronously
x |> shouldEqual 1