Improve log diagnostics related to rhizome HTTP

This commit is contained in:
Andrew Bettison 2012-07-10 19:59:46 +09:30
parent e8eab5b27e
commit 42744da371
3 changed files with 48 additions and 32 deletions

View File

@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include "serval.h" #include "serval.h"
#include "strbuf.h"
#include <poll.h> #include <poll.h>
#define MAX_WATCHED_FDS 128 #define MAX_WATCHED_FDS 128
@ -169,7 +170,15 @@ int fd_poll()
{ {
struct call_stats call_stats; struct call_stats call_stats;
fd_func_enter(&call_stats); fd_func_enter(&call_stats);
r=poll(fds, fdcount, ms); strbuf b = strbuf_alloca(1024);
int i;
for (i = 0; i < fdcount; ++i) {
if (i)
strbuf_puts(b, ", ");
strbuf_sprintf(b, "%d:%04x:%04x", fds[i].fd, fds[i].events, fds[i].revents);
}
r = poll(fds, fdcount, ms);
DEBUGF("poll(fds=(%s), fdcount=%d, ms=%d) = %d", strbuf_str(b), fdcount, ms, r);
fd_func_exit(&call_stats, &poll_stats); fd_func_exit(&call_stats, &poll_stats);
} }

View File

@ -755,17 +755,16 @@ int rhizome_fetch_close(rhizome_file_fetch_record *q){
} }
void rhizome_fetch_write(rhizome_file_fetch_record *q){ void rhizome_fetch_write(rhizome_file_fetch_record *q){
int bytes; int bytes = write_nonblock(q->alarm.poll.fd, &q->request[q->request_ofs], q->request_len-q->request_ofs);
bytes=write(q->alarm.poll.fd,&q->request[q->request_ofs], if (bytes == -1) {
q->request_len-q->request_ofs); WHY("Got error while sending HTTP request. Closing.");
if (bytes>0) { rhizome_fetch_close(q);
} else {
// reset timeout // reset timeout
unschedule(&q->alarm); unschedule(&q->alarm);
q->alarm.alarm=overlay_gettime_ms() + RHIZOME_IDLE_TIMEOUT; q->alarm.alarm=overlay_gettime_ms() + RHIZOME_IDLE_TIMEOUT;
schedule(&q->alarm); schedule(&q->alarm);
q->request_ofs+=bytes; q->request_ofs+=bytes;
if (q->request_ofs>=q->request_len) { if (q->request_ofs>=q->request_len) {
/* Sent all of request. Switch to listening for HTTP response headers. /* Sent all of request. Switch to listening for HTTP response headers.
*/ */
@ -775,9 +774,6 @@ void rhizome_fetch_write(rhizome_file_fetch_record *q){
watch(&q->alarm); watch(&q->alarm);
}else if(q->state==RHIZOME_FETCH_CONNECTING) }else if(q->state==RHIZOME_FETCH_CONNECTING)
q->state = RHIZOME_FETCH_SENDINGHTTPREQUEST; q->state = RHIZOME_FETCH_SENDINGHTTPREQUEST;
} else if (errno!=EAGAIN) {
WHY("Got error while sending HTTP request. Closing.");
rhizome_fetch_close(q);
} }
} }

View File

@ -187,7 +187,7 @@ int rhizome_http_server_start()
return WHY("Failed to start rhizome HTTP server"); return WHY("Failed to start rhizome HTTP server");
} }
INFOF("Started Rhizome HTTP server on port %d", port); INFOF("Started Rhizome HTTP server on port %d, fd = %d", port, rhizome_server_socket);
/* Add Rhizome HTTPd server to list of file descriptors to watch */ /* Add Rhizome HTTPd server to list of file descriptors to watch */
server_alarm.function = rhizome_server_poll; server_alarm.function = rhizome_server_poll;
@ -270,30 +270,41 @@ void rhizome_client_poll(struct sched_ent *alarm)
void rhizome_server_poll(struct sched_ent *alarm) void rhizome_server_poll(struct sched_ent *alarm)
{ {
if (debug & DEBUG_RHIZOMESYNC) D;
struct sockaddr addr; struct sockaddr addr;
unsigned int addr_len=0; unsigned int addr_len = sizeof addr;
int sock; int sock;
while ((sock = accept(rhizome_server_socket, &addr, &addr_len)) != -1) {
/* Deal with any new requests */ if (addr.sa_family == AF_INET) {
struct sockaddr_in *peerip = (struct sockaddr_in *)&addr;
while ((sock=accept(rhizome_server_socket,&addr,&addr_len))>-1) INFOF("HTTP ACCEPT addrlen=%u family=%u port=%u addr=%u.%u.%u.%u",
{ addr_len, peerip->sin_family, peerip->sin_port,
rhizome_http_request *request = calloc(sizeof(rhizome_http_request),1); ((unsigned char*)&peerip->sin_addr.s_addr)[0],
((unsigned char*)&peerip->sin_addr.s_addr)[1],
/* We are now trying to read the HTTP request */ ((unsigned char*)&peerip->sin_addr.s_addr)[2],
request->request_type=RHIZOME_HTTP_REQUEST_RECEIVING; ((unsigned char*)&peerip->sin_addr.s_addr)[3]
request->alarm.function = rhizome_client_poll; );
connection_stats.name="rhizome_client_poll"; } else {
request->alarm.stats=&connection_stats; INFOF("HTTP ACCEPT addrlen=%u family=%u data=%s",
request->alarm.poll.fd=sock; addr_len, addr.sa_family, alloca_tohex((unsigned char *)addr.sa_data, sizeof addr.sa_data));
request->alarm.poll.events=POLLIN;
request->alarm.alarm = overlay_gettime_ms()+RHIZOME_IDLE_TIMEOUT;
// watch for the incoming http request
watch(&request->alarm);
// set an inactivity timeout to close the connection
schedule(&request->alarm);
} }
rhizome_http_request *request = calloc(sizeof(rhizome_http_request),1);
/* We are now trying to read the HTTP request */
request->request_type=RHIZOME_HTTP_REQUEST_RECEIVING;
request->alarm.function = rhizome_client_poll;
connection_stats.name="rhizome_client_poll";
request->alarm.stats=&connection_stats;
request->alarm.poll.fd=sock;
request->alarm.poll.events=POLLIN;
request->alarm.alarm = overlay_gettime_ms()+RHIZOME_IDLE_TIMEOUT;
// watch for the incoming http request
watch(&request->alarm);
// set an inactivity timeout to close the connection
schedule(&request->alarm);
}
if (errno != EAGAIN) {
WARN_perror("accept");
}
} }
int rhizome_server_free_http_request(rhizome_http_request *r) int rhizome_server_free_http_request(rhizome_http_request *r)