Commit b9f4ba50 authored by Seva Oparin's avatar Seva Oparin Committed by Facebook Github Bot

Support SO_RCVBUF, SO_SNDBUF, and SO_BUSY_POLL in AsyncUdpSocket

Summary:
Add API to AsyncUdpSocket to support three options on the socket.

- SO_RCVBUF and SO_SNDBUG options are responsible for the size of the queues that contain received and sent messages, respectively.
- SO_BUSY_POLL sets the approximate time in microseconds to busy poll on a blocking receive when there is no data

Details in socket man: http://man7.org/linux/man-pages/man7/socket.7.html

Reviewed By: yfeldblum

Differential Revision: D7372123

fbshipit-source-id: 5fd92489cbe86879d63df208c3821febdadb0020
parent 6b684431
...@@ -95,6 +95,58 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) { ...@@ -95,6 +95,58 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
} }
} }
if (busyPollUs_ > 0) {
#ifdef SO_BUSY_POLL
// Set busy_poll time in microseconds on the socket.
// It sets how long socket will be in busy_poll mode when no event occurs.
int value = busyPollUs_;
if (setsockopt(socket,
SOL_SOCKET,
SO_BUSY_POLL,
&value,
sizeof(value)) != 0) {
throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
"failed to set SO_BUSY_POLL on the socket",
errno);
}
#else /* SO_BUSY_POLL is not supported*/
throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
"SO_BUSY_POLL is not supported",
errno);
#endif
}
if (rcvBuf_ > 0) {
// Set the size of the buffer for the received messages in rx_queues.
int value = rcvBuf_;
if (setsockopt(socket,
SOL_SOCKET,
SO_RCVBUF,
&value,
sizeof(value)) != 0) {
throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
"failed to set SO_RCVBUF on the socket",
errno);
}
}
if (sndBuf_ > 0) {
// Set the size of the buffer for the sent messages in tx_queues.
int value = rcvBuf_;
if (setsockopt(socket,
SOL_SOCKET,
SO_SNDBUF,
&value,
sizeof(value)) != 0) {
throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
"failed to set SO_SNDBUF on the socket",
errno);
}
}
// If we're using IPv6, make sure we don't accept V4-mapped connections // If we're using IPv6, make sure we don't accept V4-mapped connections
if (address.getFamily() == AF_INET6) { if (address.getFamily() == AF_INET6) {
int flag = 1; int flag = 1;
......
...@@ -158,6 +158,28 @@ class AsyncUDPSocket : public EventHandler { ...@@ -158,6 +158,28 @@ class AsyncUDPSocket : public EventHandler {
reuseAddr_ = reuseAddr; reuseAddr_ = reuseAddr;
} }
/**
* Set SO_RCVBUF option on the socket, if not zero. Default is zero.
*/
virtual void setRcvBuf(int rcvBuf) {
rcvBuf_ = rcvBuf;
}
/**
* Set SO_SNDBUG option on the socket, if not zero. Default is zero.
*/
virtual void setSndBuf(int sndBuf) {
sndBuf_ = sndBuf;
}
/**
* Set SO_BUSY_POLL option on the socket, if not zero. Default is zero.
* Caution! The feature is not available on Apple's systems.
*/
virtual void setBusyPoll(int busyPollUs) {
busyPollUs_ = busyPollUs;
}
EventBase* getEventBase() const { EventBase* getEventBase() const {
return eventBase_; return eventBase_;
} }
...@@ -203,6 +225,9 @@ class AsyncUDPSocket : public EventHandler { ...@@ -203,6 +225,9 @@ class AsyncUDPSocket : public EventHandler {
bool reuseAddr_{true}; bool reuseAddr_{true};
bool reusePort_{false}; bool reusePort_{false};
int rcvBuf_{0};
int sndBuf_{0};
int busyPollUs_{0};
}; };
} // namespace folly } // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment