when trying to flush the send buffer, use select to wait until it is possible instead of using sleep to retry at a given frequency
This commit is contained in:
@ -77,6 +77,14 @@ namespace ix
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// FIXME: on macOS we should configure the pipe to not trigger SIGPIPE
|
||||
// on reads/writes to a closed fd
|
||||
//
|
||||
// The generation of the SIGPIPE signal can be suppressed using the
|
||||
// F_SETNOSIGPIPE fcntl command.
|
||||
//
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -49,22 +49,27 @@ namespace ix
|
||||
return;
|
||||
}
|
||||
|
||||
PollResultType pollResult = select(timeoutSecs, 0);
|
||||
PollResultType pollResult = isReadyToRead(timeoutSecs);
|
||||
|
||||
if (onPollCallback) onPollCallback(pollResult);
|
||||
}
|
||||
|
||||
PollResultType Socket::select(int timeoutSecs, int timeoutMs)
|
||||
PollResultType Socket::select(bool readyToRead, int timeoutSecs, int timeoutMs)
|
||||
{
|
||||
fd_set rfds;
|
||||
fd_set wfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(_sockfd, &rfds);
|
||||
FD_ZERO(&wfds);
|
||||
|
||||
fd_set* fds = (readyToRead) ? &rfds : & wfds;
|
||||
|
||||
FD_SET(_sockfd, fds);
|
||||
|
||||
// File descriptor used to interrupt select when needed
|
||||
int interruptFd = _selectInterrupt->getFd();
|
||||
if (interruptFd != -1)
|
||||
{
|
||||
FD_SET(interruptFd, &rfds);
|
||||
FD_SET(interruptFd, fds);
|
||||
}
|
||||
|
||||
struct timeval timeout;
|
||||
@ -75,7 +80,7 @@ namespace ix
|
||||
int sockfd = _sockfd;
|
||||
int nfds = (std::max)(sockfd, interruptFd);
|
||||
|
||||
int ret = ::select(nfds + 1, &rfds, nullptr, nullptr,
|
||||
int ret = ::select(nfds + 1, &rfds, &wfds, nullptr,
|
||||
(timeoutSecs < 0) ? nullptr : &timeout);
|
||||
|
||||
PollResultType pollResult = PollResultType_ReadyForRead;
|
||||
@ -87,7 +92,7 @@ namespace ix
|
||||
{
|
||||
pollResult = PollResultType_Timeout;
|
||||
}
|
||||
else if (interruptFd != -1 && FD_ISSET(interruptFd, &rfds))
|
||||
else if (interruptFd != -1 && FD_ISSET(interruptFd, fds))
|
||||
{
|
||||
uint64_t value = _selectInterrupt->read();
|
||||
|
||||
@ -100,10 +105,34 @@ namespace ix
|
||||
pollResult = PollResultType_CloseRequest;
|
||||
}
|
||||
}
|
||||
else if (sockfd != -1 && FD_ISSET(sockfd, fds))
|
||||
{
|
||||
if (readyToRead)
|
||||
{
|
||||
pollResult = PollResultType_ReadyForRead;
|
||||
}
|
||||
else
|
||||
{
|
||||
pollResult = PollResultType_ReadyForWrite;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return pollResult;
|
||||
}
|
||||
|
||||
PollResultType Socket::isReadyToRead(int timeoutSecs, int timeoutMs)
|
||||
{
|
||||
bool readyToRead = true;
|
||||
return select(readyToRead, timeoutSecs, timeoutMs);
|
||||
}
|
||||
|
||||
PollResultType Socket::isReadyToWrite(int timeoutSecs, int timeoutMs)
|
||||
{
|
||||
bool readyToRead = false;
|
||||
return select(readyToRead, timeoutSecs, timeoutMs);
|
||||
}
|
||||
|
||||
// Wake up from poll/select by writing to the pipe which is watched by select
|
||||
bool Socket::wakeUpFromPoll(uint8_t wakeUpCode)
|
||||
{
|
||||
@ -231,10 +260,9 @@ namespace ix
|
||||
else if (ret < 0 && (getErrno() == EWOULDBLOCK ||
|
||||
getErrno() == EAGAIN))
|
||||
{
|
||||
// Wait with a timeout until something is ready to read.
|
||||
// Wait with a 1ms timeout until the socket is ready to read.
|
||||
// This way we are not busy looping
|
||||
int res = select(0, 1);
|
||||
if (res < 0 && (errno == EBADF || errno == EINVAL))
|
||||
if (isReadyToRead(0, 1) == PollResultType_Error)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -301,9 +329,12 @@ namespace ix
|
||||
|
||||
if (onProgressCallback) onProgressCallback((int) output.size(), (int) length);
|
||||
|
||||
// Wait with a timeout until something is ready to read.
|
||||
// Wait with a 1ms timeout until the socket is ready to read.
|
||||
// This way we are not busy looping
|
||||
select(0, 1);
|
||||
if (isReadyToRead(0, 1) == PollResultType_Error)
|
||||
{
|
||||
return std::make_pair(false, std::string());
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_pair(true, std::string(output.begin(),
|
||||
|
@ -28,10 +28,11 @@ namespace ix
|
||||
enum PollResultType
|
||||
{
|
||||
PollResultType_ReadyForRead = 0,
|
||||
PollResultType_Timeout = 1,
|
||||
PollResultType_Error = 2,
|
||||
PollResultType_SendRequest = 3,
|
||||
PollResultType_CloseRequest = 4
|
||||
PollResultType_ReadyForWrite = 1,
|
||||
PollResultType_Timeout = 2,
|
||||
PollResultType_Error = 3,
|
||||
PollResultType_SendRequest = 4,
|
||||
PollResultType_CloseRequest = 5
|
||||
};
|
||||
|
||||
class Socket {
|
||||
@ -44,10 +45,14 @@ namespace ix
|
||||
|
||||
void configure();
|
||||
|
||||
PollResultType select(int timeoutSecs, int timeoutMs);
|
||||
virtual void poll(const OnPollCallback& onPollCallback,
|
||||
int timeoutSecs = kDefaultPollTimeout);
|
||||
virtual bool wakeUpFromPoll(uint8_t wakeUpCode);
|
||||
// Functions to check whether there is activity on the socket
|
||||
void poll(const OnPollCallback& onPollCallback,
|
||||
int timeoutSecs = kDefaultPollTimeout);
|
||||
bool wakeUpFromPoll(uint8_t wakeUpCode);
|
||||
|
||||
PollResultType select(bool readyToRead, int timeoutSecs, int timeoutMs);
|
||||
PollResultType isReadyToWrite(int timeoutSecs, int timeoutMs = 0);
|
||||
PollResultType isReadyToRead(int timeoutSecs, int timeoutMs = 0);
|
||||
|
||||
// Virtual methods
|
||||
virtual bool connect(const std::string& url,
|
||||
|
@ -1,18 +1,18 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2012, 2013 <dhbaird@gmail.com>
|
||||
*
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
@ -202,13 +202,20 @@ namespace ix
|
||||
{
|
||||
while (!isSendBufferEmpty() && !_requestInitCancellation)
|
||||
{
|
||||
sendOnSocket();
|
||||
|
||||
// Sleep 10ms between each send so that we dont busy loop
|
||||
// A better strategy would be to select on the socket to
|
||||
// check whether we can write to it without blocking
|
||||
std::chrono::duration<double, std::micro> duration(10);
|
||||
std::this_thread::sleep_for(duration);
|
||||
// Wait with a 10ms timeout until the socket is ready to write.
|
||||
// This way we are not busy looping
|
||||
int timeoutMs = 10;
|
||||
PollResultType result = _socket->isReadyToWrite(0, 10);
|
||||
if (result == PollResultType_Error)
|
||||
{
|
||||
_socket->close();
|
||||
setReadyState(CLOSED);
|
||||
break;
|
||||
}
|
||||
else if (result == PollResultType_ReadyForWrite)
|
||||
{
|
||||
sendOnSocket();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (pollResult == PollResultType_ReadyForRead)
|
||||
|
Reference in New Issue
Block a user