Skip to content

Commit

Permalink
Honor first_byte_timeout for recycled backend connections
Browse files Browse the repository at this point in the history
  • Loading branch information
daghf authored and dmatetelki committed Mar 14, 2019
1 parent 6eac050 commit 68bdb81
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
31 changes: 20 additions & 11 deletions bin/varnishd/cache/cache_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ vbe_dir_finish(const struct director *d, struct worker *wrk,
CAST_OBJ_NOTNULL(vtp, bo->htc->priv, VTP_MAGIC);
bo->htc->priv = NULL;
if (vtp->state != VTP_STATE_USED)
assert(bo->htc->doclose == SC_TX_PIPE);
assert(bo->htc->doclose == SC_TX_PIPE ||
bo->htc->doclose == SC_RX_TIMEOUT);
if (bo->htc->doclose != SC_NULL || bp->proxy_header != 0) {
VSLb(bo->vsl, SLT_BackendClose, "%d %s", vtp->fd,
bp->director->display_name);
Expand Down Expand Up @@ -233,16 +234,24 @@ vbe_dir_gethdrs(const struct director *d, struct worker *wrk,

i = V1F_SendReq(wrk, bo, &bo->acct.bereq_hdrbytes, 0);

if (vtp->state != VTP_STATE_USED)
VTP_Wait(wrk, vtp);

assert(vtp->state == VTP_STATE_USED);
if (vtp->state != VTP_STATE_USED) {
if (VTP_Wait(wrk, vtp, VTIM_real() +
bo->htc->first_byte_timeout) != 0) {
bo->htc->doclose = SC_RX_TIMEOUT;
VSLb(bo->vsl, SLT_FetchError,
"Timed out reusing backend connection");
extrachance = 0;
}
}

if (i == 0)
i = V1F_FetchRespHdr(bo);
if (i == 0) {
AN(bo->htc->priv);
return (0);
if (bo->htc->doclose == SC_NULL) {
assert(vtp->state == VTP_STATE_USED);
if (i == 0)
i = V1F_FetchRespHdr(bo);
if (i == 0) {
AN(bo->htc->priv);
return (0);
}
}

/*
Expand All @@ -252,7 +261,7 @@ vbe_dir_gethdrs(const struct director *d, struct worker *wrk,
*/
vbe_dir_finish(d, wrk, bo);
AZ(bo->htc);
if (i < 0)
if (i < 0 || extrachance == 0)
break;
if (bo->req != NULL &&
bo->req->req_body_status != REQ_BODY_NONE &&
Expand Down
20 changes: 16 additions & 4 deletions bin/varnishd/cache/cache_tcp_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "config.h"

#include <errno.h>
#include <stdlib.h>

#include "cache_varnishd.h"
Expand Down Expand Up @@ -416,22 +417,33 @@ VTP_Get(struct tcp_pool *tp, double tmo, struct worker *wrk,
/*--------------------------------------------------------------------
*/

void
VTP_Wait(struct worker *wrk, struct vtp *vtp)
int
VTP_Wait(struct worker *wrk, struct vtp *vtp, double tmo)
{
struct tcp_pool *tp;
int r;

CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
CHECK_OBJ_NOTNULL(vtp, VTP_MAGIC);
tp = vtp->tcp_pool;
CHECK_OBJ_NOTNULL(tp, TCP_POOL_MAGIC);
assert(vtp->cond == &wrk->cond);
Lck_Lock(&tp->mtx);
while (vtp->state == VTP_STATE_STOLEN)
AZ(Lck_CondWait(&wrk->cond, &tp->mtx, 0));
while (vtp->state == VTP_STATE_STOLEN) {
r = Lck_CondWait(&wrk->cond, &tp->mtx, tmo);
if (r != 0) {
if (r == EINTR)
continue;
assert(r == ETIMEDOUT);
Lck_Unlock(&tp->mtx);
return (1);
}
}
assert(vtp->state == VTP_STATE_USED);
vtp->cond = NULL;
Lck_Unlock(&tp->mtx);

return (0);
}

/*--------------------------------------------------------------------*/
Expand Down
2 changes: 1 addition & 1 deletion bin/varnishd/cache/cache_tcp_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct vtp *VTP_Get(struct tcp_pool *, double tmo, struct worker *,
* Get a (possibly) recycled connection.
*/

void VTP_Wait(struct worker *, struct vtp *);
int VTP_Wait(struct worker *, struct vtp *, double tmo);
/*
* If the connection was recycled (state != VTP_STATE_USED) call this
* function before attempting to receive on the connection.
Expand Down
24 changes: 24 additions & 0 deletions bin/varnishtest/tests/r01772.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
varnishtest "#1772: Honor first_byte_timeout on a recycled connection"

server s1 {
rxreq
expect req.url == "/first"
txresp

rxreq
expect req.url == "/second"
delay 2
txresp
} -start

varnish v1 -arg "-p first_byte_timeout=1" -vcl+backend {} -start

client c1 {
txreq -url "/first"
rxresp
expect resp.status == 200

txreq -url "/second"
rxresp
expect resp.status == 503
} -run

0 comments on commit 68bdb81

Please sign in to comment.