Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
7dbd7249
Commit
7dbd7249
authored
7 years ago
by
Jan Tattermusch
Browse files
Options
Downloads
Patches
Plain Diff
introduce inlineHandlers setting
parent
633434ae
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/csharp/Grpc.Core/GrpcEnvironment.cs
+21
-1
21 additions, 1 deletion
src/csharp/Grpc.Core/GrpcEnvironment.cs
src/csharp/Grpc.Core/Internal/GrpcThreadPool.cs
+12
-2
12 additions, 2 deletions
src/csharp/Grpc.Core/Internal/GrpcThreadPool.cs
with
33 additions
and
3 deletions
src/csharp/Grpc.Core/GrpcEnvironment.cs
+
21
−
1
View file @
7dbd7249
...
@@ -39,6 +39,7 @@ namespace Grpc.Core
...
@@ -39,6 +39,7 @@ namespace Grpc.Core
static
int
refCount
;
static
int
refCount
;
static
int
?
customThreadPoolSize
;
static
int
?
customThreadPoolSize
;
static
int
?
customCompletionQueueCount
;
static
int
?
customCompletionQueueCount
;
static
bool
inlineHandlers
;
static
readonly
HashSet
<
Channel
>
registeredChannels
=
new
HashSet
<
Channel
>();
static
readonly
HashSet
<
Channel
>
registeredChannels
=
new
HashSet
<
Channel
>();
static
readonly
HashSet
<
Server
>
registeredServers
=
new
HashSet
<
Server
>();
static
readonly
HashSet
<
Server
>
registeredServers
=
new
HashSet
<
Server
>();
...
@@ -217,13 +218,32 @@ namespace Grpc.Core
...
@@ -217,13 +218,32 @@ namespace Grpc.Core
}
}
}
}
/// <summary>
/// By default, gRPC's internal event handlers get offloaded to .NET default thread pool thread (<c>inlineHandlers=false</c>).
/// Setting <c>inlineHandlers</c> to <c>true</c> will allow scheduling the event handlers directly to
/// <c>GrpcThreadPool</c> internal threads. That can lead to significant performance gains in some situations,
/// but requires user to never block in async code (incorrectly written code can easily lead to deadlocks).
/// Inlining handlers 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.
/// Note: <c>inlineHandlers=true</c> was the default in gRPC C# v1.4.x and earlier.
/// </summary>
public
static
void
SetHandlerInlining
(
bool
inlineHandlers
)
{
lock
(
staticLock
)
{
GrpcPreconditions
.
CheckState
(
instance
==
null
,
"Can only be set before GrpcEnvironment is initialized"
);
GrpcEnvironment
.
inlineHandlers
=
inlineHandlers
;
}
}
/// <summary>
/// <summary>
/// Creates gRPC environment.
/// Creates gRPC environment.
/// </summary>
/// </summary>
private
GrpcEnvironment
()
private
GrpcEnvironment
()
{
{
GrpcNativeInit
();
GrpcNativeInit
();
threadPool
=
new
GrpcThreadPool
(
this
,
GetThreadPoolSizeOrDefault
(),
GetCompletionQueueCountOrDefault
());
threadPool
=
new
GrpcThreadPool
(
this
,
GetThreadPoolSizeOrDefault
(),
GetCompletionQueueCountOrDefault
()
,
inlineHandlers
);
threadPool
.
Start
();
threadPool
.
Start
();
}
}
...
...
This diff is collapsed.
Click to expand it.
src/csharp/Grpc.Core/Internal/GrpcThreadPool.cs
+
12
−
2
View file @
7dbd7249
...
@@ -41,6 +41,7 @@ namespace Grpc.Core.Internal
...
@@ -41,6 +41,7 @@ namespace Grpc.Core.Internal
readonly
List
<
Thread
>
threads
=
new
List
<
Thread
>();
readonly
List
<
Thread
>
threads
=
new
List
<
Thread
>();
readonly
int
poolSize
;
readonly
int
poolSize
;
readonly
int
completionQueueCount
;
readonly
int
completionQueueCount
;
readonly
bool
inlineHandlers
;
readonly
List
<
BasicProfiler
>
threadProfilers
=
new
List
<
BasicProfiler
>();
// profilers assigned to threadpool threads
readonly
List
<
BasicProfiler
>
threadProfilers
=
new
List
<
BasicProfiler
>();
// profilers assigned to threadpool threads
...
@@ -54,11 +55,13 @@ namespace Grpc.Core.Internal
...
@@ -54,11 +55,13 @@ namespace Grpc.Core.Internal
/// <param name="environment">Environment.</param>
/// <param name="environment">Environment.</param>
/// <param name="poolSize">Pool size.</param>
/// <param name="poolSize">Pool size.</param>
/// <param name="completionQueueCount">Completion queue count.</param>
/// <param name="completionQueueCount">Completion queue count.</param>
public
GrpcThreadPool
(
GrpcEnvironment
environment
,
int
poolSize
,
int
completionQueueCount
)
/// <param name="inlineHandlers">Handler inlining.</param>
public
GrpcThreadPool
(
GrpcEnvironment
environment
,
int
poolSize
,
int
completionQueueCount
,
bool
inlineHandlers
)
{
{
this
.
environment
=
environment
;
this
.
environment
=
environment
;
this
.
poolSize
=
poolSize
;
this
.
poolSize
=
poolSize
;
this
.
completionQueueCount
=
completionQueueCount
;
this
.
completionQueueCount
=
completionQueueCount
;
this
.
inlineHandlers
=
inlineHandlers
;
GrpcPreconditions
.
CheckArgument
(
poolSize
>=
completionQueueCount
,
GrpcPreconditions
.
CheckArgument
(
poolSize
>=
completionQueueCount
,
"Thread pool size cannot be smaller than the number of completion queues used."
);
"Thread pool size cannot be smaller than the number of completion queues used."
);
}
}
...
@@ -168,7 +171,14 @@ namespace Grpc.Core.Internal
...
@@ -168,7 +171,14 @@ namespace Grpc.Core.Internal
{
{
var
callback
=
cq
.
CompletionRegistry
.
Extract
(
tag
);
var
callback
=
cq
.
CompletionRegistry
.
Extract
(
tag
);
// Use cached delegates to avoid unnecessary allocations
// Use cached delegates to avoid unnecessary allocations
ThreadPool
.
QueueUserWorkItem
(
success
?
RunCompletionQueueEventCallbackSuccess
:
RunCompletionQueueEventCallbackFailure
,
callback
);
if
(!
inlineHandlers
)
{
ThreadPool
.
QueueUserWorkItem
(
success
?
RunCompletionQueueEventCallbackSuccess
:
RunCompletionQueueEventCallbackFailure
,
callback
);
}
else
{
RunCompletionQueueEventCallback
(
callback
,
success
);
}
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment