Skip to content
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

add support for sd-event as event loop #1

Draft
wants to merge 15 commits into
base: v4.1-stable
Choose a base branch
from
Draft

Conversation

defaultbranch
Copy link
Owner

Der grösste teil vom Code steckt in sdevent.c.

Als Vorlage habe ich libuv.c und libev.c verwendet.

Es gibt noch ein README.md zum Einbinden von Event-Loop-Bibliotheken.

Yucong Sun and others added 3 commits November 23, 2020 20:23
Chrome has started being able to issue frame type 0x42, we drop the connection
before we realize we wanted to ignore it.

This explicitly ignores it a bit earlier.
@defaultbranch defaultbranch marked this pull request as draft November 25, 2020 09:09
Change-Id: I81b67cbb28591a20842fc7d6e09b78375845aae4
Copy link
Collaborator

@langchr86 langchr86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss one block of: https://github.com/defaultbranch/libwebsockets/blob/main/lib/event-libs/README.md

Enabling event lib adoption

You need to add a LWS_SERVER_OPTION... flag as necessary in ./lib/libwebsockets.h enum lws_context_options, and follow the existing code in lws_create_context() to convert the flag into binding your ops struct to the context.

CMakeLists.txt Outdated
@@ -152,6 +152,7 @@ option(LWS_WITH_LIBEV "Compile with support for libev" OFF)
option(LWS_WITH_LIBUV "Compile with support for libuv" OFF)
option(LWS_WITH_LIBEVENT "Compile with support for libevent" OFF)
option(LWS_WITH_GLIB "Compile with support for glib event loop" OFF)
option(LWS_WITH_SDEVENT "Compile with support for systemd event loop" OFF)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
option(LWS_WITH_SDEVENT "Compile with support for systemd event loop" OFF)
option(LWS_WITH_SDEVENT "Compile with support for systemd event loop: sd-event" OFF)

Copy link
Owner Author

@defaultbranch defaultbranch Nov 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, I wanted to keep the wording aligned with libuv/libevent/glib above.
Is "systemd event loop" ambiguous? I thought sd-event is the only systemd event loop?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is the only one. I only think that we could easily miss the d in systemd and then think about raw Linux event loop or something like that. sd-event ist much more specific. Maybe we should use only Compile with support for sd-event instead of systemd event loop

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed text to "Compile with support for sd-event"

@@ -230,6 +230,9 @@
/**< (CTX) Disable lws_system state, eg, because we are a secure streams
* proxy client that is not trying to track system state by itself. */

#define LWS_SERVER_OPTION_SDEVENT (1ll << 36)
/**< (CTX) Use systemd event loop */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**< (CTX) Use systemd event loop */
/**< (CTX) Use systemd event loop: sd-event */

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(similar to the previous comment)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to "Use sd-event loop"

