i have tool that is using proxies to send http requests.
I need help with limiting http requests per second.
Currently i'm using following code, but i know this is wrong method.
So my question is how i can limit maximum 300 connections per second and queue threads that executed method while limit?
Using xNet:
static private string generateToken(ProxyType proxyType, string proxy)
{
string tokenResponse = null;
try
{
WaitForConnection();
using (var request = new HttpRequest())
{
request.AllowAutoRedirect = true;
request.ConnectTimeout = 5000;
request.Reconnect = false;
request.ReconnectDelay = 5000;
request.ReconnectLimit = 1;
request.Proxy = Socks5ProxyClient.Parse(proxy);
HttpResponse httpWebRequestresponse = request.Get("http://example.com");
tokenResponse = httpWebRequestresponse.ToString();
}
}
finally
{
totalConnections--;
}
return tokenResponse;
}
static void WaitForConnection()
{
bool canConnect = false;
do
{
if (totalConnections <= maxconnectionsNum)
{
canConnect = true;
}
} while (canConnect == false);
totalConnections++;
}