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

Support lua scripting #69

Closed penguinol closed 6 months ago

penguinol commented 9 months ago

Lots of MQTT broker support lua scripting, such as vernemq and emqtt. Lua can make it easier to develop plugins.

bigsmoke commented 9 months ago

As far as I know Wiebe's intentions, a big part of FlashMQ's design philosophy is to have a minimal set of dependencies; another is to do one thing and to do it well. In my opinion, built-in Lua support would add needless complexity and an extra dependency. Though the latter could be configurable at compile-time, that in itself creates more packaging choices and difficulties.

The best approach for those wishing to work with Lua (or any other scripting language for that matter) would be to write a plugin (in C++) that adds support for that language. FlashMQ's plugin ABI is stable, and, since there's not much you can't do with a plugin within FlashMQ's plugin architecture, implementing scripting support shouldn't be hugely difficult.

However … another cornerstone of FlashMQ is performance, and I would venture that in most cases it would be unwise to surrender some of that performance to the drag imposed by an interpreted language.

Then there is the matter of type safety. FlashMQ is written in strongly-typed C++ to make it as fault-free as possible. A quick Google reveals that there are a number of competing approaches to make Lua more strongly type than the baseline language.

All in all, I can't speak for Wiebe, but I think that adding Lua suport to FlashMQ itself would be a mistake. As for a plugin that adds Lua support: I think it can work fine, but would strongly advice to invest that energy in becoming a bit more comfortable in C++. Who doesn't want a non-scripting language on their CV after all?

bigsmoke commented 9 months ago

May I ask, @penguinol, if you have some specific plugin functionality in mind to make you wish you could implent it in Lua?

I'm asking because there are plans for a bunch of example C++ plugins, so that it will be less daunting for novice C++ programmers to build on these examples for most trivial needs, and it will be good to have some concrete needs and wishes in the back of our heads when creating these examples.

penguinol commented 9 months ago

I'm a C++ programmer, but i don't think everything must be implemented in C++. A external lua scripting plugin is fine. With lua, we can implement auth with http\reids\sql in less than 100 lines of lua code and no need to worry about dependencies, make file, compiler and so on. This can make flashmq out of box and easier to use. Not only for C++ programmers, but also for ops and other programmers. I don't really worry about the performance of lua, something like openresty also works fine. The cost of redis/http/sql query is much higher than lua. If users do care about the performance, they should write c++ plugins, otherwise, lua is enough. A lua plugin with sql\redis\http\json support can fit most of needs.

halfgaar commented 8 months ago

@bigsmoke said it. A plugin for Lua may be good idea just to have, however:

With lua, we can implement auth with http\reids\sql in less than 100 lines of lua code and no need to worry about dependencies, make file, compiler and so on. This can make flashmq out of box and easier to use. Not only for C++ programmers, but also for ops and other programmers.

I don't really worry about the performance of lua, something like openresty also works fine. The cost of redis/http/sql query is much higher than lua.

I suspect the way this will be implemented would be a blocking/synchronous call. An SQL query can take a few milliseconds easily, which is an eternity in FlashMQ time. You may as well then use other brokers that do support Lua.

For HTTP calls, there is an example plugin for async libcurl to avoid blocking calls, and I suspect that will be impossible to implement with Lua. Or, at least write as technical debt from the start (quick to set up, inflexible/limited later).

bigsmoke commented 6 months ago

@halfgaar , I think you can close this issue?

halfgaar commented 6 months ago

@halfgaar , I think you can close this issue?

After some consideration, I agree. I'm all for people developing a lua plugin, but as a core feature, I have to conclude that it's not the most logical feature. FlashMQ's fast, fully async model is too easily undermined with lua.