-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expoential as a default reconnection policy #196
Changes from all commits
7372652
d9072dc
16a1f79
30c4a92
27be457
b6746db
2aad937
be2ffa9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import sys | ||
import time | ||
|
||
from pubnub.pnconfiguration import PNConfiguration | ||
from pubnub.pubnub import PubNub, SubscribeListener | ||
from pubnub.enums import PNReconnectionPolicy, PNStatusCategory | ||
|
||
|
||
class TestListener(SubscribeListener): | ||
status_result = None | ||
disconnected = False | ||
|
||
def status(self, pubnub, status): | ||
if status.category == PNStatusCategory.PNDisconnectedCategory: | ||
print('Could not connect. Exiting...') | ||
self.disconnected = True | ||
|
||
def message(self, pubnub, message): | ||
print(f'Message:\n{message.__dict__}') | ||
|
||
def presence(self, pubnub, presence): | ||
print(f'Presence:\n{presence.__dict__}') | ||
|
||
|
||
logger = logging.getLogger("pubnub") | ||
logger.setLevel(logging.DEBUG) | ||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setLevel(logging.DEBUG) | ||
logger.addHandler(handler) | ||
|
||
|
||
config = PNConfiguration() | ||
config.subscribe_key = "demo" | ||
config.publish_key = "demo" | ||
config.user_id = 'example' | ||
config.enable_subscribe = True | ||
config.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL | ||
config.origin = '127.0.0.1' | ||
config.ssl = False | ||
|
||
listener = TestListener() | ||
|
||
pubnub = PubNub(config) | ||
pubnub.add_listener(listener) | ||
sub = pubnub.subscribe().channels(['example']).execute() | ||
|
||
while not listener.disconnected: | ||
time.sleep(0.5) | ||
print('Disconnected. Bye.') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import asyncio | ||
import logging | ||
import sys | ||
|
||
from pubnub.callbacks import SubscribeCallback | ||
from pubnub.models.consumer.common import PNStatus | ||
from pubnub.pubnub_asyncio import PubNubAsyncio | ||
from pubnub.pnconfiguration import PNConfiguration | ||
from pubnub.enums import PNReconnectionPolicy, PNStatusCategory | ||
|
||
config = PNConfiguration() | ||
config.subscribe_key = "demo" | ||
config.publish_key = "demo" | ||
config.enable_subscribe = True | ||
config.uuid = "test-uuid" | ||
config.origin = "127.0.0.1" | ||
config.ssl = False | ||
config.reconnect_policy = PNReconnectionPolicy.NONE | ||
|
||
pubnub = PubNubAsyncio(config) | ||
|
||
logger = logging.getLogger("pubnub") | ||
logger.setLevel(logging.WARNING) | ||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setLevel(logging.WARNING) | ||
logger.addHandler(handler) | ||
|
||
|
||
class SampleCallback(SubscribeCallback): | ||
message_result = None | ||
status_result = None | ||
presence_result = None | ||
|
||
def status(self, pubnub, status): | ||
self.status_result = status | ||
|
||
def message(self, pubnub, message): | ||
self.message_result = message | ||
|
||
def presence(self, pubnub, presence): | ||
self.presence_result = presence | ||
|
||
|
||
async def main(): | ||
listener = SampleCallback() | ||
pubnub.add_listener(listener) | ||
pubnub.subscribe().channels("my_channel").execute() | ||
while True: | ||
if isinstance(listener.status_result, PNStatus) \ | ||
and listener.status_result.category == PNStatusCategory.PNDisconnectedCategory: | ||
print('Could not connect. Exiting...') | ||
break | ||
await asyncio.sleep(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) | ||
loop.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,13 @@ def __init__(self, pubnub): | |
def _register_heartbeat_timer(self): | ||
self.stop_heartbeat_timer() | ||
|
||
if self._retry_limit_reached(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how it works in Python, but should announcing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. This one is for non-event engine subscribe (yeah, python has more than one subscribe) |
||
logger.warning("Reconnection retry limit reached. Disconnecting.") | ||
disconnect_status = PNStatus() | ||
disconnect_status.category = PNStatusCategory.PNDisconnectedCategory | ||
self._pubnub._subscription_manager._listener_manager.announce_status(disconnect_status) | ||
return | ||
|
||
self._recalculate_interval() | ||
|
||
self._timer = threading.Timer(self._timer_interval, self._call_time) | ||
|
@@ -129,6 +136,9 @@ def _call_time_callback(self, resp, status): | |
def start_polling(self): | ||
if self._pubnub.config.reconnect_policy == PNReconnectionPolicy.NONE: | ||
logger.warning("reconnection policy is disabled, please handle reconnection manually.") | ||
disconnect_status = PNStatus() | ||
disconnect_status.category = PNStatusCategory.PNDisconnectedCategory | ||
self._pubnub._subscription_manager._listener_manager.announce_status(disconnect_status) | ||
return | ||
|
||
logger.debug("reconnection manager start at: %s" % utils.datetime_now()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to provide custom delay other than
2
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, not really. Should we add this option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added in latest commit. thanks for pointing that out ;)