halfgaar / FlashMQ

FlashMQ is a fast light-weight MQTT broker/server, designed to take good advantage of multi-CPU environments
https://www.flashmq.org/
Open Software License 3.0
173 stars 24 forks source link

Access to payload in auth plugin #33

Closed halfgaar closed 1 year ago

halfgaar commented 1 year ago

Review + test + think about https://github.com/slavslavov/FlashMQ/pull/1.

Was discussed here.

slavslavov commented 1 year ago

@halfgaar, how shall we approach this one. I remember you mentioned that the payload should be exposed in the plugin_alter functions too. But does it have to be readonly or modifiable?

halfgaar commented 1 year ago

My plan was to (try to) implement the read-only option. It mostly means stopping support for Ubuntu 18. I've been kind of sluggish, I know. But the time is about now to do it.

slavslavov commented 1 year ago

I have updated my draft PR to include the payload in the two F_flashmq_plugin_alter_XX functions. As with the previous commit it compiles but I haven't tested it. These are breaking changes and I think your idea was to create plugin version 2 which will require more work to support many versions. If I can help with something - let me know.

halfgaar commented 1 year ago

My next few weeks are busy, but I did perform a quick upgrade of my PC here so I have C++ 17 available. I'll probably have some time to dive into this soon.

What needs testing (+test code) is whether the payload is visible in the plugin for:

halfgaar commented 1 year ago

I created a branch with a draft. I didn't actually test much manually, but all automated tests do assert that the payload is not empty, so it should work. I'll check in more detail later.

slavslavov commented 1 year ago

The current implementation doesn't work and needs some small changes. I am in the middle of testing and will push a commit in my branch later today.

halfgaar commented 1 year ago

Note that my implementation works very differently from yours. You may want to make a new branch.

slavslavov commented 1 year ago

I had a look at your branch. The modifications I had to do in mine to make it work are coming closer to yours, although yours look more logical (you know your code better than anyone else :+1: ). I will stop updating my branch for now. Only one thing you may want to consider - std::string_view is really cheap to be passed by value and will perform better than passing by const ref)

halfgaar commented 1 year ago

Only one thing you may want to consider - std::string_view is really cheap to be passed by value and will perform better than passing by const ref)

Can you elaborate on that? I thought that was only true for plain old data types. It would seem to be a simple 64 bit value as address is cheaper. Only if you benefit from locality of reference and/or CPU cache would a copy seem faster to me, but with a string_view, that's not possible anyway (it will most likely point far).

Using a reference has the advantage of being faster when you don't use the payload.

I tried to compare with Godbolt compiler explorer, but my test code was too synthetic and the optimizer threw it all away anyway...

slavslavov commented 1 year ago

I don't have personal experience measuring performance, it is all I have read. But when you google it you will read everywhere that std::string_view is faster to pass by value - it has only two members which fit well into the CPU registers, while with the reference you have one more indirection. Here are some links:

halfgaar commented 1 year ago

Interesting. I'll dive into more and likely make it a copy by value then.

Is the feature also working out for you? Having your approval before I merge it in would be great.

slavslavov commented 1 year ago

Yes, the payload is now available in the plugin. Thanks.

halfgaar commented 1 year ago

Is it weird that for an empty payload, we point it to outside the vector bites with a length 0 ... ?

slavslavov commented 1 year ago

It is an interesting one. I think it should be fine. std::string_view is not a zero based c-type string, it always relies on the length to be setup correctly. So with a length of 0 it should be considered empty, no matter what the data points to.

halfgaar commented 1 year ago

Released.