Skip to content

Commit

Permalink
Client removing part #2
Browse files Browse the repository at this point in the history
We can do this, because connection won't
be removed while we have alvie response-request
pair.
  • Loading branch information
const-t committed Feb 26, 2024
1 parent e1130eb commit 913625b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 37 deletions.
33 changes: 9 additions & 24 deletions fw/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,29 @@
static struct {
unsigned int db_size;
const char *db_path;
unsigned int expires_time;
} client_cfg __read_mostly;

/**
* Client tdb entry.
*
* @cli - client descriptor;
* @xff_addr - peer IPv6 address from X-Forwarded-For;
* @expires - expiration time for the client descriptor after all;
* connections are closed;
* @lock - lock for atomic change @expires and @users;
* @lock - lock for atomic change @expired and @users;
* @users - reference counter.
* Expiration state will begind, when the counter reaches
* zero;
* @user_agent_len - Length of @user_agent
* @user_agent - UA_CMP_LEN first characters of User-Agent
* @expired - Expired when client dissconnected;
*/
typedef struct {
TfwClient cli;
TfwAddr xff_addr;
long expires;
spinlock_t lock;
int users;
unsigned long user_agent_len;
char user_agent[UA_CMP_LEN];
bool expired;
} TfwClientEntry;

static TDB *client_db;
Expand Down Expand Up @@ -106,7 +104,8 @@ tfw_client_put(TfwClient *cli)

BUG_ON(!list_empty(&cli->conn_list));

ent->expires = tfw_current_timestamp() + client_cfg.expires_time;
/* Expire descriptor, prepare for removing. */
ent->expired = true;

spin_unlock(&ent->lock);

Expand Down Expand Up @@ -134,7 +133,6 @@ tfw_client_addr_eq(TdbRec *rec, void *data)
TfwClientEntry *ent = (TfwClientEntry *)rec->data;
TfwClient *cli = &ent->cli;
TfwClientEqCtx *ctx = (TfwClientEqCtx *)data;
long curr_time = tfw_current_timestamp();

if (memcmp_fast(&cli->addr.sin6_addr, &ctx->addr.sin6_addr,
sizeof(cli->addr.sin6_addr)))
Expand All @@ -156,14 +154,12 @@ tfw_client_addr_eq(TdbRec *rec, void *data)

spin_lock(&ent->lock);

if (curr_time > ent->expires) {
bzero_fast(&cli->class_prvt, sizeof(cli->class_prvt));
if (ctx->init)
ctx->init(cli);
/* Don't use expired descriptor. It must be evicted from database. */
if (ent->expired) {
spin_unlock(&ent->lock);
return false;
}

ent->expires = LONG_MAX;

++ent->users;
if (ent->users == 1)
TFW_INC_STAT_BH(clnt.online);
Expand All @@ -185,7 +181,6 @@ tfw_client_ent_init(TdbRec *rec, void *data)

spin_lock_init(&ent->lock);

ent->expires = LONG_MAX;
bzero_fast(&cli->class_prvt, sizeof(cli->class_prvt));
if (ctx->init)
ctx->init(cli);
Expand Down Expand Up @@ -294,14 +289,6 @@ tfw_client_for_each(int (*fn)(void *))
return tdb_entry_walk(client_db, fn);
}

void
tfw_client_set_expires_time(unsigned int expires_time)
{
if (client_cfg.expires_time < expires_time + 1) {
client_cfg.expires_time = expires_time + 1;
}
}

static int
tfw_client_start(void)
{
Expand Down Expand Up @@ -363,8 +350,6 @@ TfwMod tfw_client_mod = {
int __init
tfw_client_init(void)
{
client_cfg.expires_time = 1;

tfw_mod_register(&tfw_client_mod);

return 0;
Expand Down
3 changes: 1 addition & 2 deletions fw/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Tempesta FW
*
* Copyright (C) 2014 NatSys Lab. (info@natsys-lab.com).
* Copyright (C) 2015-2022 Tempesta Technologies, Inc.
* Copyright (C) 2015-2024 Tempesta Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -40,7 +40,6 @@ TfwClient *tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr,
TfwStr *user_agent, void (*init)(void *));
void tfw_client_put(TfwClient *cli);
int tfw_client_for_each(int (*fn)(void *));
void tfw_client_set_expires_time(unsigned int expires_time);
void tfw_cli_conn_release(TfwCliConn *cli_conn);
int tfw_cli_conn_send(TfwCliConn *cli_conn, TfwMsg *msg);
int tfw_cli_conn_abort_all(void *data);
Expand Down
5 changes: 0 additions & 5 deletions fw/t/unit/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ ss_stop(void)
{
}

void
tfw_client_set_expires_time(unsigned int expires_time)
{
}

void
tfw_client_put(TfwClient *cli)
{
Expand Down
6 changes: 0 additions & 6 deletions fw/vhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -2108,12 +2108,6 @@ __tfw_cfgop_frang_rsp_code_block(TfwCfgSpec *cs, TfwCfgEntry *ce,
|| frang_parse_ushort(ce->vals[ce->val_n - 1], &cb->tf))
return -EINVAL;

/*
* We need the maximum time frame used by all the limiting logic
* to keep limit accounting data during this time if the connection is
* closed
*/
tfw_client_set_expires_time(cb->tf);
/* Update time frame value to reduce calculations in hot-path. */
cb->tf = (cb->tf * HZ) / FRANG_FREQ;

Expand Down

0 comments on commit 913625b

Please sign in to comment.