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
Hide 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
services
.
AddSingleton
(
options
);
services
.
AddSingleton
<
IConsumerClientFactory
,
RabbitMQConsumerClientFactory
>();
services
.
AddSingleton
<
Connection
Pool
>();
services
.
AddSingleton
<
IConnectionChannelPool
,
ConnectionChannel
Pool
>();
services
.
AddSingleton
<
IQueueExecutor
,
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 @@
using
System.Collections.Concurrent
;
using
System.Diagnostics
;
using
System.Threading
;
using
Microsoft.Extensions.Logging
;
using
RabbitMQ.Client
;
namespace
DotNetCore.CAP.RabbitMQ
{
public
class
Connection
Pool
:
IConnection
Pool
,
IDisposable
public
class
Connection
ChannelPool
:
IConnectionChannel
Pool
,
IDisposable
{
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
_maxSize
;
public
ConnectionPool
(
RabbitMQOptions
options
)
public
ConnectionChannelPool
(
ILogger
<
ConnectionChannelPool
>
logger
,
RabbitMQOptions
options
)
{
_logger
=
logger
;
_maxSize
=
DefaultPoolSize
;
_
activator
=
CreateActivator
(
options
);
_
connectionActivator
=
CreateConnection
(
options
);
}
I
Connection
IConnection
Pool
.
Rent
()
I
Model
IConnectionChannel
Pool
.
Rent
()
{
return
Rent
();
}
bool
IConnection
Pool
.
Return
(
IConnection
connection
)
bool
IConnection
ChannelPool
.
Return
(
IModel
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
()
{
_maxSize
=
0
;
...
...
@@ -42,7 +54,7 @@ namespace DotNetCore.CAP.RabbitMQ
context
.
Dispose
();
}
private
static
Func
<
IConnection
>
Create
Activator
(
RabbitMQOptions
options
)
private
static
Func
<
IConnection
>
Create
Connection
(
RabbitMQOptions
options
)
{
var
factory
=
new
ConnectionFactory
{
...
...
@@ -59,23 +71,28 @@ namespace DotNetCore.CAP.RabbitMQ
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
);
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
)
{
...
...
src/DotNetCore.CAP.RabbitMQ/IConnectionPool.cs
→
src/DotNetCore.CAP.RabbitMQ/IConnection
Channel
Pool.cs
View file @
83aaab26
...
...
@@ -2,10 +2,12 @@
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,41 +9,34 @@ namespace DotNetCore.CAP.RabbitMQ
{
internal
sealed
class
PublishQueueExecutor
:
BasePublishQueueExecutor
{
private
readonly
ConnectionPool
_connection
Pool
;
private
readonly
IConnectionChannelPool
_connectionChannel
Pool
;
private
readonly
ILogger
_logger
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
public
PublishQueueExecutor
(
CapOptions
options
,
IStateChanger
stateChanger
,
ConnectionPool
connectionPool
,
RabbitMQOptions
rabbitMQOptions
,
ILogger
<
PublishQueueExecutor
>
logger
)
public
PublishQueueExecutor
(
ILogger
<
PublishQueueExecutor
>
logger
,
CapOptions
options
,
RabbitMQOptions
rabbitMQOptions
,
IConnectionChannelPool
connectionChannelPool
,
IStateChanger
stateChanger
)
:
base
(
options
,
stateChanger
,
logger
)
{
_logger
=
logger
;
_connection
Pool
=
connection
Pool
;
_connection
ChannelPool
=
connectionChannel
Pool
;
_rabbitMQOptions
=
rabbitMQOptions
;
}
public
override
Task
<
OperateResult
>
PublishAsync
(
string
keyName
,
string
content
)
{
var
connection
=
_connectionPool
.
Rent
();
var
channel
=
_connectionChannelPool
.
Rent
();
try
{
using
(
var
channel
=
connection
.
CreateModel
())
{
var
body
=
Encoding
.
UTF8
.
GetBytes
(
content
);
var
body
=
Encoding
.
UTF8
.
GetBytes
(
content
);
channel
.
ExchangeDeclare
(
_rabbitMQOptions
.
TopicExchangeName
,
RabbitMQOptions
.
ExchangeType
,
true
);
channel
.
BasicPublish
(
_rabbitMQOptions
.
TopicExchangeName
,
keyName
,
null
,
body
);
channel
.
ExchangeDeclare
(
_rabbitMQOptions
.
TopicExchangeName
,
RabbitMQOptions
.
ExchangeType
,
true
);
channel
.
BasicPublish
(
_rabbitMQOptions
.
TopicExchangeName
,
keyName
,
null
,
body
);
_logger
.
LogDebug
(
$"RabbitMQ topic message [
{
keyName
}
] has been published."
);
_logger
.
LogDebug
(
$"RabbitMQ topic message [
{
keyName
}
] has been published."
);
}
return
Task
.
FromResult
(
OperateResult
.
Success
);
}
catch
(
Exception
ex
)
...
...
@@ -60,7 +53,7 @@ namespace DotNetCore.CAP.RabbitMQ
}
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
{
internal
sealed
class
RabbitMQConsumerClient
:
IConsumerClient
{
private
readonly
ConnectionPool
_connection
Pool
;
private
readonly
IConnectionChannelPool
_connectionChannel
Pool
;
private
readonly
string
_exchageName
;
private
readonly
string
_queueName
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
...
...
@@ -19,11 +19,11 @@ namespace DotNetCore.CAP.RabbitMQ
private
ulong
_deliveryTag
;
public
RabbitMQConsumerClient
(
string
queueName
,
ConnectionPool
connection
Pool
,
IConnectionChannelPool
connectionChannel
Pool
,
RabbitMQOptions
options
)
{
_queueName
=
queueName
;
_connection
Pool
=
connection
Pool
;
_connection
ChannelPool
=
connectionChannel
Pool
;
_rabbitMQOptions
=
options
;
_exchageName
=
options
.
TopicExchangeName
;
...
...
@@ -69,7 +69,7 @@ namespace DotNetCore.CAP.RabbitMQ
private
void
InitClient
()
{
var
connection
=
_connection
Pool
.
Rent
();
var
connection
=
_connection
ChannelPool
.
GetConnection
();
_channel
=
connection
.
CreateModel
();
...
...
@@ -82,8 +82,6 @@ namespace DotNetCore.CAP.RabbitMQ
{
"x-message-ttl"
,
_rabbitMQOptions
.
QueueMessageExpires
}
};
_channel
.
QueueDeclare
(
_queueName
,
true
,
false
,
false
,
arguments
);
_connectionPool
.
Return
(
connection
);
}
private
void
OnConsumerReceived
(
object
sender
,
BasicDeliverEventArgs
e
)
...
...
src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs
View file @
83aaab26
...
...
@@ -2,19 +2,19 @@
{
internal
sealed
class
RabbitMQConsumerClientFactory
:
IConsumerClientFactory
{
private
readonly
ConnectionPool
_connection
Pool
;
private
readonly
IConnectionChannelPool
_connectionChannel
Pool
;
private
readonly
RabbitMQOptions
_rabbitMQOptions
;
public
RabbitMQConsumerClientFactory
(
RabbitMQOptions
rabbitMQOptions
,
ConnectionPool
p
ool
)
public
RabbitMQConsumerClientFactory
(
RabbitMQOptions
rabbitMQOptions
,
IConnectionChannelPool
channelP
ool
)
{
_rabbitMQOptions
=
rabbitMQOptions
;
_connection
Pool
=
p
ool
;
_connection
ChannelPool
=
channelP
ool
;
}
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