Skip to content

Commit

Permalink
read all available bytes when socket ready
Browse files Browse the repository at this point in the history
  • Loading branch information
wg committed Oct 5, 2013
1 parent 796f4e1 commit fe4c1a6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
7 changes: 7 additions & 0 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include "net.h"

Expand Down Expand Up @@ -30,3 +31,9 @@ status sock_write(connection *c, char *buf, size_t len, size_t *n) {
*n = (size_t) r;
return OK;
}

size_t sock_readable(connection *c) {
int n, rc;
rc = ioctl(c->fd, FIONREAD, &n);
return rc == -1 ? 0 : n;
}
10 changes: 6 additions & 4 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ typedef enum {
} status;

struct sock {
status (*connect)(connection *);
status ( *close)(connection *);
status ( *read)(connection *, size_t *);
status ( *write)(connection *, char *, size_t, size_t *);
status ( *connect)(connection *);
status ( *close)(connection *);
status ( *read)(connection *, size_t *);
status ( *write)(connection *, char *, size_t, size_t *);
size_t (*readable)(connection *);
};

status sock_connect(connection *);
status sock_close(connection *);
status sock_read(connection *, size_t *);
status sock_write(connection *, char *, size_t, size_t *);
size_t sock_readable(connection *);

#endif /* NET_H */
4 changes: 4 additions & 0 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ status ssl_write(connection *c, char *buf, size_t len, size_t *n) {
*n = (size_t) r;
return OK;
}

size_t ssl_readable(connection *c) {
return SSL_pending(c->ssl);
}
1 change: 1 addition & 0 deletions src/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ status ssl_connect(connection *);
status ssl_close(connection *);
status ssl_read(connection *, size_t *);
status ssl_write(connection *, char *, size_t, size_t *);
size_t ssl_readable(connection *);

#endif /* SSL_H */
34 changes: 19 additions & 15 deletions src/wrk.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ static struct {
} statistics;

static struct sock sock = {
.connect = sock_connect,
.close = sock_close,
.read = sock_read,
.write = sock_write
.connect = sock_connect,
.close = sock_close,
.read = sock_read,
.write = sock_write,
.readable = sock_readable
};

static struct http_parser_settings parser_settings = {
Expand Down Expand Up @@ -114,10 +115,11 @@ int main(int argc, char **argv) {
ERR_print_errors_fp(stderr);
exit(1);
}
sock.connect = ssl_connect;
sock.close = ssl_close;
sock.read = ssl_read;
sock.write = ssl_write;
sock.connect = ssl_connect;
sock.close = ssl_close;
sock.read = ssl_read;
sock.write = ssl_write;
sock.readable = ssl_readable;
}

signal(SIGPIPE, SIG_IGN);
Expand Down Expand Up @@ -496,14 +498,16 @@ static void socket_readable(aeEventLoop *loop, int fd, void *data, int mask) {
connection *c = data;
size_t n;

switch (sock.read(c, &n)) {
case OK: break;
case ERROR: goto error;
case RETRY: return;
}
do {
switch (sock.read(c, &n)) {
case OK: break;
case ERROR: goto error;
case RETRY: return;
}

if (http_parser_execute(&c->parser, &parser_settings, c->buf, n) != n) goto error;
c->thread->bytes += n;
if (http_parser_execute(&c->parser, &parser_settings, c->buf, n) != n) goto error;
c->thread->bytes += n;
} while (n == RECVBUF && sock.readable(c) > 0);

return;

Expand Down
2 changes: 1 addition & 1 deletion src/wrk.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "script.h"
#include "http_parser.h"

#define VERSION "3.0.0"
#define VERSION "3.0.1"
#define RECVBUF 8192
#define SAMPLES 100000000

Expand Down

0 comments on commit fe4c1a6

Please sign in to comment.