OneDrive / onedrive-sdk-csharp

OneDrive SDK for C#! https://dev.onedrive.com
Other
294 stars 143 forks source link

Is OneDriveClient concurrency-safe? #163

Closed tipa closed 8 years ago

tipa commented 8 years ago

Is the OneDriveClient safe to use for up-/downloading multiple objects at once (via multiple tasks) like in the example below? And is there any best-practice for up-/downloadng lots of files (at once/asap)?

List<Task> tasks = new List<Task>();
foreach (var itemPath in items)
{
  tasks.Add(Task.Run(async () =>
    {
        using (var itemStream = await oneDriveClient.Drive.Special.AppRoot.ItemWithPath(itemPath)
           .Content.Request().GetAsync())
        {   ...    }
    }));
}
Task.WaitAll(tasks.ToArray());

[slight edits by @cmayer for readability]

cdmayer commented 8 years ago

Your example is good. Generally speaking, the default HttpProvider included in Graph.Core is safe for multiple requests at once. You can inspect the source here. Let me know if you run into any specific issues.

cdmayer commented 8 years ago

One other comment, when you construct the OneDriveClient object, be sure to explicitly pass in an instance of HttpProvider. When sending many requests, this will skip creating and disposing many instances of the class needlessly.

tipa commented 8 years ago

Thanks a lot for the answer! Could you please point me to the source code where there is the HttpProvider created repeatedly if none is passed in to the OneDriveClient? I searched the source but wasn't successful finding it.

cdmayer commented 8 years ago

Ah, I was mistaken. In BaseClient the HttpProvider is created in the default constructor. I had a different part of the code in mind (authentication). Sorry for the mistake!

TL;DR: It should be safe to pass null to the OneDriveClient constructor if you want the default HttpProvider implementation.