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 queueLength() #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add queueLength() #38

wants to merge 2 commits into from

Conversation

MoonModules
Copy link

To know how full the client queue is - and do only 'lossless' things if the queue is not too full

Comment on lines +212 to +214
size_t queueLength() { //added by ewowi
return _messageQueue.length();
}
Copy link

@mathieucarbou mathieucarbou Sep 30, 2024

Choose a reason for hiding this comment

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

Not a great idea for these reasons:

  1. Traversal is O(N), not O(1) like with std::list on c++ >= 11
  2. No mutex protection to block concurrent access

The internal structure (LinkedList) should be changed IMO because it is not efficient.

In our community maintained fork (https://github.com/mathieucarbou/ESPAsyncWebServer), which is far more evolved than this fork, we have implemented it like this:

size_t AsyncWebSocketClient::queueLen() const {
#ifdef ESP32
  std::lock_guard<std::mutex> lock(_lock);
#endif

  return _messageQueue.size();
}

_messageQueue being a std::list

Copy link

@mathieucarbou mathieucarbou left a comment

Choose a reason for hiding this comment

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

just a few comments ;-)

@ewowi
Copy link

ewowi commented Sep 30, 2024

just a few comments ;-)

Hi Mathieu, thx for your response, sounds like I should try this repo! - Which I am doing now ;-)

queueLen() works !!

However, I get a few use of deleted function errors like this:

src/Sys/SysModWeb.h:44:45: error: use of deleted function 'AsyncWebSocket::AsyncWebSocket(const AsyncWebSocket&)'
     AsyncWebSocket ws = AsyncWebSocket("/ws");

Any idea what can cause this?
I define ws and server in a class like this:

image

That isn't the problem hopefully, or is defining this globally required?

@mathieucarbou
Copy link

mathieucarbou commented Sep 30, 2024

However, I get a few use of deleted function errors like this:

That's not normal... Did you import AsyncWebSocket.h ?
(That was incorrect sorry)

I would need to see more of your code... This is something related to the recent split to move implementation code in cpp files, but I don't understand why I don't saw it in the many projects I have.

@ewowi
Copy link

ewowi commented Sep 30, 2024

I created a branch in my repo which moves from the esp home to your repo.
You can see the complete log here:

https://github.com/MoonModules/StarLight/actions/runs/11113157238/job/30876843828

this is the branch: https://github.com/MoonModules/StarLight/tree/mc-eaws-test
ws is defined here: https://github.com/MoonModules/StarLight/blob/mc-eaws-test/src/Sys/SysModWeb.h

Let me know if you need more info, thx for looking into this,
Ewoud

Oh and including AsyncWebSocket.h indeed made no difference, and moving the vars out of the class also resulted in the same errors

@mathieucarbou
Copy link

I created a branch in my repo which moves from the esp home to your repo. You can see the complete log here:

https://github.com/MoonModules/StarLight/actions/runs/11113157238/job/30876843828

this is the branch: https://github.com/MoonModules/StarLight/tree/mc-eaws-test ws is defined here: https://github.com/MoonModules/StarLight/blob/mc-eaws-test/src/Sys/SysModWeb.h

Let me know if you need more info, thx for looking into this, Ewoud

Oh and including AsyncWebSocket.h indeed made no difference, and moving the vars out of the class also resulted in the same errors

This is not right:

    WebSocket ws = WebSocket("/ws");
    WebServer server = WebServer(80);

=>

    WebSocket ws("/ws");
    WebServer server(80);

You are relying on an inexisting copy constructor.

Sae error above for Psychic (side note; I am one of PsychicHttp dev also)

@ewowi
Copy link

ewowi commented Oct 1, 2024

Hey thanks for your help!

That makes sense if ws and server is defined outside a class, but within a class I thought this was the way to do it. But maybe just not possible to define it inside a class? (although esp home version 'accepted' it - but making copies ...)

I now did it like this:

image

Diff is here: MoonModules/StarLight@06423f8

I then had new errors, mainly related to for (auto client:ws.getClients()) I use the auto : syntax and added an &. But maybe this is not your recommendated way. See the latest code, specially here:

https://github.com/MoonModules/StarLight/blob/06423f84fe84d5b52a45d47a3bb85986745d0f2f/src/Sys/SysModWeb.cpp#L424-L459

Error log here: https://github.com/MoonModules/StarLight/actions/runs/11120233580/job/30896848815

And yeah I am investigating for a while which ws server is the best to use, see also some remarks / comments I have in my platformio.ini. Tried also PsychicHttp but never finished that POC. So if you work on both, which one to choose ? ;-)

@mathieucarbou
Copy link

Choose the one you prefer!

I am a heavy contributor / collaborator of Psychic: at the moment, we are preparing v2 which will be an API rework and won't be compatible with v1. A lot of perf issues and bugs are fixed. Psychic is not yet on par with ESPAsyncWebServer in terms of features m, and this is partly due to the limitations of the esp-idf API used behind. Also, it does not have years of history behind it...

In terms of flag usage and memory footprint, ESPAsyncWebServer is better than Psychic and also better than Arduino Webserver. But it does not support https.

For your issue, it is possible that you need to change a little your code if you are doing more advanced things like using the client API because we had to change the internal structure used to store clients for better perfs. So yes use references, always avoid copies.

Most of perf issues often lies in the user code or how a library is used ;-)

@mathieucarbou
Copy link

Also I am more concerned about this const issue which might be related to that:

https://github.com/mathieucarbou/ESPAsyncWebServer/blob/main/src/AsyncWebSocket.h#L354

Can you please open a discussion in the right projet so that we stop hijacking ESPhome 's PR ?

Also maybe try to fix the warnings: there are so many 😅

Thanks!

@mathieucarbou
Copy link

mathieucarbou commented Oct 1, 2024

@ewowi : you can upgrade to 3.3.7 : it includes the fix to be able to iterate over clients in a non-const way to call send methods. This limitation was there since previous forks.

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.

4 participants