Browse Source

Use non-blocking reads in read_socket_line, waiting up to the full timeout

master
Con Kolivas 10 years ago
parent
commit
4bc776f99b
  1. 34
      src/ckpool.c
  2. 2
      src/ckpool.h

34
src/ckpool.c

@ -495,11 +495,13 @@ void empty_buffer(connsock_t *cs)
/* Read from a socket into cs->buf till we get an '\n', converting it to '\0' /* Read from a socket into cs->buf till we get an '\n', converting it to '\0'
* and storing how much extra data we've received, to be moved to the beginning * and storing how much extra data we've received, to be moved to the beginning
* of the buffer for use on the next receive. */ * of the buffer for use on the next receive. */
int read_socket_line(connsock_t *cs, const int timeout) int read_socket_line(connsock_t *cs, float timeout)
{ {
int fd = cs->fd, ret = -1; int fd = cs->fd, ret = -1;
char *eom = NULL; char *eom = NULL;
tv_t start, now;
size_t buflen; size_t buflen;
float diff;
if (unlikely(fd < 0)) if (unlikely(fd < 0))
goto out; goto out;
@ -515,28 +517,35 @@ int read_socket_line(connsock_t *cs, const int timeout)
eom = strchr(cs->buf, '\n'); eom = strchr(cs->buf, '\n');
} }
while (42) { tv_time(&start);
char readbuf[PAGESIZE] = {}; rewait:
int backoff = 1;
char *newbuf;
ret = wait_read_select(fd, eom ? 0 : timeout); ret = wait_read_select(fd, eom ? 0 : timeout);
if (ret < 1) { if (ret < 1) {
if (!ret) {
if (eom) if (eom)
break; goto parse;
if (!ret)
LOGDEBUG("Select timed out in read_socket_line"); LOGDEBUG("Select timed out in read_socket_line");
else } else
LOGERR("Select failed in read_socket_line"); LOGERR("Select failed in read_socket_line");
goto out; goto out;
} }
ret = recv(fd, readbuf, PAGESIZE - 4, 0); tv_time(&now);
diff = tvdiff(&now, &start);
timeout -= diff;
while (42) {
char readbuf[PAGESIZE] = {};
int backoff = 1;
char *newbuf;
ret = recv(fd, readbuf, PAGESIZE - 4, MSG_DONTWAIT);
if (ret < 1) { if (ret < 1) {
/* Closed socket after valid message */ /* No more to read or closed socket after valid message */
if (eom) if (eom)
break; break;
/* Have we used up all the timeout yet? */
if (timeout > 0 && (errno == EAGAIN || errno == EWOULDBLOCK || !ret))
goto rewait;
LOGERR("Failed to recv in read_socket_line"); LOGERR("Failed to recv in read_socket_line");
ret = -1;
goto out; goto out;
} }
buflen = cs->bufofs + ret + 1; buflen = cs->bufofs + ret + 1;
@ -557,6 +566,7 @@ int read_socket_line(connsock_t *cs, const int timeout)
cs->buf[cs->bufofs] = '\0'; cs->buf[cs->bufofs] = '\0';
eom = strchr(cs->buf, '\n'); eom = strchr(cs->buf, '\n');
} }
parse:
ret = eom - cs->buf; ret = eom - cs->buf;
cs->buflen = cs->buf + cs->bufofs - eom - 1; cs->buflen = cs->buf + cs->bufofs - eom - 1;

2
src/ckpool.h

@ -242,7 +242,7 @@ ckpool_t *global_ckp;
bool ping_main(ckpool_t *ckp); bool ping_main(ckpool_t *ckp);
void empty_buffer(connsock_t *cs); void empty_buffer(connsock_t *cs);
int read_socket_line(connsock_t *cs, const int timeout); int read_socket_line(connsock_t *cs, float timeout);
void _send_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line); void _send_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line);
#define send_proc(pi, msg) _send_proc(pi, msg, __FILE__, __func__, __LINE__) #define send_proc(pi, msg) _send_proc(pi, msg, __FILE__, __func__, __LINE__)
char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line); char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line);

Loading…
Cancel
Save