-
Notifications
You must be signed in to change notification settings - Fork 43
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
Finer topic handling #143
Finer topic handling #143
Conversation
16a2bf1
to
a8ffcea
Compare
Codecov Report
@@ Coverage Diff @@
## master #143 +/- ##
=========================================
- Coverage 98.31% 98.3% -0.02%
=========================================
Files 25 25
Lines 3030 3062 +32
Branches 232 237 +5
=========================================
+ Hits 2979 3010 +31
Misses 38 38
- Partials 13 14 +1
Continue to review full report at Codecov.
|
Related to fixing the problem with the SYNC_SUB subscriptions: The cause was that, subscriptions were working before only because they were being done at However, when we wanted to unsubscribe from a topic, we couldn't figure out which was the full name of said topic, because the uuid was not available anywhere anymore. My fix as of now involves copying the |
b0efda5
to
df5e89c
Compare
d95a560
to
5b2b12b
Compare
5b2b12b
to
dda74d5
Compare
osbrain/agent.py
Outdated
@@ -898,6 +898,44 @@ def _connect_and_register(self, client_address, alias=None, handler=None, | |||
self.register(socket, register_as, alias, handler) | |||
return client_address | |||
|
|||
def unsubscribe_socket_from_topic(self, socket, topic: bytes): |
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.
Maybe unsubscribe_from_topic()
would be better. First parameter could be an AgentAddress, an alias...
Also, if you set the topic
type (i.e.: topic: bytest
), do the same for all parameters. 😉
osbrain/agent.py
Outdated
''' | ||
if isinstance(self.address[socket], AgentAddress): | ||
self.socket[socket].setsockopt(zmq.UNSUBSCRIBE, topic) | ||
if isinstance(self.address[socket], AgentChannel): |
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.
Use if-else
or if: return; if: return
.
osbrain/agent.py
Outdated
''' | ||
if isinstance(self.address[socket], AgentAddress): | ||
self.socket[socket].setsockopt(zmq.SUBSCRIBE, topic) | ||
if isinstance(self.address[socket], AgentChannel): |
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.
Use if-else
or if: return; if: return
.
client.subscribe_socket_to_topic('sub', b'TOP') | ||
|
||
# Message not received since 'TOP' topic not specified in the send call | ||
server.send('pub', 'world') |
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.
Again we find synchronization problems here.
What do you think about this? We create a function only for tests sync_pub_sub_append_received
(for example). This function takes a publisher and a subscriber. Assumes the handler in the subscriber is the classic append_received
(which we use most of the time). Starts publishing messages like increasing natural numbers (in a for loop, not using a timer, we can set a small sleep here). We wait for the first one to be appended in the client. Then we no longer send messages from the publisher. We wait for the client to receive the last number sent from the publisher (once the first is received, all messages after that one should also be received, and we know which number we sent last from the publisher). Then PUB-SUB are definitely connected and in sync. Now we reset received
in the client and there we go!
What you think?
(This would apply for other tests we have around there, until we implement heart-beating or some other fancy stuff).
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.
I think it is a good idea, however I would still need to set a handler other than append_received
once the connection is guaranteed to be ok. Perhaps we could do the same that we did in #151 with subscribe
or some similar implementation?
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.
Okay, I'm just throwing random ideas... 😂
Incorporated the proposed changes. |
osbrain/agent.py
Outdated
@@ -898,6 +898,51 @@ def _connect_and_register(self, client_address, alias=None, handler=None, | |||
self.register(socket, register_as, alias, handler) | |||
return client_address | |||
|
|||
def unsubscribe_from_topic(self, | |||
socket: Union[AgentAddress, str, zmq.Socket], |
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.
Maybe Union[AgentAddress, AgentChannel, str]
? AgentChannel should be there, and I would remove
zmq.Socket`, as the user should never be working with raw sockets in osBrain.
osbrain/agent.py
Outdated
treated_topic = channel.uuid + topic | ||
self.socket[socket].setsockopt(zmq.UNSUBSCRIBE, treated_topic) | ||
|
||
def subscribe_to_topic(self, socket: Union[AgentAddress, str, zmq.Socket], |
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.
Maybe Union[AgentAddress, AgentChannel, str]
? AgentChannel should be there, and I would remove
zmq.Socket`, as the user should never be working with raw sockets in osBrain.
osbrain/agent.py
Outdated
@@ -898,6 +898,51 @@ def _connect_and_register(self, client_address, alias=None, handler=None, | |||
self.register(socket, register_as, alias, handler) | |||
return client_address | |||
|
|||
def unsubscribe_from_topic(self, | |||
socket: Union[AgentAddress, str, zmq.Socket], | |||
topic: bytes): |
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.
We do allow Union[bytes, str]
when making subscriptions (see Agent's _subscribe
method). I think we should do the same here as well. User can directly use str
(it will automatically be converted to bytes encoded in UTF8, but can use bytes if they prefer to.
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.
(maybe this should be considered in the tests as well: test with bytes and test with string).
osbrain/agent.py
Outdated
self.socket[socket].setsockopt(zmq.UNSUBSCRIBE, treated_topic) | ||
|
||
def subscribe_to_topic(self, socket: Union[AgentAddress, str, zmq.Socket], | ||
topic: bytes): |
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.
We do allow Union[bytes, str]
when making subscriptions (see Agent's _subscribe
method). I think we should do the same here as well. User can directly use str
(it will automatically be converted to bytes encoded in UTF8, but can use bytes if they prefer to.
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.
(maybe this should be considered in the tests as well: test with bytes and test with string).
@Flood1993 for consistency with how we currently execute the
What do you think? (We can talk about this tomorrow if you want) |
I think that would be great. Right now the I completely agree with accepting various handlers at the same time through a dictionary, and also with making |
Maybe We could also think for a word to use everywhere when we refer to an Agent's address/channel/alias. Which in my head would be simply "address". 😕 |
Closed in favor of #158 |
Use case: In osMarkets, we want to be able to subscribe to different topics after the
connect
call has been done by a client. Therefore, it will come in handy to be able to easily modify the topics a given SUB socket is subscribed to.Note that this first implementation is only for direct PUB/SUB topics. I think for SYNC_PUB/SYNC_SUB channels, it will need a bit more work, but the idea would be the same. I will start working on the tests for that case right now, and will push to this PR when they are ready.