-
Notifications
You must be signed in to change notification settings - Fork 37.7k
ZMQ: Small cleanups in the ZMQ code #13686
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
Conversation
I split the patch into three commits which I think make sense logically and for the review. But of course I'm happy to squash any of the changes, or to revert parts of them (as code readability is a matter of taste). |
Concept ACK. From a quick view LGTM. |
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
Rebased |
What do you think about wrapping the code with namespace a {
namespace b {
class C {}
}
} |
@kostyantyn: At least for now, there seems to be not much namespace usage at all in the Bitcoin code - so I'm not sure if we should change that lightly. Also, note that Also, note that C++ namespaces are different from Java's, so that some people advise against nesting (e.g. exact correspondence to the directory structure as you suggest). |
Concept ACK @jmcorgan do you mind taking a look here? |
55eca08
to
00447bc
Compare
Rebased |
c4ce449
to
95c442d
Compare
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.
Concept ACK.
@hebasto: For some reason, Github won't let me reply this directly to your comment above. The guard is not necessary, since it is present already where the |
Rebased. Is there anything missing for me to do here? I think this is a real improvement to the quality of the ZMQ code. |
@jmcorgan Are you able to take a look at this? |
02464dd
to
f7950b1
Compare
b43ad20
to
d47b028
Compare
Rebase done, and I've now cleaned up the commit history as well (squashed in the "fixup" commits as appropriate). Also split out the |
The first commit 707c480 is not compiled for me. Make iterator type |
d47b028
to
7da6bad
Compare
This is a pure refactoring of zmqnotificationinterface to make the code easier to read and maintain. It replaces explicit iterators with C++11 for-each loops where appropriate and uses std::unique_ptr to make memory ownership more explicit.
This factors out the common logic to run over all ZMQ notifiers, call a function on them, and remove them from the list if the function fails is extracted to a helper method. Note that this also fixes a potential memory leak: When a notifier was removed previously after its callback returned false, it would just be removed from the list without destructing the object. This is now done correctly by std::unique_ptr behind the scenes.
Instead of returning a raw pointer from CZMQNotifierFactory and implicitly requiring the caller to know that it has to take ownership, return a std::unique_ptr to make this explicit. This also changes the typedef for CZMQNotifierFactory to use the new C++11 using syntax, which makes it (a little) less cryptic.
zmqconfig.h is currently not really needed anywhere, except that it declares zmqError (which is then defined in zmqnotificationinterface.cpp). Note in particular that there is no need to conditionally include zmq.h only if ZMQ is enabled, because the place in the core code where the ZMQ library itself is included (init.cpp) is conditional already on that. This commit removes zmqconfig.h and replaces it by a much simpler zmqutil.h library for zmqError. The definition of the function is moved to the matching (newly created) zmqutil.cpp.
7da6bad
to
166ddcb
Compare
Sorry for that, it is fixed now. The overall PR is not changed (except I've rebased it to current |
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.
Windows headers define SendMessage as a macro, which leads to problems with the method name "SendMessage". To circumvent this, we rename the method to "SendZmqMessage". -BEGIN VERIFY SCRIPT- sed -i 's/SendMessage/SendZmqMessage/g' src/zmq/zmqpublishnotifier.* -END VERIFY SCRIPT-
166ddcb
to
6fe2ef2
Compare
Good point, done. |
utACK 6fe2ef2 I restarted one travis job that appeared to be a spurious timeout. |
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.
6fe2ef2 scripted-diff: Rename SendMessage to SendZmqMessage. (Daniel Kraft) a3ffb6e Replace zmqconfig.h by a simple zmqutil. (Daniel Kraft) 7f2ad1b Use std::unique_ptr for CZMQNotifierFactory. (Daniel Kraft) b93b9d5 Simplify and fix notifier removal on error. (Daniel Kraft) e15b1cf Various cleanups in zmqnotificationinterface. (Daniel Kraft) Pull request description: This contains various small code cleanups that make the ZMQ code easier to read and maintain (at least in my opinion). The only functional change is that a potential memory leak is fixed that would have occured when a notifier is removed from the `notifiers` list after its callback function returned `false` (which is likely not relevant in practice but still a bug). ACKs for top commit: instagibbs: utACK 6fe2ef2 hebasto: re-ACK 6fe2ef2, only the latest commit got a scripted-diff since my [previous](bitcoin#13686 (review)) review. Tree-SHA512: 8206f8713bf3698d7cd4cb235f6657dc1c4dd920f50a8c5f371a559dd17ce5ab6d94d6281165eef860a22fc844a6bb25489ada12c83ebc780efd7ccdc0860f70
Merged bitcoin/bitcoin#13686 with Xaya's ZMQ extensions.
Co-authored-by: Calin Culianu <calin.culianu@gmail.com> Summary --- This is a backport of [Core#13686](bitcoin/bitcoin#13686). While working on !1045 I noticed some un-idiomatic C++ usage and a potential memory leak. I went and looked at the Core repo and it turns out they have since updated the ZMQ subsystem to be better and they fixed the leak. Original commit message: This contains various small code cleanups that make the ZMQ code easier to read and maintain (at least in my opinion). The only functional change is that a potential memory leak is fixed that would have occured when a notifier is removed from the notifiers list after its callback function returned false (which is likely not relevant in practice but still a bug). Note that this depends on MR !1045 (it is a commit on top of it). I adapted this code a little bit to alphabetize the headers and also other minor tiny nits. It's 99% the Core commit, essentially. Test Plan --- - `ninja all check` - `test/functional/test_runner.py interface_zmq` - `test/functional/test_runner.py rpc_zmq`
merge bitcoin#18820, bitcoin#19764, bitcoin#13686, bitcoin#17538, bitcoin#18405: zmq backports, boost depends split
Summary: This is a pure refactoring of zmqnotificationinterface to make the code easier to read and maintain. It replaces explicit iterators with C++11 for-each loops where appropriate and uses std::unique_ptr to make memory ownership more explicit. This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] [1/5] bitcoin/bitcoin@e15b1cf Test Plan: `ninja all check-all` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D10282
Summary: This factors out the common logic to run over all ZMQ notifiers, call a function on them, and remove them from the list if the function fails is extracted to a helper method. Note that this also fixes a potential memory leak: When a notifier was removed previously after its callback returned false, it would just be removed from the list without destructing the object. This is now done correctly by std::unique_ptr behind the scenes. This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] [2/5] bitcoin/bitcoin@b93b9d5 Depends on D10282 Test Plan: `ninja all check-all` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D10283
Summary: Instead of returning a raw pointer from CZMQNotifierFactory and implicitly requiring the caller to know that it has to take ownership, return a std::unique_ptr to make this explicit. This also changes the typedef for CZMQNotifierFactory to use the new C++11 using syntax, which makes it (a little) less cryptic. This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] [3/5] bitcoin/bitcoin@7f2ad1b Depends on D10283 Test Plan: `ninja all check-all` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D10284
Summary: zmqconfig.h is currently not really needed anywhere, except that it declares zmqError (which is then defined in zmqnotificationinterface.cpp). Note in particular that there is no need to conditionally include zmq.h only if ZMQ is enabled, because the place in the core code where the ZMQ library itself is included (init.cpp) is conditional already on that. This commit removes zmqconfig.h and replaces it by a much simpler zmqutil.h library for zmqError. The definition of the function is moved to the matching (newly created) zmqutil.cpp. This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] bitcoin/bitcoin@a3ffb6e See D7579 for why blockdb.h is included instead of validation.h in zmqpublishnotifier.cpp Depends on D10284 Test Plan: `ninja all check-all` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D10285
Summary: Windows headers define SendMessage as a macro, which leads to problems with the method name "SendMessage". To circumvent this, we rename the method to "SendZmqMessage". This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] [5/5] bitcoin/bitcoin@6fe2ef2 Depends on D10285 Test Plan: `ninja all check-all` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D10286
This contains various small code cleanups that make the ZMQ code easier to read and maintain (at least in my opinion). The only functional change is that a potential memory leak is fixed that would have occured when a notifier is removed from the
notifiers
list after its callback function returnedfalse
(which is likely not relevant in practice but still a bug).