Exemple #1
0
void Pool::connect() {
    if (state_ == POOL_STATE_NEW ||
            state_ == POOL_STATE_WAITING_TO_CONNECT) {
        LOG_DEBUG("Connect %s pool(%p)",
                  address_.to_string().c_str(), static_cast<void*>(this));

        connect_timer.stop(); // There could be a delayed connect waiting

        for (unsigned i = 0; i < config_.core_connections_per_host(); ++i) {
            spawn_connection();
        }
        state_ = POOL_STATE_CONNECTING;
        maybe_notify_ready();
    }
}
Exemple #2
0
void Pool::maybe_spawn_connection() {
    if (connections_pending_.size() >= config_.max_concurrent_creation()) {
        return;
    }

    if (connections_.size() + connections_pending_.size() >=
            config_.max_connections_per_host()) {
        return;
    }

    if (state_ != POOL_STATE_READY) {
        return;
    }

    spawn_connection();
}
Exemple #3
0
static void
periodic_watcher_cb(struct ev_loop *el, struct ev_periodic *ep, int revents) {
    static float conns_per_tick = 0.0f;
    static float conns_remainder = 0.0f;

    if (conns_per_tick == 0) {
        conns_per_tick = (connRate <= MAX_TICK_FREQUENCY) ?
            1.0f :
            (connRate / MAX_TICK_FREQUENCY);
        DBG(0, ("making %f connections per tick\n", conns_per_tick));
    }

    for (conns_remainder += conns_per_tick;
         conns_remainder >= 1.0f && numConns > conns_cnt;
         conns_remainder--, conns_cnt++) {
        spawn_connection(el);
    }

    if (numConns <= conns_cnt) {
        ev_periodic_stop(el, ep);
    }
}
Exemple #4
0
int
main(int argc, char **argv) {
    struct ev_loop *el;
    struct ev_periodic ep;
    struct sigaction act;
    int c;

    // Initialize stats
    errno_cnt_init(&socket_errors_cnt);
    errno_cnt_init(&fcntl_errors_cnt);
    errno_cnt_init(&setsockopt_errors_cnt);
    errno_cnt_init(&connect_errors_cnt);
    errno_cnt_init(&write_errors_cnt);

    // Options and defaults
    struct hostent *hent;
    float period = 1.0f / connRate;

    optreset = optind = 1;
    while ((c = getopt(argc, argv, "r:s:n:hv")) != -1) {
        switch (c) {
        case 'h':
            usage(stdout, argv[0]);
            return 0;

        case 'r':
            if ((connRate = strtoul(optarg, NULL, 10)) > 0) {
                period = 1.0f / MIN(connRate, MAX_TICK_FREQUENCY);
            }
            break;

        case 's':
            dataSize = strtoul(optarg, NULL, 10);
            break;

        case 'n':
            numConns = strtoul(optarg, NULL, 10);
            break;

        case 'v':
            dbg_level++;
            break;

        case '?':
            fprintf(stderr, "%s: unknown option %c\n", argv[0], optopt);
            return 1;
        }
    }

    if ((argc - optind) != 2) {
        usage(stderr, argv[0]);
        return 1;
    }

    if (!(hent = gethostbyname(argv[optind]))) {
        fprintf(stderr, "%s: could not resolve host name\n", argv[0]);
        return 1;
    }

    bzero(&addr, sizeof(addr));
    memcpy(&addr.sin_addr, hent->h_addr, sizeof(addr.sin_addr));
    addr.sin_port = htons(strtoul(argv[optind + 1], NULL, 10));
    addr.sin_len = sizeof(addr);
    addr.sin_family = AF_INET;

    bzero(&act, sizeof(act));
    act.sa_handler = sigint_cb;
    if (sigaction(SIGINT, &act, NULL)) {
        perror("sigaction");
        exit(1);
    }

    el = ev_default_loop(EVFLAG_AUTO);

    if (connRate > 0) {
        ev_periodic_init(&ep, periodic_watcher_cb, 0, period, NULL);
        ev_periodic_start(el, &ep);
    } else {
        while (numConns-- > 0) {
            spawn_connection(el);
        }
    }

    atexit(atexit_cb);

    ev_loop(el, 0);
    ev_default_destroy();

    return 0;
}