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

Make the controller and repository stack async #28

Merged
merged 3 commits into from
Jan 9, 2017
Merged

Make the controller and repository stack async #28

merged 3 commits into from
Jan 9, 2017

Conversation

joelverhagen
Copy link
Member

To facilitate async IO operations at the bottom of the stack, I have changed IServerPackageRepository, IServerPackageStore, and NuGetODataController to be async.

This will ease future improvements such as switching from NuGet.Core (sync APIs) to NuGet 3.x/4.x (a lot of async APIs).

/cc @dtivel @emgarten @maartenba

Copy link
Contributor

@maartenba maartenba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit on method naming, LGTM

{
return (Get(options)).FormattedAsCountResult<ODataPackage>();
return (await Get(options, token)).FormattedAsCountResult<ODataPackage>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await GetAsync

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this, but I wanted to leave the controller actions with their existing names. There is mapping logic around the $count methods that would make changing these action names a little nasty. So I thought the cost outweighed the benefit.

{
try
// Immediatelly defer work to the background thread.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediately

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good cathc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fiixed.


return true;
private Task<ServerPackage> CreateServerPackageAsync(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This private method can be synchronous.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually used this method as a forcing factor to make all callers async. I picked this location since it is most likely to invoke async methods in the future 😉. However, with the Task.Yield() in TryAddServerPackageAsync leaves the public API the same no matter what is done in this private method. So, sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -362,61 +373,73 @@ protected virtual void Dispose(bool disposing)
_serverPackageCache.PersistIfDirty();
}

private void RebuildPackageStore()
private async void RebuildPackageStoreAsync(CancellationToken token)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an exception occurs in this handler, the process will terminate. It seems like we should have some recovery and/or logging here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I didn't know fire-and-forget tasks can do this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please verify the runtime behavior. I may be wrong.

Copy link
Member Author

@joelverhagen joelverhagen Jan 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just did. You're totally right. IMO this is a bit unintuitive!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

_fileSystemWatcher.Dispose();
_fileSystemWatcher = null;

_logger.Log(LogLevel.Verbose, "Destroyed FileSystemWatcher - no longer monitoring {0}.", Source);
}
}

private void FileSystemChanged(object sender, FileSystemEventArgs e)
private async void FileSystemChangedAsync(object sender, FileSystemEventArgs e)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An exception from this method would terminate the process.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

{
_logger.Log(LogLevel.Error, "Error while reading packages from disk: {0} {1}", ex.Message, ex.StackTrace);
_logger.Log(LogLevel.Info, "Finished reading packages from disk.");
;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

return handle;
}

private class DisposableSemphoreSlim : IDisposable
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semphore -> Semaphore

SemaphoreSlim already implements IDisposable, which makes this name a little nonsensical.

How about renaming to Lock?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fine. The intend is that Dispose releases the semaphore instead of what Semaphore.Dispose does.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

_semaphore.Release();
_lockTaken = false;
}
}
}

private class SupressedFileSystemWatcher : IDisposable
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supressed -> Suppressed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

/// <summary>
/// ReadPackagesFromDisk loads all packages from disk and determines additional metadata such as the hash, IsAbsoluteLatestVersion, and IsLatestVersion.
/// ReadPackagesFromDisk loads all packages from disk and determines additional metadata such as the hash,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ReadPackagesFromDisk loads" -> "Loads"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


foreach (var packageFile in _fileSystem.GetFiles(_fileSystem.Root, "*.nupkg", false))
foreach (var packageFile in _fileSystem.GetFiles(_fileSystem.Root, "*.nupkg", false))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add named parameter for false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@joelverhagen joelverhagen merged commit fe567d4 into NuGet:dev Jan 9, 2017
@joelverhagen joelverhagen deleted the async branch January 9, 2017 17:17
private ServerPackage CreateServerPackage(
IPackage package,
bool enableDelisting,
CancellationToken token)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The token parameter can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll fix this in a subsequent PR (will have one soon for SemVer 2.0.0 which is in master but not dev).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants