Exemplo n.º 1
0
bool NwConnection::setRateLimit(size_t rr, size_t rb, size_t wr, size_t wb, float sec)
{
	if (!mBackend.backend)
	{
		return false;
	}

	if (mBackend.rate)
	{
		bufferevent_set_rate_limit(mBackend.backend, 0);
		ev_token_bucket_cfg_free(mBackend.rate);
	}

	timeval tv;

	tv.tv_sec = static_cast<long>(sec);
	tv.tv_usec = static_cast<long>((sec - tv.tv_sec) * 1000000);

	mBackend.rate = ev_token_bucket_cfg_new(rr, rb, wr, wb, &tv);

	if (!mBackend.rate)
	{
		return false;
	}

	bufferevent_set_rate_limit(mBackend.backend, mBackend.rate);
	return true;
}
Exemplo n.º 2
0
static evhtp_res
test_fini(evhtp_request_t * r, void * arg) {
    struct ev_token_bucket_cfg * tcfg = arg;

    if (tcfg) {
        ev_token_bucket_cfg_free(tcfg);
    }

    return EVHTP_RES_OK;
}
Exemplo n.º 3
0
void NwConnection::shutdown()
{
	if (mBackend.frontend)
	{
		bufferevent_free(mBackend.frontend);
		mBackend.frontend = 0;
		mBackend.backend = 0;
	}

	if (mBackend.rate)
	{
		ev_token_bucket_cfg_free(mBackend.rate);
		mBackend.rate = 0;
	}

	mFilters.clear();
}
Exemplo n.º 4
0
bool CNetService::Shutdown( void ) {
	
	DXM_NDC("NetService::Shutdown");

	// set running flag 

	boost::unique_lock<boost::shared_mutex> lock(running_mutex_);
	if(!is_running_) 
	{
		DXM_COMPONENT_LOG( DXM_COMPONENT_LOG_LEVEL_ERROR, "net service is shutdowned.");
		return false;
	}
	is_running_ = false;


	// wait for all net_shut_down
	net_shutdown_event_();
	DXM_COMPONENT_LOG( DXM_COMPONENT_LOG_LEVEL_TRACE, "shutdown_signal_.num_slots: %d.", net_shutdown_event_.NumListeners());
	while( net_shutdown_event_.NumListeners() )
	{
		DXM_SLEEP(2000);
		DXM_COMPONENT_LOG( DXM_COMPONENT_LOG_LEVEL_TRACE, "shutdown_signal_.num_slots: %d.", net_shutdown_event_.NumListeners());
	}

	// leave loop;
	event_base_loopexit(ev_base_, 0);
	pthread_join(loop_thread_, 0);
	DXM_COMPONENT_LOG( DXM_COMPONENT_LOG_LEVEL_TRACE, "waiting for thread exit.");

	// free libevent object;
	event_free(ev_);
	event_base_free(ev_base_);
	ev_token_bucket_cfg_free(ev_t_bucket_cfg_);
	event_config_free(ev_config_);
	DXM_COMPONENT_LOG( DXM_COMPONENT_LOG_LEVEL_TRACE, "free event environment ok.");
	DXM_COMPONENT_LOG( DXM_COMPONENT_LOG_LEVEL_INFO, "Shutdown Finish.");
	return true;
}
Exemplo n.º 5
0
bool NwConnection::setRateLimit(size_t rr, size_t rb, size_t wr, size_t wb)
{
	if (!mBackend.backend)
	{
		return false;
	}

	if (mBackend.rate)
	{
		bufferevent_set_rate_limit(mBackend.backend, 0);
		ev_token_bucket_cfg_free(mBackend.rate);
	}

	mBackend.rate = ev_token_bucket_cfg_new(rr, rb, wr, wb, 0);

	if (!mBackend.rate)
	{
		return false;
	}

	bufferevent_set_rate_limit(mBackend.backend, mBackend.rate);
	return true;
}
Exemplo n.º 6
0
int
main ()
{
    int sockfd;
    struct bufferevent *p_event;
    struct sockaddr_in addr;
    int cc;

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);

    if (inet_pton(AF_INET, SVRADDR, &addr.sin_addr) <= 0) {
        printf("inet_pton");
        exit(1);
    }

    if ((p_base = event_base_new()) == NULL) {
        printf("event_base_new ");
        return 1;
    }

    /* we do not need invoke socket function to create socket */
    if ((p_event = bufferevent_socket_new(p_base, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS)) == NULL) {
        printf("bufferevent_socket_new ");
        return 1;
    }

    /* client actually connecte to server at this time. */
    if ((sockfd = bufferevent_socket_connect(p_event, (struct sockaddr *) &addr,
                                             sizeof(addr))) < 0) {
        printf("bufferevent_socket_connect ");
        return 1;
    }

    /* EV_WRITE is default enabled, EV_READ is default disabled */
    /* So If we disable READ, evbuffer callback will not be added to base (read and write) */
    bufferevent_setcb(p_event, buff_input_cb, buff_output_cb, NULL, p_base);
    bufferevent_enable(p_event, EV_WRITE | EV_READ);

    /* you should not free cfg immediately, after call bufferevent_set_rate_limit. */
    /* you should not set the burst rate little than average rate */
    struct ev_token_bucket_cfg *cfg = ev_token_bucket_cfg_new (1, 1, 10240, 10240, NULL);
    assert (NULL != cfg);

    /* If Server send N bytes, every time, 
        * read callback will be invoked when 2 bytes read to evbuffer.
        */
    cc = bufferevent_set_rate_limit (p_event, cfg);
    assert (cc == 0);

#if 0
    struct bufferevent *bev=bufferevent_socket_new (p_base, bufferevent_getfd(p_event), BEV_OPT_DEFER_CALLBACKS);
    assert (NULL != bev);
    bufferevent_set_rate_limit (bev, cfg);
    bufferevent_setcb(bev, buff_input_cb, NULL, NULL, NULL);
    bufferevent_enable(bev, EV_WRITE | EV_READ);
#endif
    struct event *ev = event_new (p_base, SIGINT, EV_SIGNAL | EV_PERSIST, sigint, NULL);
    event_add (ev, NULL);
    
    event_base_dispatch(p_base);

    ev_token_bucket_cfg_free (cfg);

    return 0;
}
Exemplo n.º 7
0
int
main ()
{
    int sockfd;
    struct bufferevent *p_event;
    struct sockaddr_in addr;
    int cc;

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);

    if (inet_pton(AF_INET, SVRADDR, &addr.sin_addr) <= 0) {
        printf("inet_pton");
        exit(1);
    }

    if ((p_base = event_base_new()) == NULL) {
        printf("event_base_new ");
        return 1;
    }

    /* we do not need invoke socket function to create socket */
    if ((p_event = bufferevent_socket_new(p_base, -1, BEV_OPT_CLOSE_ON_FREE)) == NULL) {
        printf("bufferevent_socket_new ");
        return 1;
    }

    /* client actually connecte to server at this time. */
    if ((sockfd = bufferevent_socket_connect(p_event, (struct sockaddr *) &addr,
                                             sizeof(addr))) < 0) {
        printf("bufferevent_socket_connect ");
        return 1;
    }

    /* EV_WRITE is default enabled, EV_READ is default disabled */
    /* So If we disable READ, evbuffer callback will not be added to base (read and write) */
    bufferevent_setcb(p_event, buff_input_cb, NULL, NULL, p_base);
    bufferevent_enable(p_event, EV_WRITE | EV_READ);

    /* you should not free cfg immediately, after call bufferevent_set_rate_limit. */
    /* you should not set the burst rate little than average rate */
    struct ev_token_bucket_cfg *cfg = ev_token_bucket_cfg_new (EV_RATE_LIMIT_MAX, EV_RATE_LIMIT_MAX, 1, 1, NULL);
    assert (NULL != cfg);

    /* If Server send N bytes, every time, 
        * read callback will be invoked when 2 bytes read to evbuffer.
        */
    cc = bufferevent_set_rate_limit (p_event, cfg);
//    assert (cc == 0);

    /* this is the defauly value EV_SSIZE_MAX, 0x7FFFFFFF */
    ev_ssize_t size = bufferevent_get_read_limit (p_event);
    printf ("bufferevent read limit 0x%x\n", size);   /* 2, average rate */
    size = bufferevent_get_write_limit (p_event);
    printf ("bufferevent write limit 0x%x\n", size);  /* 1, average rate */

    event_base_dispatch(p_base);

    ev_token_bucket_cfg_free (cfg);

    return 0;
}