-
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.
- Loading branch information
Daniele Palaia
authored and
Daniele Palaia
committed
Oct 12, 2023
1 parent
78f2b22
commit 77e517a
Showing
5 changed files
with
152 additions
and
40 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
docs/examples/check_connection_broken/consumer_handle_connections_issues_with_close.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,50 @@ | ||
import asyncio | ||
import signal | ||
|
||
from rstream import ( | ||
AMQPMessage, | ||
Consumer, | ||
DisconnectionErrorInfo, | ||
MessageContext, | ||
amqp_decoder, | ||
) | ||
|
||
STREAM = "my-test-stream" | ||
|
||
|
||
async def consume(): | ||
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) | ||
) | ||
|
||
# clean close or reconnect | ||
await consumer.close() | ||
|
||
consumer = Consumer( | ||
host="localhost", | ||
port=5552, | ||
vhost="/", | ||
username="guest", | ||
password="guest", | ||
connection_closed_handler=on_connection_closed, | ||
) | ||
|
||
loop = asyncio.get_event_loop() | ||
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.create_task(consumer.close())) | ||
|
||
async def on_message(msg: AMQPMessage, message_context: MessageContext): | ||
stream = message_context.consumer.get_stream(message_context.subscriber_name) | ||
offset = message_context.offset | ||
# print("Got message: {} from stream {}, offset {}".format(msg, stream, offset)) | ||
|
||
await consumer.start() | ||
await consumer.subscribe(stream=STREAM, callback=on_message, decoder=amqp_decoder) | ||
print("im here") | ||
await consumer.run() | ||
|
||
|
||
asyncio.run(consume()) |
52 changes: 52 additions & 0 deletions
52
docs/examples/check_connection_broken/producer_handle_connections_issues_with_close.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,52 @@ | ||
import asyncio | ||
import time | ||
|
||
from rstream import ( | ||
AMQPMessage, | ||
DisconnectionErrorInfo, | ||
Producer, | ||
) | ||
|
||
STREAM = "my-test-stream" | ||
MESSAGES = 1000000 | ||
connection_is_closed = False | ||
|
||
|
||
async def publish(): | ||
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) | ||
) | ||
|
||
# clean close or reconnect | ||
await producer.close() | ||
global connection_is_closed | ||
connection_is_closed = True | ||
|
||
async with Producer( | ||
"localhost", username="guest", password="guest", connection_closed_handler=on_connection_closed | ||
) as producer: | ||
# create a stream if it doesn't already exist | ||
await producer.create_stream(STREAM, exists_ok=True) | ||
|
||
# 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), | ||
) | ||
# send is asynchronous | ||
if connection_is_closed is False: | ||
await producer.send(stream=STREAM, message=amqp_message) | ||
else: | ||
break | ||
|
||
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