Skip to content

Commit e94b772

Browse files
committed
prevent deadlocks when processing lots of clients at once
1 parent 3b3251f commit e94b772

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

Tocsoft.GraphQLCodeGen.Cli/Program.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Linq;
88
using System.Reflection;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011
using Tocsoft.GraphQLCodeGen.RelectionHelpers;
1112

@@ -79,7 +80,6 @@ public static void MainApplication(CommandLineApplication app)
7980
// from comment headers and based on that we generate blocks of settings
8081
// the same query each setting value can be repeated and that will cause
8182
// the collection to be duplicated too.
82-
8383
var inMsbuildMode = msbuildMode.HasValue();
8484

8585
var consoleLoger = new ConsoleLogger(inMsbuildMode);
@@ -101,22 +101,34 @@ public static void MainApplication(CommandLineApplication app)
101101

102102
IEnumerable<CodeGeneratorSettings> settings = settingsLoader.GenerateSettings(loaderSettings, sourceArgument.Values);
103103
HashSet<string> generatedFiles = new HashSet<string>();
104-
104+
var sw = Stopwatch.StartNew();
105+
106+
var semaphore = new SemaphoreSlim(Environment.ProcessorCount, Environment.ProcessorCount);
105107
var tasks = settings.Select(Generate).ToList();
108+
106109
Task Generate(CodeGeneratorSettings s)
107110
{
108111
return Task.Run(async () =>
109112
{
110-
CodeGenerator generator = new CodeGenerator(consoleLoger, s);
111-
if (await generator.GenerateAsync())
113+
await semaphore.WaitAsync();
114+
try
115+
{
116+
CodeGenerator generator = new CodeGenerator(consoleLoger, s);
117+
if (await generator.GenerateAsync())
118+
{
119+
// generated code in here
120+
generatedFiles.Add(s.OutputPath);
121+
}
122+
}
123+
finally
112124
{
113-
// generated code in here
114-
generatedFiles.Add(s.OutputPath);
125+
semaphore.Release();
115126
}
116127
});
117128
}
118129

119130
await Task.WhenAll(tasks);
131+
sw.Stop();
120132

121133
if (inMsbuildMode)
122134
{

Tocsoft.GraphQLCodeGen.MsBuild/GenerateGraphQLClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class GenerateGraphQLClient : Task
3232

3333
public override bool Execute()
3434
{
35+
// Debugger.Launch();
3536
// list all source settings files
3637
// upldate this to include *.gql/*.graphql if/when i update it to support metadata in config file
3738
IEnumerable<ITaskItem> settings =

0 commit comments

Comments
 (0)