-
Notifications
You must be signed in to change notification settings - Fork 765
Expand file tree
/
Copy pathNetMQConfig.cs
More file actions
181 lines (164 loc) · 5.65 KB
/
NetMQConfig.cs
File metadata and controls
181 lines (164 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using NetMQ.Core;
namespace NetMQ
{
/// <summary>
/// Global configuration class for NetMQ
/// </summary>
public static class NetMQConfig
{
private static TimeSpan s_linger;
private static Ctx? s_ctx;
private static int s_threadPoolSize = Ctx.DefaultIOThreads;
private static int s_maxSockets = Ctx.DefaultMaxSockets;
private static readonly object s_sync;
static NetMQConfig()
{
s_sync = new object();
s_linger = TimeSpan.Zero;
}
internal static Ctx Context
{
get
{
// Optimise for the case where the value is non-null, and we don't need to acquire the lock
var c = s_ctx;
if (c != null)
return c;
lock (s_sync)
{
// Check again whether it's null now that we have the lock
return s_ctx ??= new Ctx
{
IOThreadCount = s_threadPoolSize,
MaxSockets = s_maxSockets
};
}
}
}
/// <summary>
/// Cleanup library resources, call this method when your process is shutting-down.
/// </summary>
/// <param name="block">Set to true when you want to make sure sockets send all pending messages</param>
public static void Cleanup(bool block = true)
{
Ctx? ctx;
lock (s_sync)
{
// Capture and clear the context reference while holding the lock, then
// call Terminate outside the lock so a long-running or stuck Terminate
// (e.g. on macOS with block:false) never prevents other threads from
// acquiring s_sync and observing that cleanup has already been initiated.
ctx = s_ctx;
s_ctx = null;
}
ctx?.Terminate(block);
}
/// <summary>
/// Get or set the default linger period for the all sockets,
/// which determines how long pending messages which have yet to be sent to a peer
/// shall linger in memory after a socket is closed.
/// </summary>
/// <remarks>
/// This also affects the termination of the socket's context.
/// <para />
/// -1: Specifies infinite linger period. Pending messages shall not be discarded after the socket is closed;
/// attempting to terminate the socket's context shall block until all pending messages have been sent to a peer.
/// <para />
/// 0: The default value of <see cref="TimeSpan.Zero"/> specifies no linger period. Pending messages shall be discarded immediately when the socket is closed.
/// Positive values specify an upper bound for the linger period. Pending messages shall not be discarded after the socket is closed;
/// attempting to terminate the socket's context shall block until either all pending messages have been sent to a peer,
/// or the linger period expires, after which any pending messages shall be discarded.
/// </remarks>
public static TimeSpan Linger
{
get
{
lock (s_sync)
{
return s_linger;
}
}
set
{
lock (s_sync)
{
s_linger = value;
}
}
}
/// <summary>
/// Get or set the number of IO Threads NetMQ will create, default is 1.
/// 1 is good for most cases.
/// </summary>
public static int ThreadPoolSize
{
get
{
lock (s_sync)
return s_threadPoolSize;
}
set
{
lock (s_sync)
{
s_threadPoolSize = value;
if (s_ctx != null)
s_ctx.IOThreadCount = value;
}
}
}
/// <summary>
/// Get or set the maximum number of sockets.
/// </summary>
public static int MaxSockets
{
get
{
lock (s_sync)
return s_maxSockets;
}
set
{
lock (s_sync)
{
s_maxSockets = value;
if (s_ctx != null)
s_ctx.MaxSockets = value;
}
}
}
#region Obsolete
/// <summary>
/// Method is obsolete, call Cleanup instead
/// </summary>
[Obsolete("Use Cleanup method")]
public static void ManualTerminationTakeOver()
{
}
/// <summary>
/// Method is obsolete, call Cleanup instead
/// </summary>
[Obsolete("Use Cleanup method")]
internal static void DisableManualTermination()
{
}
/// <summary>
/// Method is obsolete, call Cleanup instead
/// </summary>
/// <param name="block">Should the context block the thread while terminating.</param>
[Obsolete("Use Cleanup method")]
public static void ContextTerminate(bool block = true)
{
}
/// <summary>
/// Method is obsolete, context created automatically
/// </summary>
[Obsolete("Context is created automatically")]
public static void ContextCreate(bool block = false)
{
Cleanup(block);
}
#endregion
}
}