diff options
author | Determinant <[email protected]> | 2018-07-16 18:44:56 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2018-07-16 18:44:56 -0400 |
commit | ea892d32e090c5d48d383646d67105f571877993 (patch) | |
tree | 4abc565149bc21e11a996b8b0f3b213654a8f1c1 /src | |
parent | fd881223556fc608b24626a7bc6e78bf576d3ed7 (diff) |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/conn.cpp | 24 | ||||
-rw-r--r-- | src/util.cpp | 4 |
2 files changed, 17 insertions, 11 deletions
diff --git a/src/conn.cpp b/src/conn.cpp index 0b42853..62cba8a 100644 --- a/src/conn.cpp +++ b/src/conn.cpp @@ -45,12 +45,12 @@ ConnPool::Conn::operator std::string() const { void ConnPool::Conn::send_data(evutil_socket_t fd, short events) { if (!(events & EV_WRITE)) return; auto conn = self(); /* pin the connection */ - ssize_t ret = BUFF_SEG_SIZE; - while (ret == BUFF_SEG_SIZE) + ssize_t ret = seg_buff_size; + while (ret == (ssize_t)seg_buff_size) { if (!send_buffer.size()) /* nothing to write */ break; - bytearray_t buff_seg = send_buffer.pop(BUFF_SEG_SIZE); + bytearray_t buff_seg = send_buffer.pop(seg_buff_size); ssize_t size = buff_seg.size(); ret = send(fd, buff_seg.data(), size, MSG_NOSIGNAL); SALTICIDAE_LOG_DEBUG("socket sent %d bytes", ret); @@ -88,12 +88,12 @@ void ConnPool::Conn::send_data(evutil_socket_t fd, short events) { void ConnPool::Conn::recv_data(evutil_socket_t fd, short events) { if (!(events & EV_READ)) return; auto conn = self(); /* pin the connection */ - ssize_t ret = BUFF_SEG_SIZE; - while (ret == BUFF_SEG_SIZE) + ssize_t ret = seg_buff_size; + while (ret == (ssize_t)seg_buff_size) { bytearray_t buff_seg; - buff_seg.resize(BUFF_SEG_SIZE); - ret = recv(fd, buff_seg.data(), BUFF_SEG_SIZE, 0); + buff_seg.resize(seg_buff_size); + ret = recv(fd, buff_seg.data(), seg_buff_size, 0); SALTICIDAE_LOG_DEBUG("socket read %d bytes", ret); if (ret <= 0) { @@ -137,6 +137,7 @@ void ConnPool::accept_client(evutil_socket_t fd, short) { NetAddr addr((struct sockaddr_in *)&client_addr); conn_t conn = create_conn(); Conn *conn_ptr = conn.get(); + conn->seg_buff_size = seg_buff_size; conn->fd = client_fd; conn->cpool = this; conn->mode = Conn::PASSIVE; @@ -197,7 +198,7 @@ void ConnPool::init(NetAddr listen_addr) { if (bind(listen_fd, (struct sockaddr *)&sockin, sizeof(sockin)) < 0) throw ConnPoolError(std::string("binding error")); - if (::listen(listen_fd, MAX_LISTEN_BACKLOG) < 0) + if (::listen(listen_fd, max_listen_backlog) < 0) throw ConnPoolError(std::string("listen error")); ev_listen = Event(eb, listen_fd, EV_READ, std::bind(&ConnPool::accept_client, this, _1, _2)); @@ -238,7 +239,7 @@ void ConnPool::Conn::try_conn(evutil_socket_t, short) { } ev_connect = Event(cpool->eb, fd, EV_WRITE, std::bind(&Conn::conn_server, this, _1, _2)); - ev_connect.add_with_timeout(CONN_SERVER_TIMEOUT); + ev_connect.add_with_timeout(cpool->conn_server_timeout); } ConnPool::conn_t ConnPool::create_conn(const NetAddr &addr) { @@ -252,13 +253,14 @@ ConnPool::conn_t ConnPool::create_conn(const NetAddr &addr) { if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) throw ConnPoolError(std::string("unable to set nonblocking socket")); conn_t conn = create_conn(); - Conn * conn_ptr = conn.get(); + Conn *conn_ptr = conn.get(); + conn->seg_buff_size = seg_buff_size; conn->fd = fd; conn->cpool = this; conn->mode = Conn::ACTIVE; conn->addr = addr; conn->ev_connect = Event(eb, -1, 0, std::bind(&Conn::try_conn, conn_ptr, _1, _2)); - conn->ev_connect.add_with_timeout(gen_rand_timeout(TRY_CONN_DELAY)); + conn->ev_connect.add_with_timeout(gen_conn_timeout()); add_conn(conn); SALTICIDAE_LOG_INFO("created connection %s", std::string(*conn).c_str()); return conn; diff --git a/src/util.cpp b/src/util.cpp index 2adb997..7125598 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -47,6 +47,10 @@ void event_add_with_timeout(struct event *ev, double timeout) { event_add(ev, &tv); } +double gen_rand_timeout(double base_timeout, double alpha) { + return base_timeout + rand() / (double)RAND_MAX * alpha * base_timeout; +} + const std::string get_current_datetime() { /* credit: http://stackoverflow.com/a/41381479/544806 */ char fmt[64], buf[64]; |