mirror of
https://github.com/zevenet/zproxy.git
synced 2025-10-26 11:19:19 +00:00
session: remove defunct
Remove the defunct variable from zproxy_session_node, and simply use refcnt. Signed-off-by: Nicolás A. Ortega Froysa <nicolas.ortega@zevenet.com>
This commit is contained in:
parent
009d478d63
commit
d2ba5edb99
@ -42,7 +42,7 @@ struct zproxy_session_node {
|
||||
// last_seen is used to calculate if the session has expired.
|
||||
// If it has the value 0 means that the session does not expired, it is permanent
|
||||
unsigned int timestamp;
|
||||
bool defunct;
|
||||
struct zproxy_sessions *sessions_group;
|
||||
int refcnt;
|
||||
};
|
||||
|
||||
@ -57,7 +57,7 @@ void zproxy_sessions_free(struct zproxy_sessions *sessions);
|
||||
* @param key Unique key that identifies the session.
|
||||
*
|
||||
* @return A pointer to the session that must be released with
|
||||
* zproxy_session_release(). If not found it will return NULL.
|
||||
* zproxy_session_free(). If not found it will return NULL.
|
||||
*/
|
||||
struct zproxy_session_node *
|
||||
zproxy_session_get(struct zproxy_sessions *sessions, const char *key);
|
||||
@ -69,7 +69,7 @@ zproxy_session_get(struct zproxy_sessions *sessions, const char *key);
|
||||
* @param bck Backend associated with the new session.
|
||||
*
|
||||
* @return A pointer to the session that must be released with
|
||||
* zproxy_session_release(). If fails to add it will return NULL.
|
||||
* zproxy_session_free(). If fails to add it will return NULL.
|
||||
*/
|
||||
struct zproxy_session_node *
|
||||
zproxy_session_add(struct zproxy_sessions *sessions, const char *key,
|
||||
@ -79,7 +79,7 @@ zproxy_session_add(struct zproxy_sessions *sessions, const char *key,
|
||||
*
|
||||
* @param session Session to release.
|
||||
*/
|
||||
void zproxy_session_release(struct zproxy_session_node **session);
|
||||
void zproxy_session_free(struct zproxy_session_node **session);
|
||||
void zproxy_sessions_remove_expired(struct zproxy_sessions *sessions);
|
||||
void zproxy_session_delete_backend(struct zproxy_sessions *sessions, const struct sockaddr_in *bck);
|
||||
int zproxy_session_delete(struct zproxy_sessions *sessions, const char *key);
|
||||
|
||||
@ -352,7 +352,7 @@ static enum ws_responses handle_patch(const std::string &req_path,
|
||||
return WS_HTTP_500;
|
||||
}
|
||||
sess->timestamp = i.last_seen;
|
||||
zproxy_session_release(&sess);
|
||||
zproxy_session_free(&sess);
|
||||
}
|
||||
|
||||
zproxy_state_release(&state);
|
||||
@ -480,7 +480,7 @@ static enum ws_responses handle_patch(const std::string &req_path,
|
||||
return WS_HTTP_500;
|
||||
}
|
||||
sess->timestamp = k.last_seen;
|
||||
zproxy_session_release(&sess);
|
||||
zproxy_session_free(&sess);
|
||||
}
|
||||
}
|
||||
zproxy_state_release(&state);
|
||||
@ -644,7 +644,7 @@ static enum ws_responses handle_put(const std::string &req_path,
|
||||
}
|
||||
if (last_seen >= 0)
|
||||
session->timestamp = last_seen;
|
||||
zproxy_session_release(&session);
|
||||
zproxy_session_free(&session);
|
||||
zproxy_state_release(&state);
|
||||
} else if (zproxy_regex_exec(API_REGEX_SELECT_SERVICE_BACKENDS,
|
||||
req_path.c_str(), matches)) {
|
||||
|
||||
@ -593,7 +593,7 @@ static void zproxy_http_manage_set_cookie(HttpStream *stream, std::string sessio
|
||||
|
||||
session = zproxy_session_add(stream->session, session_key.data(),
|
||||
&stream->backend_config->runtime.addr);
|
||||
zproxy_session_release(&session);
|
||||
zproxy_session_free(&session);
|
||||
}
|
||||
|
||||
validation::REQUEST_RESULT http_manager::validateResponse(HttpStream *stream)
|
||||
|
||||
@ -137,8 +137,6 @@ static json_t *serialize_service_sessions(const struct zproxy_service_cfg *servi
|
||||
pthread_mutex_lock(&sessions->sessions_mutex);
|
||||
for (int i = 0; i < HASH_SESSION_SLOTS; i++) {
|
||||
list_for_each_entry(session, &sessions->session_hashtable[i], hlist) {
|
||||
if (session->defunct)
|
||||
continue;
|
||||
json_array_append_new(jsessions,
|
||||
serialize_session(session, service_cfg));
|
||||
}
|
||||
|
||||
@ -500,7 +500,7 @@ zproxy_service_select_backend(struct zproxy_service_cfg *service_config,
|
||||
session = zproxy_session_get(sessions, session_key.data());
|
||||
if (session) {
|
||||
selected_backend = zproxy_service_backend_session(service_config, &session->bck_addr, http_state);
|
||||
zproxy_session_release(&session);
|
||||
zproxy_session_free(&session);
|
||||
if (selected_backend)
|
||||
return selected_backend;
|
||||
}
|
||||
@ -511,7 +511,7 @@ zproxy_service_select_backend(struct zproxy_service_cfg *service_config,
|
||||
if (selected_backend && !session_key.empty() &&
|
||||
service_config->session.sess_type != SESS_TYPE::SESS_NONE) {
|
||||
session = zproxy_session_add(sessions, session_key.data(), &selected_backend->runtime.addr);
|
||||
zproxy_session_release(&session);
|
||||
zproxy_session_free(&session);
|
||||
}
|
||||
|
||||
return selected_backend;
|
||||
|
||||
@ -20,6 +20,7 @@
|
||||
#include "zcu_log.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
#include <sys/syslog.h>
|
||||
|
||||
@ -31,10 +32,9 @@ void zproxy_sessions_dump(struct zproxy_sessions *sessions)
|
||||
for (int i = 0; i < HASH_SESSION_SLOTS; i++) {
|
||||
list_for_each_entry(cur, &sessions->session_hashtable[i], hlist) {
|
||||
zcu_log_print(LOG_DEBUG,
|
||||
"** sessions[%d]: %s (bck=%s:%d;l-s=%d;d=%s)",
|
||||
"** sessions[%d]: %s (bck=%s:%d;l-s=%d)",
|
||||
i, cur->key, inet_ntoa(cur->bck_addr.sin_addr),
|
||||
ntohs(cur->bck_addr.sin_port), cur->timestamp,
|
||||
cur->defunct ? "true" : "false");
|
||||
ntohs(cur->bck_addr.sin_port), cur->timestamp);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&sessions->sessions_mutex);
|
||||
@ -57,15 +57,12 @@ static int zproxy_session_is_expired(struct zproxy_session_node *session, unsign
|
||||
return time(NULL) - session->timestamp > ttl;
|
||||
}
|
||||
|
||||
static int zproxy_session_free(struct zproxy_session_node *session)
|
||||
static void _zproxy_session_free(struct zproxy_session_node *session)
|
||||
{
|
||||
if (session->refcnt == 0) {
|
||||
if (--session->refcnt == 0) {
|
||||
list_del(&session->hlist);
|
||||
session->sessions_group->size--;
|
||||
free(session);
|
||||
return 1;
|
||||
} else {
|
||||
session->defunct = true;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,17 +92,18 @@ void zproxy_sessions_flush(struct zproxy_sessions *sessions)
|
||||
int i;
|
||||
|
||||
pthread_mutex_lock(&sessions->sessions_mutex);
|
||||
sessions->size = 0;
|
||||
for (i = 0; i < HASH_SESSION_SLOTS; i++) {
|
||||
list_for_each_entry_safe(session, next, &sessions->session_hashtable[i], hlist)
|
||||
zproxy_session_free(session);
|
||||
_zproxy_session_free(session);
|
||||
}
|
||||
sessions->size = 0;
|
||||
pthread_mutex_unlock(&sessions->sessions_mutex);
|
||||
}
|
||||
|
||||
void zproxy_sessions_free(struct zproxy_sessions *sessions)
|
||||
{
|
||||
zproxy_sessions_flush(sessions);
|
||||
pthread_mutex_destroy(&sessions->sessions_mutex);
|
||||
free(sessions);
|
||||
}
|
||||
|
||||
@ -115,7 +113,7 @@ static struct zproxy_session_node *_zproxy_session_get(struct zproxy_sessions *s
|
||||
int hash = djb_hash(key) % HASH_SESSION_SLOTS;
|
||||
|
||||
list_for_each_entry(cur, &sessions->session_hashtable[hash], hlist) {
|
||||
if (!cur->defunct && strncmp(cur->key, key, strlen(cur->key)+1) == 0)
|
||||
if (strncmp(cur->key, key, strlen(cur->key)+1) == 0)
|
||||
return cur;
|
||||
}
|
||||
|
||||
@ -159,7 +157,8 @@ struct zproxy_session_node *zproxy_session_add(struct zproxy_sessions *sessions,
|
||||
snprintf(session->key, sizeof(session->key), "%s", key);
|
||||
memcpy(&session->bck_addr, bck, sizeof(struct sockaddr_in));
|
||||
session->timestamp = time(NULL);
|
||||
session->defunct = false;
|
||||
session->sessions_group = sessions;
|
||||
session->refcnt = 1;
|
||||
|
||||
sessions->size++;
|
||||
|
||||
@ -172,12 +171,18 @@ struct zproxy_session_node *zproxy_session_add(struct zproxy_sessions *sessions,
|
||||
return session;
|
||||
}
|
||||
|
||||
void zproxy_session_release(struct zproxy_session_node **session)
|
||||
void zproxy_session_free(struct zproxy_session_node **session)
|
||||
{
|
||||
if (!*session)
|
||||
return;
|
||||
|
||||
(*session)->refcnt--;
|
||||
struct zproxy_sessions *sessions =
|
||||
(*session)->sessions_group;
|
||||
pthread_mutex_lock(&sessions->sessions_mutex);
|
||||
_zproxy_session_free(*session);
|
||||
pthread_mutex_unlock(&sessions->sessions_mutex);
|
||||
|
||||
*session = NULL;
|
||||
}
|
||||
|
||||
void zproxy_sessions_remove_expired(struct zproxy_sessions *sessions)
|
||||
@ -188,38 +193,28 @@ void zproxy_sessions_remove_expired(struct zproxy_sessions *sessions)
|
||||
pthread_mutex_lock(&sessions->sessions_mutex);
|
||||
for (i = 0; i < HASH_SESSION_SLOTS; i++) {
|
||||
list_for_each_entry_safe(session, next, &sessions->session_hashtable[i], hlist) {
|
||||
if (zproxy_session_is_expired(session, sessions->ttl)) {
|
||||
if (zproxy_session_free(session) < 0)
|
||||
sessions->size--;
|
||||
}
|
||||
if (zproxy_session_is_expired(session, sessions->ttl))
|
||||
_zproxy_session_free(session);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&sessions->sessions_mutex);
|
||||
}
|
||||
|
||||
static int _zproxy_session_delete(struct zproxy_sessions *sessions, const char *key)
|
||||
int zproxy_session_delete(struct zproxy_sessions *sessions, const char *key)
|
||||
{
|
||||
struct zproxy_session_node *session;
|
||||
|
||||
session = _zproxy_session_get(sessions, key);
|
||||
if (!session)
|
||||
return -1;
|
||||
|
||||
if (zproxy_session_free(session) < 0)
|
||||
sessions->size--;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zproxy_session_delete(struct zproxy_sessions *sessions, const char *key)
|
||||
{
|
||||
int ret;
|
||||
|
||||
pthread_mutex_lock(&sessions->sessions_mutex);
|
||||
ret = _zproxy_session_delete(sessions, key);
|
||||
session = _zproxy_session_get(sessions, key);
|
||||
if (!session) {
|
||||
pthread_mutex_unlock(&sessions->sessions_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
_zproxy_session_free(session);
|
||||
pthread_mutex_unlock(&sessions->sessions_mutex);
|
||||
|
||||
return ret;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int zproxy_session_update(struct zproxy_sessions *sessions, const char *key, const struct sockaddr_in *bck, unsigned int timestamp)
|
||||
@ -248,10 +243,8 @@ void zproxy_session_delete_backend(struct zproxy_sessions *sessions, const struc
|
||||
pthread_mutex_lock(&sessions->sessions_mutex);
|
||||
for (i = 0; i < HASH_SESSION_SLOTS; i++) {
|
||||
list_for_each_entry_safe(session, next, &sessions->session_hashtable[i], hlist) {
|
||||
if (memcmp(&session->bck_addr, bck, sizeof(struct sockaddr_in)) == 0) {
|
||||
if (zproxy_session_free(session) < 0)
|
||||
sessions->size--;
|
||||
}
|
||||
if (memcmp(&session->bck_addr, bck, sizeof(struct sockaddr_in)) == 0)
|
||||
_zproxy_session_free(session);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&sessions->sessions_mutex);
|
||||
@ -265,10 +258,8 @@ void zproxy_session_delete_old_backends(const struct zproxy_service_cfg *service
|
||||
pthread_mutex_lock(&sessions->sessions_mutex);
|
||||
for (i = 0; i < HASH_SESSION_SLOTS; i++) {
|
||||
list_for_each_entry_safe(session, next, &sessions->session_hashtable[i], hlist) {
|
||||
if (!zproxy_backend_cfg_lookup(service, &session->bck_addr)) {
|
||||
if (zproxy_session_free(session) < 0)
|
||||
sessions->size--;
|
||||
}
|
||||
if (!zproxy_backend_cfg_lookup(service, &session->bck_addr))
|
||||
_zproxy_session_free(session);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&sessions->sessions_mutex);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user