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;
}
Beispiel #2
0
static evhtp_res
set_my_connection_handlers(evhtp_connection_t * conn, void * arg) {
    struct timeval               tick;
    struct ev_token_bucket_cfg * tcfg = NULL;

    evhtp_set_hook(&conn->hooks, evhtp_hook_on_header, print_kv, "foo");
    evhtp_set_hook(&conn->hooks, evhtp_hook_on_headers, print_kvs, "bar");
    evhtp_set_hook(&conn->hooks, evhtp_hook_on_path, print_path, "baz");
    evhtp_set_hook(&conn->hooks, evhtp_hook_on_read, print_data, "derp");
    evhtp_set_hook(&conn->hooks, evhtp_hook_on_new_chunk, print_new_chunk_len, NULL);
    evhtp_set_hook(&conn->hooks, evhtp_hook_on_chunk_complete, print_chunk_complete, NULL);
    evhtp_set_hook(&conn->hooks, evhtp_hook_on_chunks_complete, print_chunks_complete, NULL);
    /* evhtp_set_hook(&conn->hooks, evhtp_hook_on_hostname, print_hostname, NULL); */

    if (bw_limit > 0) {
        tick.tv_sec  = 0;
        tick.tv_usec = 500 * 100;

        tcfg         = ev_token_bucket_cfg_new(bw_limit, bw_limit, bw_limit, bw_limit, &tick);

        bufferevent_set_rate_limit(conn->bev, tcfg);
    }

    evhtp_set_hook(&conn->hooks, evhtp_hook_on_request_fini, test_fini, tcfg);

    return EVHTP_RES_OK;
}
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;
}
Beispiel #4
0
static void
echo_listenercb(struct evconnlistener *listener, evutil_socket_t newsock,
    struct sockaddr *sourceaddr, int socklen, void *ctx)
{
	struct event_base *base = ctx;
	int flags = BEV_OPT_CLOSE_ON_FREE|BEV_OPT_THREADSAFE;
	struct bufferevent *bev;

	bev = bufferevent_socket_new(base, newsock, flags);
	bufferevent_setcb(bev, echo_readcb, echo_writecb, echo_eventcb, NULL);
	if (conn_bucket_cfg)
		bufferevent_set_rate_limit(bev, conn_bucket_cfg);
	if (ratelim_group)
		bufferevent_add_to_rate_limit_group(bev, ratelim_group);
	++n_echo_conns_open;
	bufferevent_enable(bev, EV_READ|EV_WRITE);
}
Beispiel #5
0
static void
echo_listenercb(struct evconnlistener *listener, evutil_socket_t newsock,
    struct sockaddr *sourceaddr, int socklen, void *ctx)
{
	struct event_base *base = ctx;
	int flags = BEV_OPT_CLOSE_ON_FREE|BEV_OPT_THREADSAFE;
	struct bufferevent *bev;

	bev = bufferevent_socket_new(base, newsock, flags);
	bufferevent_setcb(bev, echo_readcb, echo_writecb, echo_eventcb, NULL);
	if (conn_bucket_cfg) {
		struct event *check_event =
		    event_new(base, -1, EV_PERSIST, check_bucket_levels_cb, bev);
		bufferevent_set_rate_limit(bev, conn_bucket_cfg);

		assert(bufferevent_get_token_bucket_cfg(bev) != NULL);
		event_add(check_event, ms100_common);
	}
	if (ratelim_group)
		bufferevent_add_to_rate_limit_group(bev, ratelim_group);
	++n_echo_conns_open;
	bufferevent_enable(bev, EV_READ|EV_WRITE);
}
Beispiel #6
0
int
tor_set_bufferevent_rate_limit(struct bufferevent *bev,
                               struct ev_token_bucket_cfg *cfg)
{
  return bufferevent_set_rate_limit(tor_get_root_bufferevent(bev), cfg);
}
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;
}
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;
}