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

Reply back more than LWS_RECOMMENDED_MIN_HEADER_SPACE #3067

Open
jongsun89 opened this issue Feb 13, 2024 · 2 comments
Open

Reply back more than LWS_RECOMMENDED_MIN_HEADER_SPACE #3067

jongsun89 opened this issue Feb 13, 2024 · 2 comments

Comments

@jongsun89
Copy link

@lws-team Hello Andy
I have seen some lws minimal http examples(server).
All samples looks using LWS_RECOMMENDED_MIN_HEADER_SPACE size buffer for reply back.
But, i want to use more than LWS_RECOMMENDED_MIN_HEADER_SPACE size buffer.
If server wants to reply back for GET method as json type more than LWS_RECOMMENDED_MIN_HEADER_SPACE, is it safe the server to write json data(more than 2048) in lws_writable callback ?
Thanks.

@lws-team
Copy link
Member

It's generally safe to write larger amounts at once, but it's not efficient. Maybe you don't care too much in your use case which is fine then.

lws is told by the POSIX apis when an fd can accept "more" data (ie, is writeable). But it doesn't have a way to know how much data will be accepted by the fd kernelside... if memory is under pressure, that's a very dynamic piece of information controlled by how fast the sent data is being acknowledged vs the tcp window size. When you ask to write n bytes, the libc api copies into kernel memory the whole n bytes, then does the write call kernelside and then really finds out how much it will accept to buffer kernelside on the fd. The rest of the copied-into-kernelside data (not the original of it in userland) is discarded.

If the tcp window for the fd is almost full, it may accept only one mtu's worth, typ 1500 bytes or so. lws will understand this situation and silently allocate and manage a userland buffer to hold the unsent data, and prioritize sending it out, concealing WRITEABLE callbacks until it is all gone.

If n was large, eg, many megabytes, this means we copied megabytes around, in order to send maybe 1500 bytes, it's going to lead to poor performance in both speed and memory usage.

That's why the general advice is to write relatively small things (eg, 2K, 4K, 16K or whatever) each time and ask for a new writeable callback for the rest.

@jongsun89
Copy link
Author

@lws-team
Thanks, I got it.
If i managed buffer size (transmitting bytes at once) optimizingly, lws would free to manage memory for better performance ..

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

No branches or pull requests

2 participants