Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed May 28, 2024
1 parent d147372 commit 0a6783b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
30 changes: 24 additions & 6 deletions tunnels/client/reverse/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#define CSTATE_U_MUT(x) ((x)->line->chains_state)[self->chain_index]
enum
{
kPreconnectDelayShort = 10,
kPreconnectDelayHigh = 750,
kConnectionStarvationTimeOut = 3000
kPreconnectDelayShort = 10,
kPreconnectDelayHigh = 750,
kConnectionStarvationTimeOut = 5 * 1000,
kConnectionStarvationTimeOutAfterFirstConfirmation = 25 * 1000
};

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

hloop_post_event(worker_loop, &ev);
}

static void onStarvedConnectionExpire(idle_item_t *idle_con)
{
LOGW("ReverseClient: onStarvedConnectionExpire");
LOGW("ReverseClient: a starved connection detected and closed");
reverse_client_con_state_t *cstate = idle_con->userdata;
tunnel_t *self = cstate->self;
cstate->idle_handle_removed = true;
// old connection will expire anyway...
reverse_client_state_t *state = STATE(self);

if (cstate->handshaked)
{
if (state->unused_cons[cstate->u->tid] > 0)
{
state->unused_cons[cstate->u->tid] -= 1;
}
}

cstate->idle_handle_removed = true;
initiateConnect(self, cstate->u->tid, false);

(cstate->u->chains_state)[self->chain_index] = NULL;
(cstate->d->chains_state)[state->chain_index_d] = NULL;

context_t *fc = newFinContext(cstate->u);
cleanup(cstate);
self->up->upStream(self->up, fc);
}
11 changes: 8 additions & 3 deletions tunnels/client/reverse/reverse_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void downStream(tunnel_t *self, context_t *c)

if (! ucstate->idle_handle_removed)
{
ucstate->idle_handle_removed = true;
ucstate->idle_handle_removed = true;
reverse_client_state_t *state = STATE(ucstate->self);
removeIdleItemByHash(ucstate->u->tid, state->starved_connections, (hash_t) (ucstate));
}
Expand Down Expand Up @@ -116,7 +116,12 @@ 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)
{
continue;
Expand Down Expand Up @@ -170,7 +175,7 @@ static void downStream(tunnel_t *self, context_t *c)
newIdleItem(state->starved_connections, (hash_t) (ucstate), ucstate, onStarvedConnectionExpire,
c->line->tid, kConnectionStarvationTimeOut);
ucstate->idle_handle_removed = false;

(void) con_idle_item;
destroyContext(c);
}
Expand Down
2 changes: 1 addition & 1 deletion ww/idle_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ idle_item_t *newIdleItem(idle_table_t *self, hash_t key, void *userdata, ExpireC
void keepIdleItemForAtleast(idle_table_t *self, idle_item_t *item, uint64_t age_ms)
{
hhybridmutex_lock(&(self->mutex));
item->expire_at_ms += self->last_update_ms + age_ms;
item->expire_at_ms = self->last_update_ms + age_ms;
heapq_idles_t_make_heap(&self->hqueue);
hhybridmutex_unlock(&(self->mutex));
}
Expand Down

0 comments on commit 0a6783b

Please sign in to comment.