Skip to content

Commit

Permalink
fixes around reverse client with timings
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed May 28, 2024
1 parent 3c3270c commit 04aa5ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
18 changes: 15 additions & 3 deletions tunnels/client/reverse/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ enum
{
kPreconnectDelayShort = 10,
kPreconnectDelayHigh = 750,
kConnectionStarvationTimeOut = 5 * 1000,
kConnectionStarvationTimeOutAfterFirstConfirmation = 25 * 1000
kConnectionStarvationTimeOut = 3000,
kConnectionStarvationTimeOutAfterFirstConfirmation = 15000
};

static void onLinePausedU(void *cstate)
Expand Down Expand Up @@ -142,17 +142,29 @@ static void initiateConnect(tunnel_t *self, uint8_t tid, bool delay)

static void onStarvedConnectionExpire(idle_item_t *idle_con)
{
LOGW("ReverseClient: a starved connection detected and closed");
reverse_client_con_state_t *cstate = idle_con->userdata;
tunnel_t *self = cstate->self;
reverse_client_state_t *state = STATE(self);
if (cstate->idle_handle_removed){
//this can happen if we are unlucky and 2 events are passed to eventloop in
// a bad order, first connection to peer succeds and also the starvation cb call
// is already in the queue
assert(cstate->pair_connected);
return;
}

if (cstate->handshaked)
{
assert(! cstate->pair_connected);
if (state->unused_cons[cstate->u->tid] > 0)
{
state->unused_cons[cstate->u->tid] -= 1;
}
LOGW("ReverseClient: a idle connection detected and closed");
}
else
{
LOGW("ReverseClient: a starved connection detected and closed");
}

cstate->idle_handle_removed = true;
Expand Down
20 changes: 12 additions & 8 deletions tunnels/client/reverse/reverse_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ static void downStream(tunnel_t *self, context_t *c)
state->unused_cons[tid] += 1;
LOGI("ReverseClient: connected, tid: %d unused: %u active: %d", tid, state->unused_cons[tid],
atomic_load_explicit(&(state->reverse_cons), memory_order_relaxed));
idle_item_t *con_idle = getIdleItemByHash(tid, state->starved_connections, (hash_t) (ucstate));
if (con_idle != NULL)
{
keepIdleItemForAtleast(state->starved_connections,con_idle,
kConnectionStarvationTimeOutAfterFirstConfirmation);
}

if (bufLen(c->payload) > 0)
{
idle_item_t *con_idle = getIdleItemByHash(tid, state->starved_connections, (hash_t) (ucstate));
if (con_idle != NULL)
{
keepIdleItemForAtleast(state->starved_connections, con_idle,
kConnectionStarvationTimeOutAfterFirstConfirmation);
}
continue;
}
reuseContextBuffer(c);
Expand Down Expand Up @@ -174,9 +175,12 @@ static void downStream(tunnel_t *self, context_t *c)
idle_item_t *con_idle_item =
newIdleItem(state->starved_connections, (hash_t) (ucstate), ucstate, onStarvedConnectionExpire,
c->line->tid, kConnectionStarvationTimeOut);
ucstate->idle_handle_removed = false;

(void) con_idle_item;
if (con_idle_item)
{
ucstate->idle_handle_removed = false;
}

destroyContext(c);
}
else
Expand Down

0 comments on commit 04aa5ec

Please sign in to comment.