Skip to content

Commit

Permalink
Add option to build libevhtp
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Mar 12, 2024
1 parent f4bcd33 commit e0ee240
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 30 deletions.
10 changes: 5 additions & 5 deletions common/fs-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ chunking_worker (gpointer vdata, gpointer user_data)
if (chunk->result < 0)
goto out;

idx = chunk->offset / seaf->http_server->fixed_block_size;
idx = chunk->offset / seaf->fixed_block_size;
memcpy (data->blk_sha1s + idx * CHECKSUM_LENGTH, chunk->checksum, CHECKSUM_LENGTH);

out:
Expand Down Expand Up @@ -688,7 +688,7 @@ split_file_to_block (const char *repo_id,
CDCDescriptor *chunk;
int ret = 0;

n_blocks = (file_size + seaf->http_server->fixed_block_size - 1) / seaf->http_server->fixed_block_size;
n_blocks = (file_size + seaf->fixed_block_size - 1) / seaf->fixed_block_size;
block_sha1s = g_new0 (uint8_t, n_blocks * CHECKSUM_LENGTH);
if (!block_sha1s) {
seaf_warning ("Failed to allocate block_sha1s.\n");
Expand All @@ -708,7 +708,7 @@ split_file_to_block (const char *repo_id,
data.finished_tasks = finished_tasks;

tpool = g_thread_pool_new (chunking_worker, &data,
seaf->http_server->max_indexing_threads, FALSE, NULL);
seaf->max_indexing_threads, FALSE, NULL);
if (!tpool) {
seaf_warning ("Failed to allocate thread pool\n");
ret = -1;
Expand All @@ -719,7 +719,7 @@ split_file_to_block (const char *repo_id,
guint64 len;
guint64 left = (guint64)file_size;
while (left > 0) {
len = ((left >= seaf->http_server->fixed_block_size) ? seaf->http_server->fixed_block_size : left);
len = ((left >= seaf->fixed_block_size) ? seaf->fixed_block_size : left);

chunk = g_new0 (CDCDescriptor, 1);
chunk->offset = offset;
Expand All @@ -739,7 +739,7 @@ split_file_to_block (const char *repo_id,
goto out;
}
if (indexed)
*indexed += seaf->http_server->fixed_block_size;
*indexed += seaf->fixed_block_size;

if ((--n_pending) <= 0) {
if (indexed)
Expand Down
2 changes: 2 additions & 0 deletions common/merge-new.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "common.h"

#include "seafile-session.h"
#include "merge-new.h"
#include "vc-common.h"
Expand Down
10 changes: 10 additions & 0 deletions common/rpc-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -1436,15 +1436,23 @@ seafile_web_query_access_token (const char *token, GError **error)
char *
seafile_query_zip_progress (const char *token, GError **error)
{
#ifdef HAVE_EVHTP
return zip_download_mgr_query_zip_progress (seaf->zip_download_mgr,
token, error);
#else
return NULL;
#endif
}

int
seafile_cancel_zip_task (const char *token, GError **error)
{
#ifdef HAVE_EVHTP
return zip_download_mgr_cancel_zip_task (seaf->zip_download_mgr,
token);
#else
return 0;
#endif
}

int
Expand Down Expand Up @@ -3751,7 +3759,9 @@ seafile_delete_repo_tokens_by_peer_id(const char *email,
return -1;
}

#ifdef HAVE_EVHTP
seaf_http_server_invalidate_tokens(seaf->http_server, tokens);
#endif
g_list_free_full (tokens, (GDestroyNotify)g_free);
return 0;
}
Expand Down
13 changes: 3 additions & 10 deletions server/http-server.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "common.h"

#ifdef HAVE_EVHTP
#include <pthread.h>
#include <string.h>
#include <jansson.h>
Expand All @@ -12,9 +13,7 @@
#include <event.h>
#endif

#ifdef HAVE_EVHTP
#include <evhtp.h>
#endif

#include <jwt.h>

Expand Down Expand Up @@ -63,11 +62,9 @@
#define FS_ID_LIST_TOKEN_LEN 36

struct _HttpServer {
#ifdef HAVE_EVHTP
evbase_t *evbase;
evhtp_t *evhtp;
event_t *reap_timer;
#endif
pthread_t thread_id;

GHashTable *token_cache;
Expand Down Expand Up @@ -295,7 +292,6 @@ load_http_config (HttpServerStruct *htp_server, SeafileSession *session)
}
}

#ifdef HAVE_EVHTP
static int
validate_token (HttpServer *htp_server, evhtp_request_t *req,
const char *repo_id, char **username,
Expand Down Expand Up @@ -2876,7 +2872,6 @@ http_request_init (HttpServerStruct *server)
if (upload_file_init (priv->evhtp, server->http_temp_dir) < 0)
exit(-1);
}
#endif

static void
token_cache_value_free (gpointer data)
Expand Down Expand Up @@ -2968,7 +2963,6 @@ remove_expire_cache_cb (evutil_socket_t sock, short type, void *data)
static void *
http_server_run (void *arg)
{
#ifdef HAVE_EVHTP
HttpServerStruct *server = arg;
HttpServer *priv = server->priv;

Expand Down Expand Up @@ -2998,7 +2992,6 @@ http_server_run (void *arg)

event_base_loop (priv->evbase, 0);

#endif
return NULL;
}

Expand All @@ -3008,10 +3001,8 @@ seaf_http_server_new (struct _SeafileSession *session)
HttpServerStruct *server = g_new0 (HttpServerStruct, 1);
HttpServer *priv = g_new0 (HttpServer, 1);

#ifdef HAVE_EVHTP
priv->evbase = NULL;
priv->evhtp = NULL;
#endif

load_http_config (server, session);

Expand Down Expand Up @@ -3163,3 +3154,5 @@ seaf_http_server_invalidate_tokens (HttpServerStruct *htp_server,
pthread_mutex_unlock (&htp_server->priv->token_cache_lock);
return 0;
}

#endif
2 changes: 1 addition & 1 deletion server/http-server.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef HTTP_SERVER_H
#define HTTP_SERVER_H

#ifdef HAVE_EVHTP
#include <glib.h>

struct _SeafileSession;
Expand Down Expand Up @@ -36,7 +37,6 @@ int
seaf_http_server_invalidate_tokens (HttpServerStruct *htp_server,
const GList *tokens);

#ifdef HAVE_EVHTP
void
send_statistic_msg (const char *repo_id, char *user, char *operation, guint64 bytes);
#endif
Expand Down
2 changes: 1 addition & 1 deletion server/index-blocks-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ index_blocks_mgr_new (SeafileSession *session)

priv->idx_tpool = g_thread_pool_new (start_index_task,
priv,
session->http_server->max_index_processing_threads,
session->max_index_processing_threads,
FALSE, &error);
if (!priv->idx_tpool) {
if (error) {
Expand Down
2 changes: 2 additions & 0 deletions server/pack-dir.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common.h"
#ifdef HAVE_EVHTP

#define DEBUG_FLAG SEAFILE_DEBUG_HTTP
#include "log.h"
Expand Down Expand Up @@ -481,3 +482,4 @@ pack_files (const char *store_id,

return ret;
}
#endif
2 changes: 2 additions & 0 deletions server/pack-dir.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef PACK_DIR_H
#define PACK_DIR_H
#ifdef HAVE_EVHTP

/* Pack a seafile directory to a zipped archive, saved in a temporary file.
Return the path of this temporary file.
Expand All @@ -23,5 +24,6 @@ pack_files (const char *store_id,
SeafileCrypt *crypt,
gboolean is_windows,
Progress *progress);
#endif

#endif
4 changes: 4 additions & 0 deletions server/repo-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,9 @@ seaf_repo_manager_delete_token (SeafRepoManager *mgr,

GList *tokens = NULL;
tokens = g_list_append (tokens, g_strdup(token));
#ifdef HAVE_EVHTP
seaf_http_server_invalidate_tokens (seaf->http_server, tokens);
#endif
g_list_free_full (tokens, (GDestroyNotify)g_free);

return 0;
Expand Down Expand Up @@ -1838,7 +1840,9 @@ seaf_repo_manager_delete_repo_tokens_by_email (SeafRepoManager *mgr,
goto out;
}

#ifdef HAVE_EVHTP
seaf_http_server_invalidate_tokens (seaf->http_server, token_list);
#endif

out:
g_list_free_full (token_list, (GDestroyNotify)g_free);
Expand Down
80 changes: 69 additions & 11 deletions server/seafile-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,67 @@

#define DEFAULT_THREAD_POOL_SIZE 500

#define DEFAULT_FIXED_BLOCK_SIZE ((gint64)1 << 23) /* 8MB */

static void
load_fileserver_config (SeafileSession *session)
{
int web_token_expire_time;
int max_index_processing_threads;
int fixed_block_size_mb;
int max_indexing_threads;

session->go_fileserver = g_key_file_get_boolean (session->config,
"fileserver", "use_go_fileserver",
NULL);
if (session->go_fileserver) {
char *type = NULL;
type = g_key_file_get_string (session->config, "database", "type", NULL);
if (!type || g_strcmp0 (type, "mysql") != 0) {
session->go_fileserver = FALSE;
}
g_free (type);
}

web_token_expire_time = g_key_file_get_integer (session->config,
"fileserver", "web_token_expire_time",
NULL);
if (web_token_expire_time <= 0) {
session->web_token_expire_time = 3600;
} else {
session->web_token_expire_time = web_token_expire_time;
}

max_index_processing_threads = g_key_file_get_integer (session->config,
"fileserver", "max_index_processing_threads",
NULL);
if (max_index_processing_threads <= 0) {
session->max_index_processing_threads = 3;
} else {
session->max_index_processing_threads = max_index_processing_threads;
}

fixed_block_size_mb = g_key_file_get_integer (session->config,
"fileserver", "fixed_block_size",
NULL);
if (fixed_block_size_mb <= 0){
session->fixed_block_size = DEFAULT_FIXED_BLOCK_SIZE;
} else {
session->fixed_block_size = fixed_block_size_mb * ((gint64)1 << 20);
}

max_indexing_threads = g_key_file_get_integer (session->config,
"fileserver", "max_indexing_threads",
NULL);
if (max_indexing_threads <= 0) {
session->max_indexing_threads = 1;
} else {
session->max_indexing_threads = max_indexing_threads;
}

return;
}

SeafileSession *
seafile_session_new(const char *central_config_dir,
const char *seafile_dir,
Expand Down Expand Up @@ -115,17 +176,7 @@ seafile_session_new(const char *central_config_dir,
"general", "cloud_mode",
NULL);

session->go_fileserver = g_key_file_get_boolean (config,
"fileserver", "use_go_fileserver",
NULL);
if (session->go_fileserver) {
char *type = NULL;
type = g_key_file_get_string (config, "database", "type", NULL);
if (!type || g_strcmp0 (type, "mysql") != 0) {
session->go_fileserver = FALSE;
}
g_free (type);
}
load_fileserver_config (session);

notif_enabled = g_key_file_get_boolean (config,
"notification", "enabled",
Expand Down Expand Up @@ -201,13 +252,15 @@ seafile_session_new(const char *central_config_dir,
if (!session->mq_mgr)
goto onerror;

#ifdef HAVE_EVHTP
session->http_server = seaf_http_server_new (session);
if (!session->http_server)
goto onerror;

session->zip_download_mgr = zip_download_mgr_new ();
if (!session->zip_download_mgr)
goto onerror;
#endif

session->index_blocks_mgr = index_blocks_mgr_new (session);
if (!session->index_blocks_mgr)
Expand Down Expand Up @@ -324,10 +377,15 @@ seafile_session_start (SeafileSession *session)
}

if (!session->go_fileserver) {
#ifdef HAVE_EVHTP
if (seaf_http_server_start (session->http_server) < 0) {
seaf_warning ("Failed to start http server thread.\n");
return -1;
}
#else
seaf_warning ("Failed to start http server thread, please use go fileserver.\n");
return -1;
#endif
}

return 0;
Expand Down
7 changes: 7 additions & 0 deletions server/seafile-session.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,22 @@ struct _SeafileSession {

int cloud_mode;

#ifdef HAVE_EVHTP
HttpServerStruct *http_server;
ZipDownloadMgr *zip_download_mgr;
#endif
IndexBlksMgr *index_blocks_mgr;

gboolean create_tables;
gboolean ccnet_create_tables;

gboolean go_fileserver;

int web_token_expire_time;
int max_index_processing_threads;
gint64 fixed_block_size;
int max_indexing_threads;

// For notification server
NotifManager *notif_mgr;
char *private_key;
Expand Down
4 changes: 3 additions & 1 deletion server/web-accesstoken-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ seaf_web_at_manager_get_access_token (SeafWebAccessTokenManager *mgr,
pthread_mutex_lock (&mgr->priv->lock);

t = gen_new_token (mgr->priv->access_token_hash);
expire = now + seaf->http_server->web_token_expire_time;
expire = now + seaf->web_token_expire_time;

info = g_new0 (AccessInfo, 1);
info->repo_id = g_strdup (repo_id);
Expand All @@ -176,6 +176,7 @@ seaf_web_at_manager_get_access_token (SeafWebAccessTokenManager *mgr,

pthread_mutex_unlock (&mgr->priv->lock);

#ifdef HAVE_EVHTP
if (!seaf->go_fileserver) {
if (strcmp(op, "download-dir") == 0 ||
strcmp(op, "download-multi") == 0 ||
Expand All @@ -202,6 +203,7 @@ seaf_web_at_manager_get_access_token (SeafWebAccessTokenManager *mgr,
g_object_unref (webaccess);
}
}
#endif

return t;
}
Expand Down
Loading

0 comments on commit e0ee240

Please sign in to comment.