@@ -24,6 +24,7 @@ public class GarnetServerTcp : GarnetServerBase, IServerHook
24
24
readonly int networkSendThrottleMax ;
25
25
readonly NetworkBufferSettings networkBufferSettings ;
26
26
readonly LimitedFixedBufferPool networkPool ;
27
+ readonly int networkConnectionLimit ;
27
28
28
29
public IPEndPoint GetEndPoint
29
30
{
@@ -69,9 +70,10 @@ public IEnumerable<IClusterSession> ActiveClusterSessions()
69
70
/// <param name="tlsOptions"></param>
70
71
/// <param name="networkSendThrottleMax"></param>
71
72
/// <param name="logger"></param>
72
- public GarnetServerTcp ( string address , int port , int networkBufferSize = default , IGarnetTlsOptions tlsOptions = null , int networkSendThrottleMax = 8 , ILogger logger = null )
73
+ public GarnetServerTcp ( string address , int port , int networkBufferSize = default , IGarnetTlsOptions tlsOptions = null , int networkSendThrottleMax = 8 , int networkConnectionLimit = - 1 , ILogger logger = null )
73
74
: base ( address , port , networkBufferSize , logger )
74
75
{
76
+ this . networkConnectionLimit = networkConnectionLimit ;
75
77
this . tlsOptions = tlsOptions ;
76
78
this . networkSendThrottleMax = networkSendThrottleMax ;
77
79
var serverBufferSize = BufferSizeUtils . ServerBufferSize ( new MaxSizeSettings ( ) ) ;
@@ -134,11 +136,11 @@ private unsafe bool HandleNewConnection(SocketAsyncEventArgs e)
134
136
string remoteEndpointName = e . AcceptSocket . RemoteEndPoint ? . ToString ( ) ;
135
137
logger ? . LogDebug ( "Accepted TCP connection from {remoteEndpoint}" , remoteEndpointName ) ;
136
138
137
-
138
139
ServerTcpNetworkHandler handler = null ;
139
140
if ( activeHandlerCount >= 0 )
140
141
{
141
- if ( Interlocked . Increment ( ref activeHandlerCount ) > 0 )
142
+ var currentActiveHandlerCount = Interlocked . Increment ( ref activeHandlerCount ) ;
143
+ if ( currentActiveHandlerCount > 0 && ( networkConnectionLimit == - 1 || currentActiveHandlerCount <= networkConnectionLimit ) )
142
144
{
143
145
try
144
146
{
@@ -157,6 +159,11 @@ private unsafe bool HandleNewConnection(SocketAsyncEventArgs e)
157
159
handler ? . Dispose ( ) ;
158
160
}
159
161
}
162
+ else
163
+ {
164
+ Interlocked . Decrement ( ref activeHandlerCount ) ;
165
+ e . AcceptSocket . Dispose ( ) ;
166
+ }
160
167
}
161
168
return true ;
162
169
}
0 commit comments