SendQ bugfixes

Fix DoWrite running on errored sockets
Add testnet module for sendq and shutdown testing
Prevent DoWrite from trying to write when writes are blocking

git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11768 e03df62e-2008-0410-955e-edbf42e46eb7
This commit is contained in:
danieldg 2009-09-27 00:22:29 +00:00
parent 7c1352df0c
commit 934d9a6a18
4 changed files with 98 additions and 20 deletions

View File

@ -1684,6 +1684,12 @@
# To use, SWHOIS must be in one of your oper class blocks.
#<module name="m_swhois.so">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Test module: enable this to create a command useful in testing
# flood control. To avoid accidental use on live networks, the server
# name must contain ".test" to load the module
#<module name="m_testnet.so">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Timed bans module: Adds timed channel bans and the /TBAN command
#<module name="m_timedbans.so">

View File

@ -215,6 +215,11 @@ void StreamSocket::DoWrite()
{
if (sendq.empty())
return;
if (!error.empty() || fd < 0 || fd == INT_MAX)
{
ServerInstance->Logs->Log("SOCKET", DEBUG, "DoWrite on errored or closed socket");
return;
}
if (IOHook)
{
@ -272,11 +277,13 @@ void StreamSocket::DoWrite()
}
else
{
bool again = true;
while (again)
// don't even try if we are known to be blocking
if (GetEventMask() & FD_WRITE_WILL_BLOCK)
return;
// start out optimistic - we won't need to write any more
int eventChange = FD_WANT_EDGE_WRITE;
while (sendq_len && eventChange == FD_WANT_EDGE_WRITE)
{
again = false;
// Prepare a writev() call to write all buffers efficiently
int bufcount = sendq.size();
@ -284,14 +291,15 @@ void StreamSocket::DoWrite()
if (bufcount > IOV_MAX)
{
bufcount = IOV_MAX;
again = true;
}
int rv_max = 0;
iovec* iovecs = new iovec[bufcount];
for(int i=0; i < bufcount; i++)
{
iovecs[i].iov_base = const_cast<char*>(sendq[i].data());
iovecs[i].iov_len = sendq[i].length();
rv_max += sendq[i].length();
}
int rv = writev(fd, iovecs, bufcount);
delete[] iovecs;
@ -310,7 +318,7 @@ void StreamSocket::DoWrite()
while (rv > 0 && !sendq.empty())
{
std::string& front = sendq.front();
if (front.length() < (size_t)rv)
if (front.length() <= (size_t)rv)
{
// this string got fully written out
rv -= front.length();
@ -323,6 +331,11 @@ void StreamSocket::DoWrite()
rv = 0;
}
}
if (rv < rv_max)
{
// it's going to block now
eventChange = FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
}
}
else if (rv == 0)
{
@ -330,11 +343,11 @@ void StreamSocket::DoWrite()
}
else if (errno == EAGAIN)
{
again = false;
eventChange = FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
}
else if (errno == EINTR)
{
again = true;
// restart interrupted syscall
}
else
{
@ -346,15 +359,9 @@ void StreamSocket::DoWrite()
// error - kill all events
ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
}
else if (sendq_len)
{
// writes have blocked, we can use FAST_WRITE to find when they unblock
ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
}
else
{
// writes are done, we can use EDGE_WRITE to stop asking for write
ServerInstance->SE->ChangeEventMask(this, FD_WANT_EDGE_WRITE);
ServerInstance->SE->ChangeEventMask(this, eventChange);
}
}
}

66
src/modules/m_testnet.cpp Normal file
View File

@ -0,0 +1,66 @@
/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
* InspIRCd: (C) 2002-2009 InspIRCd Development Team
* See: http://wiki.inspircd.org/Credits
*
* This program is free but copyrighted software; see
* the file COPYING for details.
*
* ---------------------------------------------------
*/
/* $ModDesc: Provides a module for testing the server while linked in a network */
#include "inspircd.h"
class CommandTest : public Command
{
public:
CommandTest(Module* parent) : Command(parent, "TEST", 1)
{
syntax = "<action> <parameters>";
}
CmdResult Handle(const std::vector<std::string> &parameters, User *user)
{
if (parameters[0] == "flood")
{
unsigned int count = parameters.size() > 1 ? atoi(parameters[1].c_str()) : 100;
std::string line = parameters.size() > 2 ? parameters[2] : ":z.z NOTICE !flood :Flood text";
for(unsigned int i=0; i < count; i++)
user->Write(line);
}
else if (parameters[0] == "freeze")
{
user->Penalty += 100;
}
else if (parameters[0] == "shutdown")
{
int i = parameters.size() > 1 ? atoi(parameters[1].c_str()) : 2;
ServerInstance->SE->Shutdown(user->GetFd(), i);
}
return CMD_SUCCESS;
}
};
class ModuleTest : public Module
{
CommandTest cmd;
public:
ModuleTest() : cmd(this)
{
if (!strstr(ServerInstance->Config->ServerName, ".test"))
throw ModuleException("Don't load modules without reading their descriptions!");
ServerInstance->AddCommand(&cmd);
}
Version GetVersion()
{
return Version("Provides a module for testing the server while linked in a network", VF_VENDOR|VF_OPTCOMMON);
}
};
MODULE_INIT(ModuleTest)

View File

@ -575,13 +575,12 @@ void User::AddWriteBuf(const std::string &data)
if (!quitting && MyClass && getSendQSize() + data.length() > MyClass->GetSendqHardMax() && !HasPrivPermission("users/flood/increased-buffers"))
{
/*
* Fix by brain - Set the error text BEFORE calling, because
* if we dont it'll recursively call here over and over again trying
* to repeatedly add the text to the sendq!
* Quit the user FIRST, because otherwise we could recurse
* here and hit the same limit.
*/
ServerInstance->Users->QuitUser(this, "SendQ exceeded");
ServerInstance->SNO->WriteToSnoMask('a', "User %s SendQ of %lu exceeds connect class maximum of %lu",
nick.c_str(), (unsigned long)getSendQSize() + data.length(), MyClass->GetSendqHardMax());
ServerInstance->SNO->WriteToSnoMask('a', "User %s SendQ exceeds connect class maximum of %lu",
nick.c_str(), MyClass->GetSendqHardMax());
return;
}