Unverified Commit 76c4462d authored by Andreas Caravella's avatar Andreas Caravella Committed by GitHub

Make SocketManager worker count configurable (#1115)

* add overload exposing workerCount and move CTOR's together

* update XML comment to align with implementation

* update according to PR feedback
parent fbe89e2a
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
...@@ -350,8 +350,8 @@ public bool PreserveAsyncOrder ...@@ -350,8 +350,8 @@ public bool PreserveAsyncOrder
public string ServiceName { get; set; } public string ServiceName { get; set; }
/// <summary> /// <summary>
/// Gets or sets the SocketManager instance to be used with these options; if this is null a per-multiplexer /// Gets or sets the SocketManager instance to be used with these options; if this is null a shared cross-multiplexer SocketManager
/// SocketManager is created automatically. /// is used
/// </summary> /// </summary>
public SocketManager SocketManager { get; set; } public SocketManager SocketManager { get; set; }
......
...@@ -20,43 +20,11 @@ public sealed partial class SocketManager : IDisposable ...@@ -20,43 +20,11 @@ public sealed partial class SocketManager : IDisposable
public string Name { get; } public string Name { get; }
/// <summary> /// <summary>
/// Creates a new (optionally named) <see cref="SocketManager"/> instance /// Creates a new <see cref="SocketManager"/> instance
/// </summary> /// </summary>
/// <param name="name">The name for this <see cref="SocketManager"/>.</param> /// <param name="name">The name for this <see cref="SocketManager"/>.</param>
public SocketManager(string name = null) public SocketManager(string name)
: this(name, false, DEFAULT_WORKERS) { } : this(name, DEFAULT_WORKERS, false) { }
/// <summary>
/// Default / shared socket manager
/// </summary>
public static SocketManager Shared
{
get
{
var shared = _shared;
if (shared != null) return _shared;
try
{
// note: we'll allow a higher max thread count on the shared one
shared = new SocketManager("DefaultSocketManager", false, DEFAULT_WORKERS * 2);
if (Interlocked.CompareExchange(ref _shared, shared, null) == null)
shared = null;
}
finally { shared?.Dispose(); }
return Volatile.Read(ref _shared);
}
}
/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString()
{
var scheduler = SchedulerPool;
return $"scheduler - queue: {scheduler?.TotalServicedByQueue}, pool: {scheduler?.TotalServicedByPool}";
}
private static SocketManager _shared;
/// <summary> /// <summary>
/// Creates a new <see cref="SocketManager"/> instance /// Creates a new <see cref="SocketManager"/> instance
...@@ -64,13 +32,18 @@ public override string ToString() ...@@ -64,13 +32,18 @@ public override string ToString()
/// <param name="name">The name for this <see cref="SocketManager"/>.</param> /// <param name="name">The name for this <see cref="SocketManager"/>.</param>
/// <param name="useHighPrioritySocketThreads">Whether this <see cref="SocketManager"/> should use high priority sockets.</param> /// <param name="useHighPrioritySocketThreads">Whether this <see cref="SocketManager"/> should use high priority sockets.</param>
public SocketManager(string name, bool useHighPrioritySocketThreads) public SocketManager(string name, bool useHighPrioritySocketThreads)
: this(name, useHighPrioritySocketThreads, DEFAULT_WORKERS) { } : this(name, DEFAULT_WORKERS, useHighPrioritySocketThreads) { }
private const int DEFAULT_WORKERS = 5, MINIMUM_SEGMENT_SIZE = 8 * 1024; /// <summary>
/// Creates a new (optionally named) <see cref="SocketManager"/> instance
private SocketManager(string name, bool useHighPrioritySocketThreads, int workerCount) /// </summary>
/// <param name="name">The name for this <see cref="SocketManager"/>.</param>
/// <param name="workerCount">the number of dedicated workers for this <see cref="SocketManager"/>.</param>
/// <param name="useHighPrioritySocketThreads">Whether this <see cref="SocketManager"/> should use high priority sockets.</param>
public SocketManager(string name = null, int workerCount = 0, bool useHighPrioritySocketThreads = false)
{ {
if (string.IsNullOrWhiteSpace(name)) name = GetType().Name; if (string.IsNullOrWhiteSpace(name)) name = GetType().Name;
if (workerCount <= 0) workerCount = DEFAULT_WORKERS;
Name = name; Name = name;
const long Receive_PauseWriterThreshold = 4L * 1024 * 1024 * 1024; // receive: let's give it up to 4GiB of buffer for now const long Receive_PauseWriterThreshold = 4L * 1024 * 1024 * 1024; // receive: let's give it up to 4GiB of buffer for now
...@@ -106,6 +79,40 @@ private SocketManager(string name, bool useHighPrioritySocketThreads, int worker ...@@ -106,6 +79,40 @@ private SocketManager(string name, bool useHighPrioritySocketThreads, int worker
useSynchronizationContext: false); useSynchronizationContext: false);
} }
/// <summary>
/// Default / shared socket manager
/// </summary>
public static SocketManager Shared
{
get
{
var shared = _shared;
if (shared != null) return _shared;
try
{
// note: we'll allow a higher max thread count on the shared one
shared = new SocketManager("DefaultSocketManager", DEFAULT_WORKERS * 2, false);
if (Interlocked.CompareExchange(ref _shared, shared, null) == null)
shared = null;
}
finally { shared?.Dispose(); }
return Volatile.Read(ref _shared);
}
}
/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString()
{
var scheduler = SchedulerPool;
return $"scheduler - queue: {scheduler?.TotalServicedByQueue}, pool: {scheduler?.TotalServicedByPool}";
}
private static SocketManager _shared;
private const int DEFAULT_WORKERS = 5, MINIMUM_SEGMENT_SIZE = 8 * 1024;
private DedicatedThreadPoolPipeScheduler _schedulerPool; private DedicatedThreadPoolPipeScheduler _schedulerPool;
internal readonly PipeOptions SendPipeOptions, ReceivePipeOptions; internal readonly PipeOptions SendPipeOptions, ReceivePipeOptions;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment