Esempio n. 1
0
void HttpClientHandler::ChannelClosed(ChannelHandlerContext& ctx,
		ChannelStateEvent& e)
{
	g_ready_channels.erase(this);
	g_all_channels.erase(this);
	if (!m_connected)
	{
		WARN_LOG(
				"Connect %s:%u failed!", g_options.host.c_str(), g_options.port);
	}
	try_send_request();
	if (m_request_sent)
	{
		g_unanwsered_requests++;
		if (g_unanwsered_requests % 10000 == 0)
		{
			INFO_LOG("Recv %d xres.", g_unanwsered_requests);
		}
	}
}
Esempio n. 2
0
void HttpClientHandler::MessageReceived(ChannelHandlerContext& ctx,
		MessageEvent<HTTPMessage>& e)
{
	g_recved_res++;
	g_ratio_recved_res++;
	m_request_sent = false;
	if (!g_options.keepAlive)
	{
		ctx.GetChannel()->Close();
		g_all_channels.erase(this);
	} else
	{
		g_ready_channels.insert(this);
	}
	try_send_request();
}
Esempio n. 3
0
static int try_send_request()
{
	if (g_limit > 0 && g_sent_count >= g_limit)
	{
		stop_serv();
		return 0;
	}
	uint64 now = get_current_monotonic_millis();

	if (g_tp10ms > 0 && g_count_in_10msperiod >= g_tp10ms)
	{

		if (g_period_task_id == -1)
		{
			if (g_count_in_1speriod >= g_options.tps)
			{
				uint64 delay_1s = 0;
				if (now >= g_1s_transc_start_time + 1000)
				{
					delay_1s = 0;
				} else
				{
					delay_1s = (g_1s_transc_start_time + 1000) - now;
				}
				g_period_task_id = g_serv->GetTimer().ScheduleHeapTask(
						make_fun_runnable(start_1s_period_task, true), delay_1s,
						-1);
			} else
			{
				uint64 delay_10ms = 0;
				if (now >= g_10ms_transc_start_time + 10)
				{
					delay_10ms = 0;
				} else
				{
					delay_10ms = (g_10ms_transc_start_time + 10) - now;
				}
				g_period_task_id = g_serv->GetTimer().ScheduleHeapTask(
						make_fun_runnable(start_10ms_period_task, true),
						delay_10ms, -1);
			}
		}

		return 0;
	}
	if (!g_ready_channels.empty())
	{
		ChannelSet::iterator it = g_ready_channels.begin();
		HttpClientHandler* client = *it;
		client->m_request_sent = true;
		Buffer& data = get_request_data();
		client->m_client->Write(data);
		data.SetReadIndex(0);
		g_ready_channels.erase(it);
		g_count_in_10msperiod++;
		g_sent_count++;
		g_ratio_sent_req++;
		g_count_in_1speriod++;
		return 1;
	} else
	{
		if (g_all_channels.size() < g_options.concurrentConns)
		{
			http_client_connect(g_options.host.c_str(), g_options.port);
		}
	}
	return 0;
}