00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 Beesnest is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcLoadController.h: interface for the CdcLoadController class. */ 00021 00022 #ifndef __CDC_LOAD_CONTROLLER 00023 #define __CDC_LOAD_CONTROLLER 00024 00025 #include "DcGlobals.h" 00026 #include _GET_INCLUDE_PATH (DcSocket.h) 00027 #include _GET_INCLUDE_PATH (DcMutex.h) 00028 #include "DcParameters.h" 00029 00030 00031 /** 00032 * This is a static class to control the load of requests on the server. 00033 * It controls two parameters: The number of open threads, and the number of 00034 * open threads per one client (IP address). 00035 * If the global limit is reached, the main thread will sleep for some time. 00036 * This will let the rest of the (lower priority) threads a chance to finish 00037 * their job under DoS attack. If one of the limits reached, the class send 503 00038 * error message to the client and delete the socket. 00039 * Parameters are in the server configuration file under: 00040 * <b>Total-Threads-Limit</b>, <b>Client-Threads-Limit</b> and 00041 * <b>Overload-Sleep</b>. 00042 */ 00043 class CdcLoadController 00044 { 00045 private: 00046 00047 /** Stores the total threads limit. */ 00048 static int nThreadsLimit; 00049 /** Stores the one client threads limit. */ 00050 static int nClientThreadLimit; 00051 /** Time [mSec] to sleep when total number of threads is reached. 00052 Letting the server some time to finish what it is doing. */ 00053 static int nOverloadSleep; 00054 /** Stores the total number of currently open threads. */ 00055 static int nNoOfThreads; 00056 00057 typedef map <CdcString, int> t_clients_map; 00058 /** Map of number of threads per client. */ 00059 static t_clients_map mClients; 00060 00061 /** Mutex for this static class. */ 00062 static CdcMutex oMutex; 00063 00064 public: 00065 00066 /** 00067 * Load the parameters from parameters file. 00068 */ 00069 static void Init (); 00070 00071 /** 00072 * This function checks if it can add new thread to the open threads of the 00073 * server. It gets a pointer to CdcSocket object. If the number of opened 00074 * threads is lower then the limit and the numbers of thread for this client 00075 * is lower then the limit, it increase both values and return True. If one 00076 * of the values is at the limit, it sends code 503 to the client and 00077 * returns False. 00078 */ 00079 static bool AddThread (CdcSocket* psock); 00080 00081 /** 00082 * This function decreases one from the total number of open threads and 00083 * from open threads for this client. if the total number of open threads is 00084 * zero (we have some time), it clears the clients map. 00085 */ 00086 static void ThreadDone (CdcSocket* psock); 00087 }; 00088 00089 #endif /* __CDC_LOAD_CONTROLLER */ 00090