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

Plugin interface: call a plugin main_init function from main thread before other threads are created #35

Closed slavslavov closed 1 year ago

slavslavov commented 1 year ago

Currently all plugin functions are called for each thread but it will be good to have one which is called only once from the main thread before the other threads are created. To minimise the impact on the current design its signature may look like this: typedef void(*F_flashmq_plugin_main_init_v2)(std::unordered_map<std::string, std::string> &auth_opts); No global data pointer is added to keep the multi-core design of FlashMQ unchanged and in case needed, a static variable can be used as suggested in the documentation for flashmq_plugin_allocate_thread_memory.

One use case for this function would be to check which OS user is running the process.

The best place to call this function would be here:

    GlobalStats *globalStats = GlobalStats::getInstance();
->
    for (int i = 0; i < num_threads; i++)
    {
        std::shared_ptr<ThreadData> t = std::make_shared<ThreadData>(i, settings);

But the main problem is Authentication, which is a member of ThreadData is instantiated a few lines after this location.

halfgaar commented 1 year ago

How about this? I didn't test it yet, but I don't see how it can't work.

slavslavov commented 1 year ago

Yep, this looks quite nice and exactly what I had in mind.

slavslavov commented 1 year ago

Just thinking do we need main_deinit? I don't have a use case for it but init/deinit usually go in pairs.

halfgaar commented 1 year ago

You're right. I also made some (additional) fixes. Pushed to master.

halfgaar commented 1 year ago

Released.