Commit 3497eb8754e0eb7460fde4515b4f710a476b52a1

Authored by Georg Hopp
1 parent e35308f8

dynamically get and free buffer for response write pipe now

  1 +2012-02-15 04:44:38 +0100 Georg Hopp
  2 +
  3 + * dynamically get and free buffer for response write pipe now (HEAD, master)
  4 +
1 2012-02-14 21:32:38 +0100 Georg Hopp 5 2012-02-14 21:32:38 +0100 Georg Hopp
2 6
3 - * increase write buffer (HEAD, master) 7 + * increase write buffer (origin/master, origin/HEAD)
4 8
5 2012-02-13 21:27:47 +0100 Georg Hopp 9 2012-02-13 21:27:47 +0100 Georg Hopp
6 10
7 - * use one dynamic buffer less and save at least one write on small responses (origin/master, origin/HEAD) 11 + * use one dynamic buffer less and save at least one write on small responses
8 12
9 2012-02-13 18:25:36 +0100 Georg Hopp 13 2012-02-13 18:25:36 +0100 Georg Hopp
10 14
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 #include "http/response.h" 5 #include "http/response.h"
6 #include "http/message/queue.h" 6 #include "http/message/queue.h"
7 7
8 -#define RESPONSE_WRITER_BUF_SIZE 131072 8 +#define RESPONSE_WRITER_MAX_BUF 131072
9 9
10 10
11 typedef enum e_HttpResponseState { 11 typedef enum e_HttpResponseState {
@@ -15,7 +15,7 @@ typedef enum e_HttpResponseState { @@ -15,7 +15,7 @@ typedef enum e_HttpResponseState {
15 } HttpResponseState; 15 } HttpResponseState;
16 16
17 CLASS(HttpResponseWriter) { 17 CLASS(HttpResponseWriter) {
18 - char pipe[RESPONSE_WRITER_BUF_SIZE]; 18 + char * pipe;
19 19
20 size_t nheader; 20 size_t nheader;
21 size_t nbuffer; 21 size_t nbuffer;
@@ -12,11 +12,14 @@ @@ -12,11 +12,14 @@
12 #include "http/response.h" 12 #include "http/response.h"
13 #include "http/response/writer.h" 13 #include "http/response/writer.h"
14 14
  15 +#define MAX(x,y) ((x) > (y) ? (x) : (y))
  16 +#define _PSIZE(x) (MAX((x),RESPONSE_WRITER_MAX_BUF))
  17 +#define PSIZE _PSIZE(this->nheader+message->nbody)
  18 +
15 HttpResponse 19 HttpResponse
16 httpResponseWriterWrite(HttpResponseWriter this, int fd) 20 httpResponseWriterWrite(HttpResponseWriter this, int fd)
17 { 21 {
18 HttpMessageQueue respq = this->response_queue; 22 HttpMessageQueue respq = this->response_queue;
19 - HttpResponse retval = this->cur_response;  
20 HttpMessage message = (HttpMessage)this->cur_response; 23 HttpMessage message = (HttpMessage)this->cur_response;
21 int cont = 1; 24 int cont = 1;
22 25
@@ -25,7 +28,7 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) @@ -25,7 +28,7 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd)
25 case HTTP_RESPONSE_GET: 28 case HTTP_RESPONSE_GET:
26 if (NULL == this->cur_response && 0 < respq->nmsgs) { 29 if (NULL == this->cur_response && 0 < respq->nmsgs) {
27 message = respq->msgs[0]; 30 message = respq->msgs[0];
28 - retval = this->cur_response = (HttpResponse)message; 31 + this->cur_response = (HttpResponse)message;
29 32
30 memmove(respq->msgs, 33 memmove(respq->msgs,
31 &(respq->msgs[1]), 34 &(respq->msgs[1]),
@@ -34,8 +37,9 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) @@ -34,8 +37,9 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd)
34 this->nbuffer = 0; 37 this->nbuffer = 0;
35 this->written = 0; 38 this->written = 0;
36 this->pstart = 0; 39 this->pstart = 0;
37 - this->pend = httpMessageHeaderSizeGet(message);  
38 - this->nheader = this->pend; 40 + this->nheader = httpMessageHeaderSizeGet(message);
  41 + this->pend = this->nheader;
  42 + this->pipe = malloc(PSIZE);
39 httpMessageHeaderToString(message, this->pipe); 43 httpMessageHeaderToString(message, this->pipe);
40 44
41 this->state = HTTP_RESPONSE_WRITE; 45 this->state = HTTP_RESPONSE_WRITE;
@@ -53,11 +57,11 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) @@ -53,11 +57,11 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd)
53 size_t temp = 0; 57 size_t temp = 0;
54 size_t rsize; 58 size_t rsize;
55 59
56 - this->pend = (RESPONSE_WRITER_BUF_SIZE == this->pend)? 60 + this->pend = (PSIZE == this->pend)?
57 0 : this->pend; 61 0 : this->pend;
58 62
59 rsize = (this->pstart <= this->pend)? 63 rsize = (this->pstart <= this->pend)?
60 - RESPONSE_WRITER_BUF_SIZE - this->pend : 64 + PSIZE - this->pend :
61 this->pstart - 1; 65 this->pstart - 1;
62 66
63 switch (message->type) { 67 switch (message->type) {
@@ -92,27 +96,18 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) @@ -92,27 +96,18 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd)
92 96
93 wsize = (this->pstart <= this->pend)? 97 wsize = (this->pstart <= this->pend)?
94 this->pend - this->pstart : 98 this->pend - this->pstart :
95 - RESPONSE_WRITER_BUF_SIZE - this->pstart; 99 + PSIZE - this->pstart;
96 100
97 temp = write(fd, &(this->pipe[this->pstart]), wsize); 101 temp = write(fd, &(this->pipe[this->pstart]), wsize);
98 102
99 this->written += temp; 103 this->written += temp;
100 this->pstart += temp; 104 this->pstart += temp;
101 105
102 - this->pstart = (RESPONSE_WRITER_BUF_SIZE == this->pstart)? 106 + this->pstart = (PSIZE == this->pstart)?
103 0 : this->pstart; 107 0 : this->pstart;
104 } 108 }
105 109
106 if (this->written == message->nbody + this->nheader) { 110 if (this->written == message->nbody + this->nheader) {
107 - if (HTTP_MESSAGE_PIPED == message->type) {  
108 - close(message->handle);  
109 - }  
110 - this->nheader = 0;  
111 - this->nbuffer = 0;  
112 - this->written = 0;  
113 - this->pstart = 0;  
114 - this->pend = 0;  
115 -  
116 this->state = HTTP_RESPONSE_DONE; 111 this->state = HTTP_RESPONSE_DONE;
117 } 112 }
118 else { 113 else {
@@ -121,15 +116,31 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) @@ -121,15 +116,31 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd)
121 break; 116 break;
122 117
123 case HTTP_RESPONSE_DONE: 118 case HTTP_RESPONSE_DONE:
124 - this->cur_response = NULL;  
125 - this->state = HTTP_RESPONSE_GET;  
126 - cont = 0; 119 + if (HTTP_MESSAGE_PIPED == message->type) {
  120 + close(message->handle);
  121 + }
127 122
  123 + free(this->pipe);
  124 +
  125 + this->nheader = 0;
  126 + this->nbuffer = 0;
  127 + this->written = 0;
  128 + this->pstart = 0;
  129 + this->pend = 0;
  130 +
  131 + if (httpMessageHasKeepAlive(message)) {
  132 + delete(&this->cur_response);
  133 + }
  134 + else {
  135 + cont = 0;
  136 + }
  137 +
  138 + this->state = HTTP_RESPONSE_GET;
128 break; 139 break;
129 } 140 }
130 } 141 }
131 142
132 - return retval; 143 + return this->cur_response;
133 } 144 }
134 145
135 // vim: set ts=4 sw=4: 146 // vim: set ts=4 sw=4:
@@ -173,10 +173,9 @@ serverRun(Server this) @@ -173,10 +173,9 @@ serverRun(Server this)
173 (this->fds)[i].events &= ~POLLOUT; 173 (this->fds)[i].events &= ~POLLOUT;
174 } 174 }
175 else { 175 else {
  176 + delete(&message);
176 serverCloseConn(this, i); 177 serverCloseConn(this, i);
177 } 178 }
178 -  
179 - delete(&message);  
180 } 179 }
181 } 180 }
182 } 181 }
Please register or login to post a comment