Skip to content

Commit

Permalink
Makes PathResolver for docker thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 18, 2024
1 parent 6f02db6 commit f6deffe
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions CSharpInteractive.HostApi/DockerRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// ReSharper disable UnusedType.Global
namespace HostApi;

using System.Collections.Concurrent;
using Internal;
using Internal.Cmd;
using Internal.Docker;
Expand Down Expand Up @@ -116,7 +117,7 @@ public DockerRun(ICommandLine commandLine, string image)
public IStartInfo GetStartInfo(IHost host)
{
if (host == null) throw new ArgumentNullException(nameof(host));
var directoryMap = new Dictionary<string, string>();
var directoryMap = new ConcurrentDictionary<string, string>();
var pathResolver = new PathResolver(Platform, directoryMap);
using var pathResolverToken = host.GetService<HostComponents>().PathResolverContext.Register(pathResolver);
var settings = host.GetService<HostComponents>().DockerSettings;
Expand Down Expand Up @@ -167,22 +168,16 @@ public override string ToString() =>
? $"{CommandLine} in the docker container {Image}"
: ShortName;

private class PathResolver(string platform, IDictionary<string, string> directoryMap) : IPathResolver
private class PathResolver(string platform, ConcurrentDictionary<string, string> directoryMap) : IPathResolver
{
public string Resolve(IHost host, string path, IPathResolver nextResolver)
{
path = Path.GetFullPath(path);
if (!directoryMap.TryGetValue(path, out var toPath))
public string Resolve(IHost host, string path, IPathResolver nextResolver) =>
directoryMap.GetOrAdd(Path.GetFullPath(path), _ =>
{
var isWindows = IsWindows();
var rootDirectory = isWindows ? "c:" : string.Empty;
toPath = $"{rootDirectory}/.{Guid.NewGuid().ToString().Substring(0, 8)}";
directoryMap.Add(path, toPath);
}
var rootDirectory = IsWindows() ? "c:" : string.Empty;
return $"{rootDirectory}/.{Guid.NewGuid().ToString().Substring(0, 8)}";
});

return toPath;
}

public string ToAbsolutPath(string path)
{
if (IsWindows())
Expand Down

0 comments on commit f6deffe

Please sign in to comment.