2008-08-05 15:08:32 +00:00
|
|
|
|
2008-11-16 20:33:53 +00:00
|
|
|
#include <lighttpd/base.h>
|
2008-11-17 18:26:41 +00:00
|
|
|
#include <lighttpd/plugin_core.h>
|
2008-08-05 15:08:32 +00:00
|
|
|
|
|
|
|
/** repeats write after EINTR */
|
2008-08-09 11:51:06 +00:00
|
|
|
ssize_t net_write(int fd, void *buf, ssize_t nbyte) {
|
2008-08-05 15:08:32 +00:00
|
|
|
ssize_t r;
|
|
|
|
while (-1 == (r = write(fd, buf, nbyte))) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
/* Try again */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* report error */
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* return bytes written */
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** repeats read after EINTR */
|
2008-08-09 11:51:06 +00:00
|
|
|
ssize_t net_read(int fd, void *buf, ssize_t nbyte) {
|
2008-08-05 15:08:32 +00:00
|
|
|
ssize_t r;
|
|
|
|
while (-1 == (r = read(fd, buf, nbyte))) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
/* Try again */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* report error */
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* return bytes read */
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-10-25 12:53:57 +00:00
|
|
|
network_status_t network_write(vrequest *vr, int fd, chunkqueue *cq) {
|
2008-08-09 11:51:06 +00:00
|
|
|
network_status_t res;
|
2008-11-17 18:26:41 +00:00
|
|
|
ev_tstamp ts, now = CUR_TS(vr->con->wrk);
|
|
|
|
worker *wrk;
|
2008-08-09 11:51:06 +00:00
|
|
|
#ifdef TCP_CORK
|
|
|
|
int corked = 0;
|
|
|
|
#endif
|
2008-11-17 18:26:41 +00:00
|
|
|
goffset write_max = 256*1024, write_bytes, wrote; /* 256 kb */
|
|
|
|
|
|
|
|
if (CORE_OPTION(CORE_OPTION_THROTTLE).number) {
|
|
|
|
/* throttling is enabled */
|
|
|
|
if (G_UNLIKELY((now - vr->con->throttle.ts) > vr->con->wrk->throttle_queue.delay)) {
|
|
|
|
vr->con->throttle.magazine += CORE_OPTION(CORE_OPTION_THROTTLE).number * (now - vr->con->throttle.ts);
|
|
|
|
if (vr->con->throttle.magazine > CORE_OPTION(CORE_OPTION_THROTTLE).number)
|
|
|
|
vr->con->throttle.magazine = CORE_OPTION(CORE_OPTION_THROTTLE).number;
|
|
|
|
vr->con->throttle.ts = now;
|
|
|
|
/*g_print("throttle magazine: %u kbytes\n", vr->con->throttle.magazine / 1024);*/
|
|
|
|
}
|
|
|
|
write_max = vr->con->throttle.magazine;
|
|
|
|
}
|
2008-08-09 11:51:06 +00:00
|
|
|
|
|
|
|
#ifdef TCP_CORK
|
|
|
|
/* Linux: put a cork into the socket as we want to combine the write() calls
|
|
|
|
* but only if we really have multiple chunks
|
|
|
|
*/
|
|
|
|
if (cq->queue->length > 1) {
|
|
|
|
corked = 1;
|
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &corked, sizeof(corked));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-17 18:26:41 +00:00
|
|
|
write_bytes = write_max;
|
2008-11-17 19:23:16 +00:00
|
|
|
/* TODO: add setup-option to select the backend */
|
|
|
|
#ifdef USE_SENDFILE
|
2008-11-17 18:26:41 +00:00
|
|
|
res = network_write_sendfile(vr, fd, cq, &write_bytes);
|
2008-11-17 19:23:16 +00:00
|
|
|
#else
|
|
|
|
res = network_write_writev(con, fd, cq, &write_bytes);
|
|
|
|
#endif
|
2008-08-09 11:51:06 +00:00
|
|
|
|
|
|
|
#ifdef TCP_CORK
|
|
|
|
if (corked) {
|
|
|
|
corked = 0;
|
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &corked, sizeof(corked));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-17 18:26:41 +00:00
|
|
|
vr->con->throttle.magazine = write_bytes;
|
|
|
|
/* check if throttle magazine is empty */
|
|
|
|
if (CORE_OPTION(CORE_OPTION_THROTTLE).number && write_bytes == 0) {
|
|
|
|
/* remove EV_WRITE from sockwatcher for now */
|
|
|
|
ev_io_rem_events(vr->con->wrk->loop, &vr->con->sock_watcher, EV_WRITE);
|
|
|
|
waitqueue_push(&vr->con->wrk->throttle_queue, &vr->con->throttle.queue_elem);
|
2008-11-17 22:08:55 +00:00
|
|
|
return NETWORK_STATUS_WAIT_FOR_AIO_EVENT;
|
2008-11-17 18:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stats */
|
|
|
|
wrote = write_max - write_bytes;
|
|
|
|
wrk = vr->con->wrk;
|
|
|
|
wrk->stats.bytes_out += wrote;
|
|
|
|
vr->con->stats.bytes_out += wrote;
|
|
|
|
|
|
|
|
/* update 5s stats */
|
|
|
|
ts = CUR_TS(wrk);
|
|
|
|
|
|
|
|
if ((ts - vr->con->stats.last_avg) >= 5.0) {
|
|
|
|
vr->con->stats.bytes_out_5s_diff = vr->con->wrk->stats.bytes_out - vr->con->wrk->stats.bytes_out_5s;
|
|
|
|
vr->con->stats.bytes_out_5s = vr->con->stats.bytes_out;
|
|
|
|
vr->con->stats.last_avg = ts;
|
|
|
|
}
|
|
|
|
|
2008-11-12 01:09:52 +00:00
|
|
|
/* only update once a second, the cast is to round the timestamp */
|
2008-11-12 21:16:52 +00:00
|
|
|
if ((vr->con->io_timeout_elem.ts + 1.) < now)
|
2008-11-12 01:09:52 +00:00
|
|
|
waitqueue_push(&vr->con->wrk->io_timeout_queue, &vr->con->io_timeout_elem);
|
2008-11-10 14:39:03 +00:00
|
|
|
|
2008-08-09 11:51:06 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-10-25 12:53:57 +00:00
|
|
|
network_status_t network_read(vrequest *vr, int fd, chunkqueue *cq) {
|
2008-08-05 15:08:32 +00:00
|
|
|
const ssize_t blocksize = 16*1024; /* 16k */
|
|
|
|
const off_t max_read = 16 * blocksize; /* 256k */
|
|
|
|
ssize_t r;
|
2008-08-06 18:46:42 +00:00
|
|
|
off_t len = 0;
|
2008-11-12 01:09:52 +00:00
|
|
|
worker *wrk = vr->con->wrk;
|
|
|
|
ev_tstamp now = CUR_TS(wrk);
|
2008-08-05 15:08:32 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
GString *buf = g_string_sized_new(blocksize);
|
2008-08-06 18:46:42 +00:00
|
|
|
g_string_set_size(buf, blocksize);
|
2008-08-05 15:08:32 +00:00
|
|
|
if (-1 == (r = net_read(fd, buf->str, blocksize))) {
|
|
|
|
g_string_free(buf, TRUE);
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
#if EWOULDBLOCK != EAGAIN
|
2008-09-23 11:09:07 +00:00
|
|
|
case EWOULDBLOCK:
|
2008-08-05 15:08:32 +00:00
|
|
|
#endif
|
|
|
|
return len ? NETWORK_STATUS_SUCCESS : NETWORK_STATUS_WAIT_FOR_EVENT;
|
|
|
|
case ECONNRESET:
|
|
|
|
return NETWORK_STATUS_CONNECTION_CLOSE;
|
|
|
|
default:
|
2008-10-25 12:53:57 +00:00
|
|
|
VR_ERROR(vr, "oops, read from fd=%d failed: %s", fd, g_strerror(errno) );
|
2008-08-05 15:08:32 +00:00
|
|
|
return NETWORK_STATUS_FATAL_ERROR;
|
|
|
|
}
|
|
|
|
} else if (0 == r) {
|
|
|
|
g_string_free(buf, TRUE);
|
|
|
|
return len ? NETWORK_STATUS_SUCCESS : NETWORK_STATUS_CONNECTION_CLOSE;
|
|
|
|
}
|
|
|
|
g_string_truncate(buf, r);
|
|
|
|
chunkqueue_append_string(cq, buf);
|
|
|
|
len += r;
|
2008-11-03 14:18:46 +00:00
|
|
|
|
|
|
|
/* stats */
|
|
|
|
wrk = vr->con->wrk;
|
|
|
|
wrk->stats.bytes_in += r;
|
|
|
|
vr->con->stats.bytes_in += r;
|
|
|
|
|
|
|
|
/* update 5s stats */
|
|
|
|
|
2008-11-12 21:16:52 +00:00
|
|
|
if ((now - vr->con->stats.last_avg) >= 5.0) {
|
2008-11-03 14:18:46 +00:00
|
|
|
vr->con->stats.bytes_in_5s_diff = vr->con->stats.bytes_in - vr->con->stats.bytes_in_5s;
|
|
|
|
vr->con->stats.bytes_in_5s = vr->con->stats.bytes_in;
|
2008-11-12 01:09:52 +00:00
|
|
|
vr->con->stats.last_avg = now;
|
2008-11-03 14:18:46 +00:00
|
|
|
}
|
2008-08-05 15:08:32 +00:00
|
|
|
} while (r == blocksize && len < max_read);
|
|
|
|
|
2008-11-12 01:09:52 +00:00
|
|
|
/* only update once a second, the cast is to round the timestamp */
|
2008-11-12 21:16:52 +00:00
|
|
|
if ((vr->con->io_timeout_elem.ts + 1.) < now)
|
2008-11-12 01:09:52 +00:00
|
|
|
waitqueue_push(&vr->con->wrk->io_timeout_queue, &vr->con->io_timeout_elem);
|
|
|
|
|
2008-08-05 15:08:32 +00:00
|
|
|
return NETWORK_STATUS_SUCCESS;
|
|
|
|
}
|