Skip to content

Commit

Permalink
improvements on preconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Apr 3, 2024
1 parent cc7c153 commit 3f73c9c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
28 changes: 23 additions & 5 deletions tunnels/client/preconnect/preconnect_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static inline void upStream(tunnel_t *self, context_t *c)
preconnect_client_con_state_t *cstate = CSTATE(c);
switch (cstate->mode)
{

case connected_direct:
self->up->upStream(self->up, c);
break;
Expand All @@ -35,20 +35,23 @@ static inline void upStream(tunnel_t *self, context_t *c)
thread_box_t *this_tb = &(state->threads[tid]);
if (c->init)
{
// if (state->chain_index_d == 0)
// state->chain_index_d = reserveChainStateIndex(c->line);

if (this_tb->length > 0)
{
atomic_fetch_add_explicit(&(state->unused_cons), -1, memory_order_relaxed);
atomic_fetch_add_explicit(&(state->active_cons), 1, memory_order_relaxed);

preconnect_client_con_state_t *ucon = this_tb->root.next;
remove_connection(this_tb, ucon);
ucon->d = c->line;
ucon->mode = connected_pair;
CSTATE_MUT(c) = ucon;
self->dw->downStream(self->dw, newEstContext(c->line));
initiateConnect(self);
}
else
{
atomic_fetch_add_explicit(&(state->active_cons), 1, memory_order_relaxed);
preconnect_client_con_state_t *dcon = create_cstate(c->line->tid);
CSTATE_MUT(c) = dcon;
dcon->mode = connected_direct;
Expand All @@ -61,6 +64,7 @@ static inline void upStream(tunnel_t *self, context_t *c)
{
preconnect_client_con_state_t *dcon = CSTATE(c);
CSTATE_MUT(c) = NULL;
atomic_fetch_add_explicit(&(state->active_cons), -1, memory_order_relaxed);

switch (dcon->mode)
{
Expand Down Expand Up @@ -122,21 +126,25 @@ static inline void downStream(tunnel_t *self, context_t *c)

if (c->fin)
{

switch (ucon->mode)
{
case connected_direct:
atomic_fetch_add_explicit(&(state->active_cons), -1, memory_order_relaxed);
destroy_cstate(ucon);
self->dw->downStream(self->dw, c);
break;

case connected_pair:;
atomic_fetch_add_explicit(&(state->active_cons), -1, memory_order_relaxed);
line_t *d_line = ucon->d;
(ucon->d->chains_state)[self->chain_index] = NULL;
destroy_cstate(ucon);
self->dw->downStream(self->dw, switchLine(c, d_line));
break;

case notconnected:
atomic_fetch_add_explicit(&(state->unused_cons), -1, memory_order_relaxed);
remove_connection(this_tb, ucon);
destroy_cstate(ucon);
destroyContext(c);
Expand All @@ -148,11 +156,21 @@ static inline void downStream(tunnel_t *self, context_t *c)

break;
}
LOGD("PreConnectClient: disconnected, unused: %d active: %d", state->unused_cons, STATE(self)->active_cons);
initiateConnect(self);
}
else if (c->est)
{
add_connection(this_tb, ucon);
destroyContext(c);
if (ucon->mode == notconnected)
{
add_connection(this_tb, ucon);
destroyContext(c);
unsigned int unused = atomic_fetch_add_explicit(&(state->unused_cons), 1, memory_order_relaxed);
LOGI("PreConnectClient: connected, unused: %d active: %d", unused + 1, state->active_cons);
initiateConnect(self);
}
else
self->dw->downStream(self->dw, c);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tunnels/client/preconnect/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct thread_box_s

typedef struct preconnect_client_state_s
{
atomic_uint preconnect_cons;
atomic_uint active_cons;
atomic_uint unused_cons;

atomic_uint round_index;
Expand Down
4 changes: 2 additions & 2 deletions tunnels/client/reverse/reverse_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ static inline void downStream(tunnel_t *self, context_t *c)
else if (c->est)
{
CSTATE_U(c)->established = true;
atomic_fetch_add_explicit(&(STATE(self)->unused_cons), 1, memory_order_relaxed);
LOGI("ReverseClient: connected, unused: %d active: %d", STATE(self)->unused_cons, STATE(self)->reverse_cons);
unsigned int unused = atomic_fetch_add_explicit(&(STATE(self)->unused_cons), 1, memory_order_relaxed);
LOGI("ReverseClient: connected, unused: %d active: %d", unused+1, STATE(self)->reverse_cons);
destroyContext(c);
initiateConnect(self);
}
Expand Down

0 comments on commit 3f73c9c

Please sign in to comment.