httpSession.c 4.5 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
slguan 已提交
16 17
#define _DEFAULT_SOURCE
#include "os.h"
H
hzcheng 已提交
18
#include "taos.h"
19 20 21 22 23
#include "tglobal.h"
#include "tcache.h"
#include "httpInt.h"
#include "httpContext.h"
#include "httpSession.h"
H
hzcheng 已提交
24 25

void httpCreateSession(HttpContext *pContext, void *taos) {
26 27
  HttpServer *server = &tsHttpServer;
  httpReleaseSession(pContext);
H
hzcheng 已提交
28

29
  pthread_mutex_lock(&server->serverMutex);
H
hzcheng 已提交
30

H
Hui Li 已提交
31
  HttpSession session;
H
Hui Li 已提交
32
  memset(&session, 0, sizeof(HttpSession));
H
hzcheng 已提交
33
  session.taos = taos;
34
  session.refCount = 1;
H
Haojun Liao 已提交
35
  int32_t len = snprintf(session.id, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass);
S
Shengliang Guan 已提交
36

37
  pContext->session = taosCachePut(server->sessionCache, session.id, len, &session, sizeof(HttpSession), tsHttpSessionExpire * 1000);
38 39
  // void *temp = pContext->session;
  // taosCacheRelease(server->sessionCache, (void **)&temp, false);
S
Shengliang Guan 已提交
40

H
hzcheng 已提交
41 42 43 44
  if (pContext->session == NULL) {
    httpError("context:%p, fd:%d, ip:%s, user:%s, error:%s", pContext, pContext->fd, pContext->ipstr, pContext->user,
              httpMsg[HTTP_SESSION_FULL]);
    taos_close(taos);
S
slguan 已提交
45 46
    pthread_mutex_unlock(&server->serverMutex);
    return;
H
hzcheng 已提交
47 48
  }

49
  httpDebug("context:%p, fd:%d, ip:%s, user:%s, create a new session:%p:%p sessionRef:%d", pContext, pContext->fd,
50
            pContext->ipstr, pContext->user, pContext->session, pContext->session->taos, pContext->session->refCount);
H
hzcheng 已提交
51 52 53
  pthread_mutex_unlock(&server->serverMutex);
}

54 55
static void httpFetchSessionImp(HttpContext *pContext) {
  HttpServer *server = &tsHttpServer;
H
hzcheng 已提交
56 57
  pthread_mutex_lock(&server->serverMutex);

S
slguan 已提交
58
  char sessionId[HTTP_SESSION_ID_LEN];
H
Haojun Liao 已提交
59
  int32_t len = snprintf(sessionId, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass);
60

H
Haojun Liao 已提交
61
  pContext->session = taosCacheAcquireByKey(server->sessionCache, sessionId, len);
62
  if (pContext->session != NULL) {
63
    atomic_add_fetch_32(&pContext->session->refCount, 1);
64
    httpDebug("context:%p, fd:%d, ip:%s, user:%s, find an exist session:%p:%p, sessionRef:%d", pContext, pContext->fd,
65
              pContext->ipstr, pContext->user, pContext->session, pContext->session->taos, pContext->session->refCount);
H
hzcheng 已提交
66
  } else {
67
    httpDebug("context:%p, fd:%d, ip:%s, user:%s, session not found", pContext, pContext->fd, pContext->ipstr,
H
hzcheng 已提交
68 69 70 71 72 73
              pContext->user);
  }

  pthread_mutex_unlock(&server->serverMutex);
}

74
void httpGetSession(HttpContext *pContext) {
S
slguan 已提交
75 76 77 78 79
  if (pContext->session == NULL) {
    httpFetchSessionImp(pContext);
  } else {
    char sessionId[HTTP_SESSION_ID_LEN];
    snprintf(sessionId, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass);
80 81
    httpReleaseSession(pContext);
    httpFetchSessionImp(pContext);
S
slguan 已提交
82 83 84
  }
}

85 86
void httpReleaseSession(HttpContext *pContext) {
  if (pContext == NULL || pContext->session == NULL) return;
H
hzcheng 已提交
87

88
  int32_t refCount = atomic_sub_fetch_32(&pContext->session->refCount, 1);
89
  assert(refCount >= 0);
90
  httpDebug("context:%p, release session:%p:%p, sessionRef:%d", pContext, pContext->session, pContext->session->taos,
91
            pContext->session->refCount);
H
hzcheng 已提交
92

93
  taosCacheRelease(tsHttpServer.sessionCache, (void **)&pContext->session, false);
S
slguan 已提交
94
  pContext->session = NULL;
H
hzcheng 已提交
95 96
}

97
static void httpDestroySession(void *data) {
98
  HttpSession *session = data;
99
  httpDebug("session:%p:%p, is destroyed, sessionRef:%d", session, session->taos, session->refCount);
S
Shengliang Guan 已提交
100

101 102 103
  if (session->taos != NULL) {
    taos_close(session->taos);
    session->taos = NULL;
H
hzcheng 已提交
104 105 106
  }
}

107 108
void httpCleanUpSessions() {
  if (tsHttpServer.sessionCache != NULL) {
109
    SCacheObj *cache = tsHttpServer.sessionCache;
S
TD-1057  
Shengliang Guan 已提交
110
    httpInfo("session cache is cleanuping, size:%" PRIzu "", taosHashGetSize(cache->pHashTable));
111 112
    taosCacheCleanup(tsHttpServer.sessionCache);
    tsHttpServer.sessionCache = NULL;
H
hzcheng 已提交
113 114 115
  }
}

116
bool httpInitSessions() {
H
Haojun Liao 已提交
117
  tsHttpServer.sessionCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, 5, false, httpDestroySession, "rests");
118 119
  if (tsHttpServer.sessionCache == NULL) {
    httpError("failed to init session cache");
H
hzcheng 已提交
120 121 122
    return false;
  }

S
Shengliang Guan 已提交
123
  return true;
H
hzcheng 已提交
124
}