summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c27
-rw-r--r--ot_fullscrape.c2
-rw-r--r--ot_http.c36
-rw-r--r--ot_http.h3
4 files changed, 53 insertions, 15 deletions
diff --git a/opentracker.c b/opentracker.c
index 441639a..cc0700c 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -156,7 +156,10 @@ static size_t header_complete( char * request, ssize_t byte_count ) {
156static void handle_dead( const int64 sock ) { 156static void handle_dead( const int64 sock ) {
157 struct http_data* cookie=io_getcookie( sock ); 157 struct http_data* cookie=io_getcookie( sock );
158 if( cookie ) { 158 if( cookie ) {
159 iob_reset( &cookie->batch ); 159 size_t i;
160 for ( i = 0; i < cookie->batches; ++i)
161 iob_reset( cookie->batch + i );
162 free( cookie->batch );
160 array_reset( &cookie->request ); 163 array_reset( &cookie->request );
161 if( cookie->flag & STRUCT_HTTP_FLAG_WAITINGFORTASK ) 164 if( cookie->flag & STRUCT_HTTP_FLAG_WAITINGFORTASK )
162 mutex_workqueue_canceltask( sock ); 165 mutex_workqueue_canceltask( sock );
@@ -204,12 +207,22 @@ static void handle_read( const int64 sock, struct ot_workstruct *ws ) {
204 207
205static void handle_write( const int64 sock ) { 208static void handle_write( const int64 sock ) {
206 struct http_data* cookie=io_getcookie( sock ); 209 struct http_data* cookie=io_getcookie( sock );
207 if( cookie ) { 210 size_t i;
208 int64 res = iob_send( sock, &cookie->batch ); 211
209 if (res == 0 || res == -3) 212 /* Look for the first io_batch still containing bytes to write */
210 handle_dead( sock ); 213 if( cookie )
211 } else 214 for( i = 0; i < cookie->batches; ++i )
212 handle_dead( sock ); 215 if( cookie->batch[i].bytesleft ) {
216 int64 res = iob_send( sock, cookie->batch + i );
217
218 if( res == -3 )
219 break;
220
221 if( res == -1 || res > 0 || i < cookie->batches - 1 )
222 return;
223 }
224
225 handle_dead( sock );
213} 226}
214 227
215static void handle_accept( const int64 serversocket ) { 228static void handle_accept( const int64 serversocket ) {
diff --git a/ot_fullscrape.c b/ot_fullscrape.c
index c3c6b34..5d115dc 100644
--- a/ot_fullscrape.c
+++ b/ot_fullscrape.c
@@ -191,7 +191,7 @@ static void fullscrape_make_gzip( int *iovec_entries, struct iovec **iovector, o
191 byte_zero( &strm, sizeof(strm) ); 191 byte_zero( &strm, sizeof(strm) );
192 strm.next_out = (uint8_t*)r; 192 strm.next_out = (uint8_t*)r;
193 strm.avail_out = OT_SCRAPE_CHUNK_SIZE; 193 strm.avail_out = OT_SCRAPE_CHUNK_SIZE;
194 if( deflateInit2(&strm,7,Z_DEFLATED,31,8,Z_DEFAULT_STRATEGY) != Z_OK ) 194 if( deflateInit2(&strm,7,Z_DEFLATED,31,9,Z_DEFAULT_STRATEGY) != Z_OK )
195 fprintf( stderr, "not ok.\n" ); 195 fprintf( stderr, "not ok.\n" );
196 196
197 if( ( mode & TASK_TASK_MASK ) == TASK_FULLSCRAPE ) { 197 if( ( mode & TASK_TASK_MASK ) == TASK_FULLSCRAPE ) {
diff --git a/ot_http.c b/ot_http.c
index e05db72..390878b 100644
--- a/ot_http.c
+++ b/ot_http.c
@@ -31,6 +31,7 @@
31#include "ot_accesslist.h" 31#include "ot_accesslist.h"
32 32
33#define OT_MAXMULTISCRAPE_COUNT 64 33#define OT_MAXMULTISCRAPE_COUNT 64
34#define OT_BATCH_LIMIT (1024*1024*16)
34extern char *g_redirecturl; 35extern char *g_redirecturl;
35 36
36char *g_stats_path; 37char *g_stats_path;
@@ -75,7 +76,13 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
75 } 76 }
76 77
77 memcpy( outbuf, ws->reply + written_size, ws->reply_size - written_size ); 78 memcpy( outbuf, ws->reply + written_size, ws->reply_size - written_size );
78 iob_addbuf_free( &cookie->batch, outbuf, ws->reply_size - written_size ); 79 if ( !cookie->batch ) {
80 cookie->batch = malloc( sizeof(io_batch) );
81 memset( cookie->batch, 0, sizeof(io_batch) );
82 cookie->batches = 1;
83 }
84
85 iob_addbuf_free( cookie->batch, outbuf, ws->reply_size - written_size );
79 86
80 /* writeable short data sockets just have a tcp timeout */ 87 /* writeable short data sockets just have a tcp timeout */
81 if( !ws->keep_alive ) { 88 if( !ws->keep_alive ) {
@@ -152,12 +159,29 @@ ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iove
152 else 159 else
153 header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %zd\r\n\r\n", size ); 160 header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %zd\r\n\r\n", size );
154 161
155 iob_reset( &cookie->batch ); 162 if (!cookie->batch ) {
156 iob_addbuf_free( &cookie->batch, header, header_size ); 163 cookie->batch = malloc( sizeof(io_batch) );
164 memset( cookie->batch, 0, sizeof(io_batch) );
165 cookie->batches = 1;
166 }
167 iob_addbuf_free( cookie->batch, header, header_size );
168
169 /* Split huge iovectors into separate io_batches */
170 for( i=0; i<iovec_entries; ++i ) {
171 io_batch *current = cookie->batch + cookie->batches;
172
173 /* If the current batch's limit is reached, try to reallocate a new batch to work on */
174 if( current->bytesleft > OT_BATCH_LIMIT ) {
175 io_batch * new_batch = realloc( current, (cookie->batches + 1) * sizeof(io_batch) );
176 if( new_batch ) {
177 cookie->batches++;
178 current = cookie->batch = new_batch;
179 memset( current, 0, sizeof(io_batch) );
180 }
181 }
157 182
158 /* Will move to ot_iovec.c */ 183 iob_addbuf_munmap( current, iovector[i].iov_base, iovector[i].iov_len );
159 for( i=0; i<iovec_entries; ++i ) 184 }
160 iob_addbuf_munmap( &cookie->batch, iovector[i].iov_base, iovector[i].iov_len );
161 free( iovector ); 185 free( iovector );
162 186
163 /* writeable sockets timeout after 10 minutes */ 187 /* writeable sockets timeout after 10 minutes */
diff --git a/ot_http.h b/ot_http.h
index b1a60e7..40161d8 100644
--- a/ot_http.h
+++ b/ot_http.h
@@ -14,7 +14,8 @@ typedef enum {
14 14
15struct http_data { 15struct http_data {
16 array request; 16 array request;
17 io_batch batch; 17 io_batch *batch;
18 size_t batches;
18 ot_ip6 ip; 19 ot_ip6 ip;
19 STRUCT_HTTP_FLAG flag; 20 STRUCT_HTTP_FLAG flag;
20}; 21};