#endif
const lws_plugin_evlib_t evlib_sd = {
.hdr = {
"systemd event loop",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"systemd event loop",
"systemd event loop: sd-event",

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(similar to the previous comment)

Comment on lines 350 to 352
if (LWS_WITH_SDEVENT)
set(LWS_WITH_SDEVENT 1)
endif()
Copy link
Collaborator

@langchr86 langchr86 Nov 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional and the set variable are both the exact same. What is this check intended for?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this, not sure whether its a bug (could be), or it is there to ensure that anything "defined" is an integer 1 while anything undefined remains undefined (similar to what https://stackoverflow.com/questions/14751973/what-is-in-c does)

Need to check...

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forwarded the question to warmcat#2130, waiting for an answer.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. (The maintainers found this pointless as well.)

Copy link
Collaborator

@langchr86 langchr86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks not too bad. But I did not completely understand everything. Maybe I will with the next review after you added more tests. So that the code can mature. Most things I noticed is the missing error handling at many points.

set(LIB_SYSTEMD_LIBRARIES CACHE PATH "Path to the libsystemd library")
if ("${LWS_SYSTEMD_LIBRARIES}" STREQUAL "")
if (NOT LIB_SYSTEMD_FOUND)
message("searching for systemd")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this line because the find_library command will already print something similar.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


if (LWS_WITH_EVLIB_PLUGINS)

create_evlib_plugin(evlib_sd sdevent.c ${LIBSYSTEMD_LIBRARIES})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other implementations do also mention their private header here.

Suggested change
create_evlib_plugin(evlib_sd sdevent.c ${LIBSYSTEMD_LIBRARIES})
create_evlib_plugin(evlib_sd sdevent.c private-lib-event-libs-sdevent.h ${LIBSYSTEMD_LIBRARIES})

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment on lines +9 to +25
struct lws_context_eventlibs_sdevent {
};

struct lws_pt_eventlibs_sdevent {
struct lws_context_per_thread *pt;
struct sd_event *io_loop;
struct sd_event_source *sultimer;
struct sd_event_source *idletimer;
};

struct lws_vh_eventlibs_sdevent {
};

struct lws_wsi_watcher_sdevent {
struct sd_event_source *source;
uint32_t events;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those should go into private-lib-event-libs-sdevent.h

@@ -0,0 +1,3 @@
#include <private-lib-core.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add the licence/copyright header in each of these source files?

sd_event_source_unref(ptpriv->sultimer);
ptpriv->sultimer = NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sd_event_source_unref(ptpriv->sultimer);
ptpriv->sultimer = NULL;
ptpriv->sultimer = sd_event_source_unref(ptpriv->sultimer);

if (!(wsi_to_priv_sd(wsi)->events & (EPOLLIN | EPOLLOUT)))
sd_event_source_set_enabled(wsi_to_priv_sd(wsi)->source, SD_EVENT_ONESHOT);
else
sd_event_source_set_enabled(wsi_to_priv_sd(wsi)->source, SD_EVENT_OFF);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All sd_event_ functions are missing error handling in this function.


sd_event_source_set_io_events(wsi_to_priv_sd(wsi)->source, wsi_to_priv_sd(wsi)->events);

if (!(wsi_to_priv_sd(wsi)->events & (EPOLLIN | EPOLLOUT)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch if/else clause to make this more understandable ( no ! needed). Besides, is this even correct? If I understand correctly we do disable the event if at least one of the two event flags are set?

eventfd.events = 0;
eventfd.revents = 0;

// TODO handle revents error bits
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

}

lws_pt_unlock(pt);
lws_context_unlock(pt->context);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to release the locks here? Because you still use pt later.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from libuv/libev.

But I will check this again...

lws_context_unlock(pt->context);

return -1;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All sd_event_ functions are missing error handling in this function.

When using a foreign libuv loop, context creation may fail after adding
handles to the foreign loop... if so, it can no longer deal with the
fatal error by unpicking the created context and returning NULL... it
has to brazen it out with a half-baked context that has already started
the destroy flow and allow the foreign loop to close out the handles
the usual way for libuv.

warmcat#2129
@defaultbranch
Copy link
Owner Author

@langchr86 wrote:

I miss one block of: https://github.com/defaultbranch/libwebsockets/blob/main/lib/event-libs/README.md

Enabling event lib adoption

You need to add a LWS_SERVER_OPTION... flag as necessary in ./lib/libwebsockets.h enum lws_context_options, and follow the existing code in lws_create_context() to convert the flag into binding your ops struct to the context.

This README is probably a bit out of date regarding the include file location. Today the enum and the new flag LWS_SERVER_OPTION_SDEVENT reside in include/libwebsockets/lws-context-vhost.h instead.

@defaultbranch defaultbranch force-pushed the sd-event branch 2 times, most recently from bb1d011 to 9239f11 Compare December 14, 2020 12:18
s-rogonov and others added 2 commits December 24, 2020 13:50
Generate the config file for the installation tree
fix wrong LWS__INCLUDE_DIRS definition
@defaultbranch defaultbranch force-pushed the sd-event branch 2 times, most recently from e6fc0d8 to 8bc15ae Compare January 6, 2021 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants