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 setting rcvbuf buffer size on a socket #907

Merged
merged 1 commit into from
Dec 6, 2023

Conversation

fadushin
Copy link
Collaborator

This PR allows users to set the buffer size on receive operations via the socket:setopt/3 function, using the {otp, rcvbuf} configuration option.

This PR addresses Issue #896

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@fadushin fadushin force-pushed the set-recv-buffer-size branch 4 times, most recently from d196d2a to ea77a28 Compare October 31, 2023 02:23
@fadushin fadushin marked this pull request as ready for review October 31, 2023 02:24
CHANGELOG.md Outdated Show resolved Hide resolved
doc/src/programmers-guide.md Outdated Show resolved Hide resolved
src/libAtomVM/otp_socket.c Show resolved Hide resolved
src/libAtomVM/otp_socket.c Show resolved Hide resolved
src/libAtomVM/otp_socket.c Show resolved Hide resolved
src/libAtomVM/otp_socket.c Show resolved Hide resolved
src/libAtomVM/otp_socket.c Show resolved Hide resolved
src/libAtomVM/otp_socket.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

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

Let's also do a rebease before going for merge.

ssize_t buffer_size = len == 0 ? (ssize_t) res : MIN((size_t) res, len);
// user-supplied len has higher precedence than the default buffer size, but we also
// want the configured default buffer size to be a lower bound on anything we peek
ssize_t buffer_size = len == 0 ? MIN((size_t) res, rsrc_obj->buf_size) : MIN((size_t) res, len);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I made a change to the logic here. @pguyot and @bettio please review

Copy link
Collaborator

Choose a reason for hiding this comment

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

Aren't we doing MIN(len == 0 ? rsrc_obj->buf_size : len, res) ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This change is also a good chance to fix a warning we have:

AtomVM/src/libAtomVM/otp_socket.c:1665:42: warning: operand of ‘?:’ changes signedness from ‘long int’ to ‘long unsigned int’ due to unsignedness of other operand [-Wsign-compare]
 1665 |         ssize_t buffer_size = len == 0 ? (ssize_t) res : MIN((size_t) res, len);
      |                                          ^~~~~~~~~~~~~

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Aren't we doing MIN(len == 0 ? rsrc_obj->buf_size : len, res) ?

I believe these are equivalent, right? (assuming MIN is commutative) Changed to your version, with changes to address any compiler warnings.

Scanning dependencies of target libAtomVMLinux-x86_64
[ 28%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/mapped_file.c.o
[ 28%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/platform_defaultatoms.c.o
[ 28%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/smp.c.o
[ 28%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/platform_nifs.c.o
[ 28%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/socket_driver.c.o
[ 28%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/otp_socket_platform.c.o
[ 29%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/sys.c.o
[ 29%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/__/__/__/libAtomVM/inet.c.o
[ 29%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/__/__/__/libAtomVM/otp_net.c.o
[ 29%] Building C object src/platforms/generic_unix/lib/CMakeFiles/libAtomVMLinux-x86_64.dir/__/__/__/libAtomVM/otp_socket.c.o
[ 29%] Linking C static library liblibAtomVMLinux-x86_64.a
[ 29%] Built target libAtomVMLinux-x86_64

@fadushin fadushin marked this pull request as draft November 3, 2023 11:02
@fadushin fadushin force-pushed the set-recv-buffer-size branch 11 times, most recently from 369f53c to 0bfe2af Compare November 14, 2023 01:58
@fadushin fadushin marked this pull request as ready for review November 14, 2023 02:47
@fadushin fadushin requested a review from pguyot November 14, 2023 02:47
@fadushin fadushin requested a review from bettio November 14, 2023 02:47
tests/libs/estdlib/test_tcp_socket.erl Show resolved Hide resolved
tests/libs/estdlib/test_tcp_socket.erl Show resolved Hide resolved
ssize_t buffer_size = len == 0 ? (ssize_t) res : MIN((size_t) res, len);
// user-supplied len has higher precedence than the default buffer size, but we also
// want the configured default buffer size to be a lower bound on anything we peek
ssize_t buffer_size = len == 0 ? MIN((size_t) res, rsrc_obj->buf_size) : MIN((size_t) res, len);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Aren't we doing MIN(len == 0 ? rsrc_obj->buf_size : len, res) ?

src/libAtomVM/otp_socket.c Show resolved Hide resolved
@@ -1804,12 +1804,14 @@ Currently, the following options are supported:
|------------|--------------|-------------|
| `{socket, reuseaddr}` | `boolean()` | Sets `SO_REUSEADDR` on the socket. |
| `{socket, linger}` | `#{onoff => boolean(), linger => non_neg_integer()}` | Sets `SO_LINGER` on the socekt. |
| `{otp, rcvbuf}` | `non_neg_integer()` | Sets the default buffer size (in bytes) on receive calls. This value is only used if the `Length` parameter of the `socket:recv` family of functions has the value `0`; otherwise, the specified non-zero length in the `socket:recv` takes precendence. |
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we have 0 for this value?
OTP specifies default | integer()>0

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wonder what the meaning of default is? Would that be for the case in which you set the buffer size to be some value, and then want to set it back to default at a later time?

I'd say in that case we either:

  • defer implementing that use case (not sure how useful it is) with a TODO
  • Add support for setting it back to default, if desired (would be easy to do)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added note to table and TODO in the code. Not sure why CI did not kick off.

Signed-off-by: Fred Dushin <fred@dushin.net>
@bettio bettio changed the title Add support for setting rcvbuf buffer size on a socket. Add support for setting rcvbuf buffer size on a socket Dec 6, 2023
@bettio bettio merged commit 6abb424 into atomvm:master Dec 6, 2023
84 checks passed
@fadushin fadushin deleted the set-recv-buffer-size branch December 7, 2023 16:43
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.

None yet

3 participants