Skip to content
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

Changed is_connected to return bool while maintaining error checking #125

Merged
merged 3 commits into from
Nov 10, 2022

Conversation

BiffoBear
Copy link

closes #109
Renamed original is_connected method to _connected to maintain internal connection status checking. Created a new is_connected function that returns a bool. This is a breaking change.

This change allows code to be written like:

while not mqtt_client.is_connected():
    try:
        mqtt_client.connect()
    except RuntimeError:
        print("Unable to connect. Waiting for 10 seconds before retrying…")
        time.sleep(10)
print("Connected to MQTT broker.")

…purpose. Created new is_connected that returns a bool.
Copy link
Member

@tekktrik tekktrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good change, but I'm also not as familiar with MQTT applications. I think this is worth getting a second opinion since its also API breaking. I'll bring it up at the CircuitPython weekly meeting tomorrow!

@BiffoBear
Copy link
Author

BiffoBear commented Oct 17, 2022

I think this is a good change, but I'm also not as familiar with MQTT applications. I think this is worth getting a second opinion since its also API breaking. I'll bring it up at the CircuitPython weekly meeting tomorrow!

After poking around in the code, I believe the is_connected() method is poorly named. The method does not reflect the state of the connection when it is called. It checks whether a network socket exists and whether an initial connection was successfully made to the MQTT broker when MQTT.connect() was called. The is_connected method acts as a gatekeeper so methods like publish() and ping() are not called before the connection to the broker has been made. This is why it returns True or raises an exception.

If the MQTT broker is down, the is_connected method will return True but any attempt to write to the broker will raise a BrokenPipeError: 32 exception.

After poking the code a bit more, MQTT.connect() returns a value if a connection with the broker was successfully initiated and raises exceptions for failed connections. Thus a while not mqtt_client.is_connected: loop is not required.
In short, I no longer think that this breaking change is worth making.

Perhaps the method should be renamed _initial_connection_made() or something along those lines to make clear that it does not reflect the current connection status and is not particularly useful to the user.

@tekktrik tekktrik requested a review from a team October 24, 2022 00:07
@BiffoBear BiffoBear closed this Oct 24, 2022
@BiffoBear BiffoBear reopened this Oct 24, 2022
Copy link
Member

@jepler jepler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! However, I think it could be written more simply. Do you mind changing it around?

adafruit_minimqtt/adafruit_minimqtt.py Show resolved Hide resolved
@BiffoBear
Copy link
Author

Thanks to @2bndy5 for clarifying my thinking and pointing out that the Paho implementation returns a bool. Changes requested made.

@BiffoBear BiffoBear reopened this Nov 1, 2022
@brentru brentru self-requested a review November 1, 2022 15:40
@tekktrik tekktrik merged commit 199a0b8 into adafruit:main Nov 10, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Nov 11, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_CharLCD to 3.4.4 from 3.4.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_CharLCD#73 from BiffoBear/add_typing
  > Update .pylintrc for v2.15.5
  > Fix release CI files
  > Update pylint to 2.15.5
  > Updated pylint version to 2.13.0
  > Switching to composite actions

Updating https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH to 5.1.19 from 5.1.18:
  > Merge pull request adafruit/Adafruit_CircuitPython_LIS3DH#73 from crotwell/extra_underscore
  > Update .pylintrc for v2.15.5
  > Fix release CI files
  > Update pylint to 2.15.5
  > Updated pylint version to 2.13.0
  > Switching to composite actions

Updating https://github.com/adafruit/Adafruit_CircuitPython_SharpMemoryDisplay to 1.4.6 from 1.4.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_SharpMemoryDisplay#22 from grouma/main
  > Update .pylintrc for v2.15.5
  > Fix release CI files
  > Update pylint to 2.15.5
  > Updated pylint version to 2.13.0
  > Switching to composite actions

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 6.0.0 from 5.5.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#125 from BiffoBear/Make_is_connected_return_bool
  > Update .pylintrc for v2.15.5
  > Fix release CI files
  > Update pylint to 2.15.5
  > Updated pylint version to 2.13.0
  > Switching to composite actions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

is_connected raises MMQTTException when not connected, rather that returning False.
4 participants