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
93805966
Commit
93805966
authored
Oct 19, 2017
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add connection pool for kafka producer.
parent
c619aff9
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
22 deletions
+119
-22
CAP.KafkaCapOptionsExtension.cs
src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs
+1
-0
ConnectionPool.cs
src/DotNetCore.CAP.Kafka/ConnectionPool.cs
+82
-0
IConnectionPool.cs
src/DotNetCore.CAP.Kafka/IConnectionPool.cs
+12
-0
PublishQueueExecutor.cs
src/DotNetCore.CAP.Kafka/PublishQueueExecutor.cs
+24
-22
No files found.
src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs
View file @
93805966
...
@@ -25,6 +25,7 @@ namespace DotNetCore.CAP
...
@@ -25,6 +25,7 @@ namespace DotNetCore.CAP
services
.
AddSingleton
<
IConsumerClientFactory
,
KafkaConsumerClientFactory
>();
services
.
AddSingleton
<
IConsumerClientFactory
,
KafkaConsumerClientFactory
>();
services
.
AddSingleton
<
IQueueExecutor
,
PublishQueueExecutor
>();
services
.
AddSingleton
<
IQueueExecutor
,
PublishQueueExecutor
>();
services
.
AddSingleton
<
IPublishExecutor
,
PublishQueueExecutor
>();
services
.
AddSingleton
<
IPublishExecutor
,
PublishQueueExecutor
>();
services
.
AddSingleton
<
ConnectionPool
>();
}
}
}
}
}
}
\ No newline at end of file
src/DotNetCore.CAP.Kafka/ConnectionPool.cs
0 → 100644
View file @
93805966
using
System
;
using
System.Collections.Concurrent
;
using
System.Diagnostics
;
using
System.Threading
;
using
Confluent.Kafka
;
namespace
DotNetCore.CAP.Kafka
{
public
class
ConnectionPool
:
IConnectionPool
,
IDisposable
{
private
const
int
DefaultPoolSize
=
15
;
private
readonly
Func
<
Producer
>
_activator
;
private
readonly
ConcurrentQueue
<
Producer
>
_pool
=
new
ConcurrentQueue
<
Producer
>();
private
int
_count
;
private
int
_maxSize
;
public
ConnectionPool
(
KafkaOptions
options
)
{
_maxSize
=
DefaultPoolSize
;
_activator
=
CreateActivator
(
options
);
}
Producer
IConnectionPool
.
Rent
()
{
return
Rent
();
}
bool
IConnectionPool
.
Return
(
Producer
connection
)
{
return
Return
(
connection
);
}
public
void
Dispose
()
{
_maxSize
=
0
;
while
(
_pool
.
TryDequeue
(
out
var
context
))
context
.
Dispose
();
}
private
static
Func
<
Producer
>
CreateActivator
(
KafkaOptions
options
)
{
return
()
=>
new
Producer
(
options
.
AsKafkaConfig
());
}
public
virtual
Producer
Rent
()
{
if
(
_pool
.
TryDequeue
(
out
var
connection
))
{
Interlocked
.
Decrement
(
ref
_count
);
Debug
.
Assert
(
_count
>=
0
);
return
connection
;
}
connection
=
_activator
();
return
connection
;
}
public
virtual
bool
Return
(
Producer
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
;
}
}
}
\ No newline at end of file
src/DotNetCore.CAP.Kafka/IConnectionPool.cs
0 → 100644
View file @
93805966
using
Confluent.Kafka
;
namespace
DotNetCore.CAP.Kafka
{
public
interface
IConnectionPool
{
Producer
Rent
();
bool
Return
(
Producer
context
);
}
}
\ No newline at end of file
src/DotNetCore.CAP.Kafka/PublishQueueExecutor.cs
View file @
93805966
using
System
;
using
System
;
using
System.Text
;
using
System.Text
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Confluent.Kafka
;
using
DotNetCore.CAP.Processor.States
;
using
DotNetCore.CAP.Processor.States
;
using
Microsoft.Extensions.Logging
;
using
Microsoft.Extensions.Logging
;
...
@@ -9,49 +8,52 @@ namespace DotNetCore.CAP.Kafka
...
@@ -9,49 +8,52 @@ namespace DotNetCore.CAP.Kafka
{
{
internal
class
PublishQueueExecutor
:
BasePublishQueueExecutor
internal
class
PublishQueueExecutor
:
BasePublishQueueExecutor
{
{
private
readonly
KafkaOptions
_kafkaOptions
;
private
readonly
ConnectionPool
_connectionPool
;
private
readonly
ILogger
_logger
;
private
readonly
ILogger
_logger
;
public
PublishQueueExecutor
(
public
PublishQueueExecutor
(
CapOptions
options
,
CapOptions
options
,
IStateChanger
stateChanger
,
IStateChanger
stateChanger
,
KafkaOptions
kafkaOptions
,
ConnectionPool
connectionPool
,
ILogger
<
PublishQueueExecutor
>
logger
)
ILogger
<
PublishQueueExecutor
>
logger
)
:
base
(
options
,
stateChanger
,
logger
)
:
base
(
options
,
stateChanger
,
logger
)
{
{
_logger
=
logger
;
_logger
=
logger
;
_
kafkaOptions
=
kafkaOptions
;
_
connectionPool
=
connectionPool
;
}
}
public
override
Task
<
OperateResult
>
PublishAsync
(
string
keyName
,
string
content
)
public
override
async
Task
<
OperateResult
>
PublishAsync
(
string
keyName
,
string
content
)
{
{
var
producer
=
_connectionPool
.
Rent
();
try
try
{
{
var
config
=
_kafkaOptions
.
AsKafkaConfig
();
var
contentBytes
=
Encoding
.
UTF8
.
GetBytes
(
content
);
var
contentBytes
=
Encoding
.
UTF8
.
GetBytes
(
content
);
using
(
var
producer
=
new
Producer
(
config
))
{
var
message
=
await
producer
.
ProduceAsync
(
keyName
,
null
,
contentBytes
);
var
message
=
producer
.
ProduceAsync
(
keyName
,
null
,
contentBytes
).
Result
;
if
(!
message
.
Error
.
HasError
)
if
(!
message
.
Error
.
HasError
)
{
{
_logger
.
LogDebug
(
$"kafka topic message [
{
keyName
}
] has been published."
);
_logger
.
LogDebug
(
$"kafka topic message [
{
keyName
}
] has been published."
);
return
Task
.
FromResult
(
OperateResult
.
Success
)
;
return
OperateResult
.
Success
;
}
}
return
Task
.
FromResult
(
OperateResult
.
Failed
(
new
OperateError
return
OperateResult
.
Failed
(
new
OperateError
{
{
Code
=
message
.
Error
.
Code
.
ToString
(),
Code
=
message
.
Error
.
Code
.
ToString
(),
Description
=
message
.
Error
.
Reason
Description
=
message
.
Error
.
Reason
})
);
}
);
}
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
{
{
_logger
.
LogError
(
_logger
.
LogError
(
ex
,
$"An error occurred during sending the topic message to kafka. Topic:[
{
keyName
}
], Exception:
{
ex
.
Message
}
"
);
$"An error occurred during sending the topic message to kafka. Topic:[
{
keyName
}
], Exception:
{
ex
.
Message
}
"
);
return
Task
.
FromResult
(
OperateResult
.
Failed
(
ex
));
return
OperateResult
.
Failed
(
ex
);
}
finally
{
_connectionPool
.
Return
(
producer
);
}
}
}
}
}
}
...
...
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