Skip to content

Instantly share code, notes, and snippets.

@tfheen
Created October 11, 2011 05:31
Show Gist options
  • Save tfheen/1277365 to your computer and use it in GitHub Desktop.
Save tfheen/1277365 to your computer and use it in GitHub Desktop.
diff --git a/bin/varnishd/cache_wrw.c b/bin/varnishd/cache_wrw.c
index 61aa2e5..c591ddc 100644
--- a/bin/varnishd/cache_wrw.c
+++ b/bin/varnishd/cache_wrw.c
@@ -123,7 +123,37 @@ WRW_Flush(struct worker *w)
wrw->iov[wrw->ciov].iov_len = 0;
}
i = writev(*wrw->wfd, wrw->iov, wrw->niov);
- if (i != wrw->liov) {
+ while (i != wrw->liov && i > 0) {
+ /* Remove sent data from start of I/O vector,
+ * then retry; we hit a timeout, but some data
+ * was sent.
+
+ XXX: Add a "minimum sent data per timeout
+ counter to prevent slowlaris attacks
+ */
+ size_t used = 0;
+
+ WSL(w, SLT_Debug, *wrw->wfd,
+ "Hit send timeout, wrote = %ld/%ld; retrying",
+ i, wrw->liov);
+
+ for (int j = 0; j < wrw->niov; j++) {
+ if (used + wrw->iov[j].iov_len > i) {
+ /* Cutoff is in this iov */
+ int used_here = i - used;
+ wrw->iov[j].iov_len -= used_here;
+ wrw->iov[j].iov_base = (char*)wrw->iov[j].iov_base + used_here;
+ memmove(wrw->iov, &wrw->iov[j],
+ (wrw->niov - j) * sizeof(struct iovec));
+ wrw->niov -= j;
+ wrw->liov -= i;
+ break;
+ }
+ used += wrw->iov[j].iov_len;
+ }
+ i = writev(*wrw->wfd, wrw->iov, wrw->niov);
+ }
+ if (i <= 0) {
wrw->werr++;
WSL(w, SLT_Debug, *wrw->wfd,
"Write error, retval = %d, len = %d, errno = %s",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment