Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Unified Diff: net/base/tcp_listen_socket.cc

Issue 10389007: Change TCPListenSocket::SendInternal to use a non-blocking implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/tcp_listen_socket.h ('k') | net/base/tcp_listen_socket_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/tcp_listen_socket.cc
===================================================================
--- net/base/tcp_listen_socket.cc (revision 135347)
+++ net/base/tcp_listen_socket.cc (working copy)
@@ -17,6 +17,7 @@
#include "net/base/net_errors.h"
#endif
+#include "base/bind.h"
#include "base/eintr_wrapper.h"
#include "base/sys_byteorder.h"
#include "base/threading/platform_thread.h"
@@ -25,6 +26,7 @@
#if defined(OS_WIN)
typedef int socklen_t;
+#include "net/base/winsock_init.h"
#endif // defined(OS_WIN)
namespace net {
@@ -32,7 +34,34 @@
namespace {
const int kReadBufSize = 4096;
+const int kMaxSendBufSize = 1024*1024*5; // 5MB
+const net::BackoffEntry::Policy kSendDataBackoffPolicy = {
+ // Number of initial errors (in sequence) to ignore before applying
+ // exponential back-off rules.
+ 0,
+
+ // Initial delay for exponential back-off in ms.
+ 100,
mmenke 2012/05/08 21:23:38 If we're sending megabytes locally, 100 millisecon
Marshall 2012/05/08 21:44:51 Reduced to 25ms.
+
+ // Factor by which the waiting time will be multiplied.
+ 2,
+
+ // Fuzzing percentage. ex: 10% will spread requests randomly
+ // between 90%-100% of the calculated time.
+ 0,
+
+ // Maximum amount of time we are willing to delay our request in ms.
+ 500,
+
+ // Time to keep an entry from being discarded even when it
+ // has no significant state, -1 to never discard.
+ -1,
+
+ // Don't use initial delay unless the last request was an error.
+ false,
+};
+
} // namespace
#if defined(OS_WIN)
@@ -75,7 +104,9 @@
: ListenSocket(del),
socket_(s),
reads_paused_(false),
- has_pending_reads_(false) {
+ has_pending_reads_(false),
+ send_data_error_(false),
+ send_data_backoff_(&kSendDataBackoffPolicy) {
#if defined(OS_WIN)
socket_event_ = WSACreateEvent();
// TODO(ibrar): error handling in case of socket_event_ == WSA_INVALID_EVENT
@@ -96,6 +127,10 @@
}
SOCKET TCPListenSocket::CreateAndBind(const std::string& ip, int port) {
+#if defined(OS_WIN)
+ EnsureWinsockInit();
+#endif
+
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s != kInvalidSocket) {
#if defined(OS_POSIX)
@@ -132,33 +167,26 @@
}
void TCPListenSocket::SendInternal(const char* bytes, int len) {
- char* send_buf = const_cast<char *>(bytes);
- int len_left = len;
- while (true) {
- int sent = HANDLE_EINTR(send(socket_, send_buf, len_left, 0));
- if (sent == len_left) { // A shortcut to avoid extraneous checks.
- break;
- }
- if (sent == kSocketError) {
-#if defined(OS_WIN)
- if (WSAGetLastError() != WSAEWOULDBLOCK) {
- LOG(ERROR) << "send failed: WSAGetLastError()==" << WSAGetLastError();
-#elif defined(OS_POSIX)
- if (errno != EWOULDBLOCK && errno != EAGAIN) {
- LOG(ERROR) << "send failed: errno==" << errno;
-#endif
- break;
- }
- // Otherwise we would block, and now we have to wait for a retry.
- // Fall through to PlatformThread::YieldCurrentThread()
- } else {
- // sent != len_left according to the shortcut above.
- // Shift the buffer start and send the remainder after a short while.
- send_buf += sent;
- len_left -= sent;
- }
- base::PlatformThread::YieldCurrentThread();
+ DCHECK(bytes);
+ DCHECK_GT(len, 0);
+
+ if (send_data_error_)
+ return;
+
+ // Add all bytes to the send buffer.
+ if (send_data_.empty()) {
+ send_data_.assign(bytes, len);
+ } else if (send_data_.size() > kMaxSendBufSize) {
mmenke 2012/05/08 20:51:43 Should this be send_data_.size() + len? We'd stil
Marshall 2012/05/08 21:44:51 Done.
+ // Too much of a backup, stop trying to add more data.
+ LOG(ERROR) << "send failed: buffer overrun";
+ send_data_error_ = true;
mmenke 2012/05/08 20:51:43 send_data_.clear()?
Marshall 2012/05/08 21:44:51 Done.
+ return;
+ } else {
+ send_data_.append(bytes, len);
}
+
+ if (!send_data_backoff_.ShouldRejectRequest())
+ SendData();
}
void TCPListenSocket::Listen() {
@@ -319,4 +347,53 @@
#endif
+void TCPListenSocket::SendData() {
+ // Another send call may have already emptied the buffer.
+ if (send_data_.empty())
+ return;
+
+ int len_left = static_cast<int>(send_data_.length());
+ int sent = HANDLE_EINTR(send(socket_, send_data_.c_str(), len_left, 0));
+ if (sent == len_left) {
+ // All data has been sent.
+ send_data_.clear();
+
+ // Successfully sending all data clears the backoff state.
+ send_data_backoff_.Reset();
+ return;
+ }
+
+ if (sent == kSocketError) {
+#if defined(OS_WIN)
+ if (WSAGetLastError() != WSAEWOULDBLOCK) {
+ LOG(ERROR) << "send failed: WSAGetLastError()==" << WSAGetLastError();
+#elif defined(OS_POSIX)
+ if (errno != EWOULDBLOCK && errno != EAGAIN) {
+ LOG(ERROR) << "send failed: errno==" << errno;
+#endif
+ // Don't try to re-send data after a socket error.
+ send_data_.clear();
+ send_data_error_ = true;
+ return;
+ }
+
+ // Blocking is considered a failure.
+ send_data_backoff_.InformOfRequest(false);
+ }
+
+ if (sent > 0) {
+ // Remove the sent bytes from the buffer.
+ send_data_ = send_data_.substr(sent, len_left - sent);
+
+ // Sending any data is considered a success.
+ send_data_backoff_.InformOfRequest(true);
+ }
+
+ if (!send_timer_.IsRunning()) {
+ // Schedule a timer to continue sending data asynchronously.
+ send_timer_.Start(FROM_HERE, send_data_backoff_.GetTimeUntilRelease(),
+ this, &TCPListenSocket::SendData);
+ }
+}
+
} // namespace net
« no previous file with comments | « net/base/tcp_listen_socket.h ('k') | net/base/tcp_listen_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698