Merge pull request #6833 from rjshade/sockopts

Adds functions to set snd/rcv socket buffer sizes.
pull/6572/head^2
Jan Tattermusch 9 years ago
commit 4f527d51f9
  1. 10
      src/core/lib/iomgr/socket_utils_common_posix.c
  2. 8
      src/core/lib/iomgr/socket_utils_posix.h
  3. 14
      src/core/lib/iomgr/udp_server.c
  4. 9
      test/core/iomgr/fd_posix_test.c

@ -111,6 +111,16 @@ int grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd) {
#endif
}
int grpc_set_socket_sndbuf(int fd, int buffer_size_bytes) {
return 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buffer_size_bytes,
sizeof(buffer_size_bytes));
}
int grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes) {
return 0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buffer_size_bytes,
sizeof(buffer_size_bytes));
}
/* set a socket to close on exec */
int grpc_set_socket_cloexec(int fd, int close_on_exec) {
int oldflags = fcntl(fd, F_GETFD, 0);

@ -78,6 +78,14 @@ int grpc_set_socket_ip_pktinfo_if_possible(int fd);
If IPV6_RECVPKTINFO is not available, returns 1. */
int grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd);
/* Tries to set the socket's send buffer to given size.
Returns 1 on success, 0 on failure. */
int grpc_set_socket_sndbuf(int fd, int buffer_size_bytes);
/* Tries to set the socket's receive buffer to given size.
Returns 1 on success, 0 on failure. */
int grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes);
/* An enum to keep track of IPv4/IPv6 socket modes.
Currently, this information is only used when a socket is first created, but

@ -210,6 +210,8 @@ static int prepare_socket(int fd, const struct sockaddr *addr,
size_t addr_len) {
struct sockaddr_storage sockname_temp;
socklen_t sockname_len;
/* Set send/receive socket buffers to 1 MB */
int buffer_size_bytes = 1024 * 1024;
if (fd < 0) {
goto error;
@ -239,6 +241,18 @@ static int prepare_socket(int fd, const struct sockaddr *addr,
goto error;
}
if (!grpc_set_socket_sndbuf(fd, buffer_size_bytes)) {
gpr_log(GPR_ERROR, "Failed to set send buffer size to %d bytes",
buf_size_bytes);
goto error;
}
if (!grpc_set_socket_rcvbuf(fd, buffer_size_bytes)) {
gpr_log(GPR_ERROR, "Failed to set receive buffer size to %d bytes",
buf_size_bytes);
goto error;
}
return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
error:

@ -52,6 +52,7 @@
#include "src/core/lib/iomgr/ev_posix.h"
#include "src/core/lib/iomgr/iomgr.h"
#include "src/core/lib/iomgr/socket_utils_posix.h"
#include "test/core/util/test_config.h"
static gpr_mu *g_mu;
@ -68,17 +69,15 @@ static void create_test_socket(int port, int *socket_fd,
struct sockaddr_in *sin) {
int fd;
int one = 1;
int buf_size = BUF_SIZE;
int buffer_size_bytes = BUF_SIZE;
int flags;
fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
/* Reset the size of socket send buffer to the minimal value to facilitate
buffer filling up and triggering notify_on_write */
GPR_ASSERT(
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf_size, sizeof(buf_size)) != -1);
GPR_ASSERT(
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof(buf_size)) != -1);
GPR_ASSERT(grpc_set_socket_sndbuf(fd, buffer_size_bytes));
GPR_ASSERT(grpc_set_socket_rcvbuf(fd, buffer_size_bytes));
/* Make fd non-blocking */
flags = fcntl(fd, F_GETFL, 0);
GPR_ASSERT(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0);

Loading…
Cancel
Save