Skip to content

Commit

Permalink
expose experimental API to set GrpcThreadPool size.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtattermusch committed May 9, 2016
1 parent 16713b9 commit df0872f
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/csharp/Grpc.Core/GrpcEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ namespace Grpc.Core
/// </summary>
public class GrpcEnvironment
{
const int THREAD_POOL_SIZE = 4;
const int MinDefaultThreadPoolSize = 4;

static object staticLock = new object();
static GrpcEnvironment instance;
static int refCount;
static int? customThreadPoolSize;

static ILogger logger = new ConsoleLogger();

Expand Down Expand Up @@ -122,14 +123,31 @@ public static void SetLogger(ILogger customLogger)
logger = customLogger;
}

/// <summary>
/// Sets the number of threads in the gRPC thread pool that polls for internal RPC events.
/// Can be only invoke before the <c>GrpcEnviroment</c> is started and cannot be changed afterwards.
/// Setting thread pool size is an advanced setting and you should only use it if you know what you are doing.
/// Most users should rely on the default value provided by gRPC library.
/// Note: this method is part of an experimental API that can change or be removed without any prior notice.
/// </summary>
public static void SetThreadPoolSize(int threadCount)
{
lock (staticLock)
{
GrpcPreconditions.CheckState(instance == null, "Can only be set before GrpcEnvironment is initialized");
GrpcPreconditions.CheckArgument(threadCount > 0, "threadCount needs to be a positive number");
customThreadPoolSize = threadCount;
}
}

/// <summary>
/// Creates gRPC environment.
/// </summary>
private GrpcEnvironment()
{
GrpcNativeInit();
completionRegistry = new CompletionRegistry(this);
threadPool = new GrpcThreadPool(this, THREAD_POOL_SIZE);
threadPool = new GrpcThreadPool(this, GetThreadPoolSizeOrDefault());
threadPool.Start();
}

Expand Down Expand Up @@ -200,5 +218,17 @@ private void Close()

debugStats.CheckOK();
}

private int GetThreadPoolSizeOrDefault()
{
if (customThreadPoolSize.HasValue)
{
return customThreadPoolSize.Value;
}
// In systems with many cores, use half of the cores for GrpcThreadPool
// and the other half for .NET thread pool. This heuristic definitely needs
// more work, but seems to work reasonably well for a start.
return Math.Max(MinDefaultThreadPoolSize, Environment.ProcessorCount / 2);
}
}
}

0 comments on commit df0872f

Please sign in to comment.