-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improving disconnection Information report
- Loading branch information
DanielePalaia
authored and
Daniele Palaia
committed
Oct 11, 2023
1 parent
11da180
commit 78f2b22
Showing
11 changed files
with
185 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 12 additions & 3 deletions
15
docs/examples/check_connection_broken/producer_handle_connections_issues.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
docs/examples/check_connection_broken/superstream_consumer_handle_connections_issues.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import asyncio | ||
import signal | ||
|
||
from rstream import ( | ||
AMQPMessage, | ||
ConsumerOffsetSpecification, | ||
DisconnectionErrorInfo, | ||
MessageContext, | ||
OffsetType, | ||
SuperStreamConsumer, | ||
amqp_decoder, | ||
) | ||
|
||
cont = 0 | ||
|
||
|
||
async def on_message(msg: AMQPMessage, message_context: MessageContext): | ||
stream = await message_context.consumer.stream(message_context.subscriber_name) | ||
offset = message_context.offset | ||
print("Received message: {} from stream: {} - message offset: {}".format(msg, stream, offset)) | ||
|
||
|
||
async def on_connection_closed(disconnection_info: DisconnectionErrorInfo) -> None: | ||
print( | ||
"connection has been closed from stream: " | ||
+ str(disconnection_info.streams) | ||
+ " for reason: " | ||
+ str(disconnection_info.reason) | ||
) | ||
|
||
|
||
async def consume(): | ||
consumer = SuperStreamConsumer( | ||
host="localhost", | ||
port=5552, | ||
vhost="/", | ||
username="guest", | ||
password="guest", | ||
super_stream="test_super_stream", | ||
connection_closed_handler=on_connection_closed, | ||
) | ||
|
||
loop = asyncio.get_event_loop() | ||
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.create_task(consumer.close())) | ||
offset_specification = ConsumerOffsetSpecification(OffsetType.FIRST, None) | ||
await consumer.start() | ||
await consumer.subscribe( | ||
callback=on_message, decoder=amqp_decoder, offset_specification=offset_specification | ||
) | ||
await consumer.run() | ||
|
||
|
||
asyncio.run(consume()) |
54 changes: 54 additions & 0 deletions
54
docs/examples/check_connection_broken/superstream_producer_handle_connections_issues.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
import time | ||
|
||
from rstream import ( | ||
AMQPMessage, | ||
DisconnectionErrorInfo, | ||
RouteType, | ||
SuperStreamProducer, | ||
) | ||
|
||
SUPER_STREAM = "test_super_stream" | ||
MESSAGES = 10000000 | ||
|
||
|
||
async def publish(): | ||
# this value will be hashed using mumh3 hashing algorithm to decide the partition resolution for the message | ||
async def routing_extractor(message: AMQPMessage) -> str: | ||
return message.application_properties["id"] | ||
|
||
async def on_connection_closed(disconnection_info: DisconnectionErrorInfo) -> None: | ||
|
||
print( | ||
"connection has been closed from stream: " | ||
+ str(disconnection_info.streams) | ||
+ " for reason: " | ||
+ str(disconnection_info.reason) | ||
) | ||
|
||
async with SuperStreamProducer( | ||
"localhost", | ||
username="guest", | ||
password="guest", | ||
routing_extractor=routing_extractor, | ||
routing=RouteType.Hash, | ||
connection_closed_handler=on_connection_closed, | ||
super_stream=SUPER_STREAM, | ||
) as super_stream_producer: | ||
|
||
# sending a million of messages in AMQP format | ||
start_time = time.perf_counter() | ||
|
||
for i in range(MESSAGES): | ||
amqp_message = AMQPMessage( | ||
body="hello: {}".format(i), | ||
application_properties={"id": "{}".format(i)}, | ||
) | ||
# send is asynchronous | ||
await super_stream_producer.send(message=amqp_message) | ||
|
||
end_time = time.perf_counter() | ||
print(f"Sent {MESSAGES} messages in {end_time - start_time:0.4f} seconds") | ||
|
||
|
||
asyncio.run(publish()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.