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
deee0bc7
Commit
deee0bc7
authored
Aug 14, 2017
by
yangxiaodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add rabbitmq connection pool.
parent
b298c943
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
105 additions
and
0 deletions
+105
-0
ConnectionPool.cs
src/DotNetCore.CAP.RabbitMQ/ConnectionPool.cs
+91
-0
IConnectionPool.cs
src/DotNetCore.CAP.RabbitMQ/IConnectionPool.cs
+14
-0
No files found.
src/DotNetCore.CAP.RabbitMQ/ConnectionPool.cs
0 → 100644
View file @
deee0bc7
using
System
;
using
System.Collections.Concurrent
;
using
System.Diagnostics
;
using
System.Threading
;
using
RabbitMQ.Client
;
namespace
DotNetCore.CAP.RabbitMQ
{
public
class
ConnectionPool
:
IConnectionPool
,
IDisposable
{
private
const
int
DefaultPoolSize
=
32
;
private
readonly
ConcurrentQueue
<
IConnection
>
_pool
=
new
ConcurrentQueue
<
IConnection
>();
private
readonly
Func
<
IConnection
>
_activator
;
private
int
_maxSize
;
private
int
_count
;
public
ConnectionPool
(
RabbitMQOptions
options
)
{
_maxSize
=
DefaultPoolSize
;
_activator
=
CreateActivator
(
options
);
}
private
static
Func
<
IConnection
>
CreateActivator
(
RabbitMQOptions
options
)
{
var
factory
=
new
ConnectionFactory
()
{
HostName
=
options
.
HostName
,
UserName
=
options
.
UserName
,
Port
=
options
.
Port
,
Password
=
options
.
Password
,
VirtualHost
=
options
.
VirtualHost
,
RequestedConnectionTimeout
=
options
.
RequestedConnectionTimeout
,
SocketReadTimeout
=
options
.
SocketReadTimeout
,
SocketWriteTimeout
=
options
.
SocketWriteTimeout
};
return
()
=>
factory
.
CreateConnection
();
}
public
virtual
IConnection
Rent
()
{
if
(
_pool
.
TryDequeue
(
out
IConnection
connection
))
{
Interlocked
.
Decrement
(
ref
_count
);
Debug
.
Assert
(
_count
>=
0
);
return
connection
;
}
connection
=
_activator
();
return
connection
;
}
public
virtual
bool
Return
(
IConnection
connection
)
{
if
(
Interlocked
.
Increment
(
ref
_count
)
<=
_maxSize
)
{
_pool
.
Enqueue
(
connection
);
return
true
;
}
Interlocked
.
Decrement
(
ref
_count
);
Debug
.
Assert
(
_maxSize
==
0
||
_pool
.
Count
<=
_maxSize
);
return
false
;
}
IConnection
IConnectionPool
.
Rent
()
=>
Rent
();
bool
IConnectionPool
.
Return
(
IConnection
connection
)
=>
Return
(
connection
);
public
void
Dispose
()
{
_maxSize
=
0
;
IConnection
context
;
while
(
_pool
.
TryDequeue
(
out
context
))
{
context
.
Dispose
();
}
}
}
}
src/DotNetCore.CAP.RabbitMQ/IConnectionPool.cs
0 → 100644
View file @
deee0bc7
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
RabbitMQ.Client
;
namespace
DotNetCore.CAP.RabbitMQ
{
public
interface
IConnectionPool
{
IConnection
Rent
();
bool
Return
(
IConnection
context
);
}
}
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