Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
83aaab26
Commit
83aaab26
authored
Nov 08, 2017
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimize the RabbitMQ connection pool
parent
52dd95ff
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
69 additions
and
48 deletions
+69
-48
CAP.RabbitMQCapOptionsExtension.cs
...otNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs
+1
-1
ConnectionChannelPool.cs
src/DotNetCore.CAP.RabbitMQ/ConnectionChannelPool.cs
+33
-16
IConnectionChannelPool.cs
src/DotNetCore.CAP.RabbitMQ/IConnectionChannelPool.cs
+13
-0
PublishQueueExecutor.cs
src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs
+14
-21
RabbitMQConsumerClient.cs
src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs
+4
-6
RabbitMQConsumerClientFactory.cs
src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs
+4
-4
No files found.
src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs
View file @
83aaab26
...
@@ -23,7 +23,7 @@ namespace DotNetCore.CAP
...
@@ -23,7 +23,7 @@ namespace DotNetCore.CAP
services
.
AddSingleton
(
options
);
services
.
AddSingleton
(
options
);
services
.
AddSingleton
<
IConsumerClientFactory
,
RabbitMQConsumerClientFactory
>();
services
.
AddSingleton
<
IConsumerClientFactory
,
RabbitMQConsumerClientFactory
>();
services
.
AddSingleton
<
Connection
Pool
>();
services
.
AddSingleton
<
IConnectionChannelPool
,
ConnectionChannel
Pool
>();
services
.
AddSingleton
<
IQueueExecutor
,
PublishQueueExecutor
>();
services
.
AddSingleton
<
IQueueExecutor
,
PublishQueueExecutor
>();
services
.
AddSingleton
<
IPublishExecutor
,
PublishQueueExecutor
>();
services
.
AddSingleton
<
IPublishExecutor
,
PublishQueueExecutor
>();
}
}
...
...
src/DotNetCore.CAP.RabbitMQ/ConnectionPool.cs
→
src/DotNetCore.CAP.RabbitMQ/Connection
Channel
Pool.cs
View file @
83aaab26
...
@@ -2,38 +2,50 @@
...
@@ -2,38 +2,50 @@
using
System.Collections.Concurrent
;
using
System.Collections.Concurrent
;
using
System.Diagnostics
;
using
System.Diagnostics
;
using
System.Threading
;
using
System.Threading
;
using
Microsoft.Extensions.Logging
;
using
RabbitMQ.Client
;
using
RabbitMQ.Client
;
namespace
DotNetCore.CAP.RabbitMQ
namespace
DotNetCore.CAP.RabbitMQ
{
{
public
class
Connection
Pool
:
IConnection
Pool
,
IDisposable
public
class
Connection
ChannelPool
:
IConnectionChannel
Pool
,
IDisposable
{
{
private
const
int
DefaultPoolSize
=
15
;
private
const
int
DefaultPoolSize
=
15
;
private
readonly
Func
<
IConnection
>
_connectionActivator
;
private
readonly
ILogger
<
ConnectionChannelPool
>
_logger
;
private
readonly
ConcurrentQueue
<
IModel
>
_pool
=
new
ConcurrentQueue
<
IModel
>();
private
IConnection
_connection
;
private
readonly
Func
<
IConnection
>
_activator
;
private
readonly
ConcurrentQueue
<
IConnection
>
_pool
=
new
ConcurrentQueue
<
IConnection
>();
private
int
_count
;
private
int
_count
;
private
int
_maxSize
;
private
int
_maxSize
;
public
ConnectionPool
(
RabbitMQOptions
options
)
public
ConnectionChannelPool
(
ILogger
<
ConnectionChannelPool
>
logger
,
RabbitMQOptions
options
)
{
{
_logger
=
logger
;
_maxSize
=
DefaultPoolSize
;
_maxSize
=
DefaultPoolSize
;
_
activator
=
CreateActivator
(
options
);
_
connectionActivator
=
CreateConnection
(
options
);
}
}
I
Connection
IConnection
Pool
.
Rent
()
I
Model
IConnectionChannel
Pool
.
Rent
()
{
{
return
Rent
();
return
Rent
();
}
}
bool
IConnection
Pool
.
Return
(
IConnection
connection
)
bool
IConnection
ChannelPool
.
Return
(
IModel
connection
)
{
{
return
Return
(
connection
);
return
Return
(
connection
);
}
}
public
IConnection
GetConnection
()
{
if
(
_connection
!=
null
&&
_connection
.
IsOpen
)
return
_connection
;
_connection
=
_connectionActivator
();
_connection
.
ConnectionShutdown
+=
RabbitMQ_ConnectionShutdown
;
return
_connection
;
}
public
void
Dispose
()
public
void
Dispose
()
{
{
_maxSize
=
0
;
_maxSize
=
0
;
...
@@ -42,7 +54,7 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -42,7 +54,7 @@ namespace DotNetCore.CAP.RabbitMQ
context
.
Dispose
();
context
.
Dispose
();
}
}
private
static
Func
<
IConnection
>
Create
Activator
(
RabbitMQOptions
options
)
private
static
Func
<
IConnection
>
Create
Connection
(
RabbitMQOptions
options
)
{
{
var
factory
=
new
ConnectionFactory
var
factory
=
new
ConnectionFactory
{
{
...
@@ -59,23 +71,28 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -59,23 +71,28 @@ namespace DotNetCore.CAP.RabbitMQ
return
()
=>
factory
.
CreateConnection
();
return
()
=>
factory
.
CreateConnection
();
}
}
public
virtual
IConnection
Rent
()
private
void
RabbitMQ_ConnectionShutdown
(
object
sender
,
ShutdownEventArgs
e
)
{
_logger
.
LogWarning
(
$"RabbitMQ client connection closed!
{
e
}
"
);
}
public
virtual
IModel
Rent
()
{
{
if
(
_pool
.
TryDequeue
(
out
var
connection
))
if
(
_pool
.
TryDequeue
(
out
var
model
))
{
{
Interlocked
.
Decrement
(
ref
_count
);
Interlocked
.
Decrement
(
ref
_count
);
Debug
.
Assert
(
_count
>=
0
);
Debug
.
Assert
(
_count
>=
0
);
return
connection
;
return
model
;
}
}
connection
=
_activator
();
model
=
GetConnection
().
CreateModel
();
return
connection
;
return
model
;
}
}
public
virtual
bool
Return
(
I
Connection
connection
)
public
virtual
bool
Return
(
I
Model
connection
)
{
{
if
(
Interlocked
.
Increment
(
ref
_count
)
<=
_maxSize
)
if
(
Interlocked
.
Increment
(
ref
_count
)
<=
_maxSize
)
{
{
...
...
src/DotNetCore.CAP.RabbitMQ/IConnectionPool.cs
→
src/DotNetCore.CAP.RabbitMQ/IConnection
Channel
Pool.cs
View file @
83aaab26
...
@@ -2,10 +2,12 @@
...
@@ -2,10 +2,12 @@
namespace
DotNetCore.CAP.RabbitMQ
namespace
DotNetCore.CAP.RabbitMQ
{
{
public
interface
IConnectionPool
public
interface
IConnection
Channel
Pool
{
{
IConnection
Rent
();
IConnection
GetConnection
();
bool
Return
(
IConnection
context
);
IModel
Rent
();
bool
Return
(
IModel
context
);
}
}
}
}
\ No newline at end of file
src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs
View file @
83aaab26
...
@@ -9,30 +9,23 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -9,30 +9,23 @@ namespace DotNetCore.CAP.RabbitMQ
{
{
internal
sealed
class
PublishQueueExecutor
:
BasePublishQueueExecutor
internal
sealed
class
PublishQueueExecutor
:
BasePublishQueueExecutor
{
{
private
readonly
ConnectionPool
_connection
Pool
;
private
readonly
IConnectionChannelPool
_connectionChannel
Pool
;
private
readonly
ILogger
_logger
;
private
readonly
ILogger
_logger
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
public
PublishQueueExecutor
(
public
PublishQueueExecutor
(
ILogger
<
PublishQueueExecutor
>
logger
,
CapOptions
options
,
CapOptions
options
,
RabbitMQOptions
rabbitMQOptions
,
IConnectionChannelPool
connectionChannelPool
,
IStateChanger
stateChanger
)
IStateChanger
stateChanger
,
ConnectionPool
connectionPool
,
RabbitMQOptions
rabbitMQOptions
,
ILogger
<
PublishQueueExecutor
>
logger
)
:
base
(
options
,
stateChanger
,
logger
)
:
base
(
options
,
stateChanger
,
logger
)
{
{
_logger
=
logger
;
_logger
=
logger
;
_connection
Pool
=
connection
Pool
;
_connection
ChannelPool
=
connectionChannel
Pool
;
_rabbitMQOptions
=
rabbitMQOptions
;
_rabbitMQOptions
=
rabbitMQOptions
;
}
}
public
override
Task
<
OperateResult
>
PublishAsync
(
string
keyName
,
string
content
)
public
override
Task
<
OperateResult
>
PublishAsync
(
string
keyName
,
string
content
)
{
{
var
connection
=
_connectionPool
.
Rent
();
var
channel
=
_connectionChannelPool
.
Rent
();
try
try
{
using
(
var
channel
=
connection
.
CreateModel
())
{
{
var
body
=
Encoding
.
UTF8
.
GetBytes
(
content
);
var
body
=
Encoding
.
UTF8
.
GetBytes
(
content
);
...
@@ -43,7 +36,7 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -43,7 +36,7 @@ namespace DotNetCore.CAP.RabbitMQ
body
);
body
);
_logger
.
LogDebug
(
$"RabbitMQ topic message [
{
keyName
}
] has been published."
);
_logger
.
LogDebug
(
$"RabbitMQ topic message [
{
keyName
}
] has been published."
);
}
return
Task
.
FromResult
(
OperateResult
.
Success
);
return
Task
.
FromResult
(
OperateResult
.
Success
);
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
...
@@ -60,7 +53,7 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -60,7 +53,7 @@ namespace DotNetCore.CAP.RabbitMQ
}
}
finally
finally
{
{
_connection
Pool
.
Return
(
connection
);
_connection
ChannelPool
.
Return
(
channel
);
}
}
}
}
}
}
...
...
src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs
View file @
83aaab26
...
@@ -10,7 +10,7 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -10,7 +10,7 @@ namespace DotNetCore.CAP.RabbitMQ
{
{
internal
sealed
class
RabbitMQConsumerClient
:
IConsumerClient
internal
sealed
class
RabbitMQConsumerClient
:
IConsumerClient
{
{
private
readonly
ConnectionPool
_connection
Pool
;
private
readonly
IConnectionChannelPool
_connectionChannel
Pool
;
private
readonly
string
_exchageName
;
private
readonly
string
_exchageName
;
private
readonly
string
_queueName
;
private
readonly
string
_queueName
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
...
@@ -19,11 +19,11 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -19,11 +19,11 @@ namespace DotNetCore.CAP.RabbitMQ
private
ulong
_deliveryTag
;
private
ulong
_deliveryTag
;
public
RabbitMQConsumerClient
(
string
queueName
,
public
RabbitMQConsumerClient
(
string
queueName
,
ConnectionPool
connection
Pool
,
IConnectionChannelPool
connectionChannel
Pool
,
RabbitMQOptions
options
)
RabbitMQOptions
options
)
{
{
_queueName
=
queueName
;
_queueName
=
queueName
;
_connection
Pool
=
connection
Pool
;
_connection
ChannelPool
=
connectionChannel
Pool
;
_rabbitMQOptions
=
options
;
_rabbitMQOptions
=
options
;
_exchageName
=
options
.
TopicExchangeName
;
_exchageName
=
options
.
TopicExchangeName
;
...
@@ -69,7 +69,7 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -69,7 +69,7 @@ namespace DotNetCore.CAP.RabbitMQ
private
void
InitClient
()
private
void
InitClient
()
{
{
var
connection
=
_connection
Pool
.
Rent
();
var
connection
=
_connection
ChannelPool
.
GetConnection
();
_channel
=
connection
.
CreateModel
();
_channel
=
connection
.
CreateModel
();
...
@@ -82,8 +82,6 @@ namespace DotNetCore.CAP.RabbitMQ
...
@@ -82,8 +82,6 @@ namespace DotNetCore.CAP.RabbitMQ
{
"x-message-ttl"
,
_rabbitMQOptions
.
QueueMessageExpires
}
{
"x-message-ttl"
,
_rabbitMQOptions
.
QueueMessageExpires
}
};
};
_channel
.
QueueDeclare
(
_queueName
,
true
,
false
,
false
,
arguments
);
_channel
.
QueueDeclare
(
_queueName
,
true
,
false
,
false
,
arguments
);
_connectionPool
.
Return
(
connection
);
}
}
private
void
OnConsumerReceived
(
object
sender
,
BasicDeliverEventArgs
e
)
private
void
OnConsumerReceived
(
object
sender
,
BasicDeliverEventArgs
e
)
...
...
src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs
View file @
83aaab26
...
@@ -2,19 +2,19 @@
...
@@ -2,19 +2,19 @@
{
{
internal
sealed
class
RabbitMQConsumerClientFactory
:
IConsumerClientFactory
internal
sealed
class
RabbitMQConsumerClientFactory
:
IConsumerClientFactory
{
{
private
readonly
ConnectionPool
_connection
Pool
;
private
readonly
IConnectionChannelPool
_connectionChannel
Pool
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
public
RabbitMQConsumerClientFactory
(
RabbitMQOptions
rabbitMQOptions
,
ConnectionPool
p
ool
)
public
RabbitMQConsumerClientFactory
(
RabbitMQOptions
rabbitMQOptions
,
IConnectionChannelPool
channelP
ool
)
{
{
_rabbitMQOptions
=
rabbitMQOptions
;
_rabbitMQOptions
=
rabbitMQOptions
;
_connection
Pool
=
p
ool
;
_connection
ChannelPool
=
channelP
ool
;
}
}
public
IConsumerClient
Create
(
string
groupId
)
public
IConsumerClient
Create
(
string
groupId
)
{
{
return
new
RabbitMQConsumerClient
(
groupId
,
_connectionPool
,
_rabbitMQOptions
);
return
new
RabbitMQConsumerClient
(
groupId
,
_connection
Channel
Pool
,
_rabbitMQOptions
);
}
}
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment