Split server in smaller modules

This commit is contained in:
Yves
2025-02-21 12:46:41 +01:00
parent ae104f697a
commit f1cc3c667d
10 changed files with 339 additions and 245 deletions

View File

@@ -0,0 +1,32 @@
#pragma once
#include <mutex>
namespace duckdb_httplib_openssl {
class DataSink;
}
namespace duckdb {
namespace ui {
class EventDispatcher {
public:
void SendConnectedEvent(const std::string &token);
void SendCatalogChangedEvent();
bool WaitEvent(duckdb_httplib_openssl::DataSink *sink);
void Close();
private:
void SendEvent(const std::string &message);
std::mutex mutex;
std::condition_variable cv;
std::atomic_int next_id{0};
std::atomic_int current_id{-1};
std::atomic_int wait_count{0};
std::string message;
std::atomic_bool closed{false};
};
} // namespace ui
} // namespace duckdb

View File

@@ -1,6 +1,6 @@
#pragma once
#include "duckdb.hpp"
#include <duckdb.hpp>
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.hpp"
@@ -11,6 +11,9 @@
#include <thread>
#include <unordered_map>
#include "watcher.hpp"
#include "event_dispatcher.hpp"
namespace httplib = duckdb_httplib_openssl;
namespace duckdb {
@@ -18,26 +21,6 @@ class MemoryStream;
namespace ui {
struct CatalogState {
std::map<idx_t, optional_idx> db_to_catalog_version;
};
class EventDispatcher {
public:
bool WaitEvent(httplib::DataSink *sink);
void SendEvent(const std::string &message);
void Close();
private:
std::mutex mutex;
std::condition_variable cv;
std::atomic_int next_id{0};
std::atomic_int current_id{-1};
std::atomic_int wait_count{0};
std::string message;
std::atomic_bool closed{false};
};
class HttpServer {
public:
@@ -53,17 +36,14 @@ public:
std::string LocalUrl() const;
private:
friend class Watcher;
// Lifecycle
void DoStart(const uint16_t local_port, const std::string &remote_url);
void DoStop();
void Run();
void UpdateDatabaseInstance(shared_ptr<DatabaseInstance> context_db);
// Watcher
void Watch();
void StartWatcher();
void StopWatcher();
// Http handlers
void HandleGetLocalEvents(const httplib::Request &req,
httplib::Response &res);
@@ -83,10 +63,8 @@ private:
void SetResponseEmptyResult(httplib::Response &res);
void SetResponseErrorResult(httplib::Response &res, const std::string &error);
// Events
void SendEvent(const std::string &message);
void SendConnectedEvent(const std::string &token);
void SendCatalogChangedEvent();
// Misc
shared_ptr<DatabaseInstance> LockDatabaseInstance();
uint16_t local_port;
std::string remote_url;
@@ -94,12 +72,8 @@ private:
std::string user_agent;
httplib::Server server;
unique_ptr<std::thread> main_thread;
unique_ptr<std::thread> watcher_thread;
std::mutex watcher_mutex;
std::condition_variable watcher_cv;
std::atomic<bool> watcher_should_run;
unique_ptr<EventDispatcher> event_dispatcher;
unique_ptr<Watcher> watcher;
static unique_ptr<HttpServer> server_instance;
};

View File

@@ -0,0 +1,8 @@
#pragma once
#include <duckdb.hpp>
namespace duckdb {
bool IsMDConnected(Connection &);
std::string GetMDToken(Connection &);
} // namespace duckdb

28
src/include/watcher.hpp Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include <duckdb.hpp>
#include <thread>
namespace duckdb {
namespace ui {
struct CatalogState {
std::map<idx_t, optional_idx> db_to_catalog_version;
};
class HttpServer;
class Watcher {
public:
Watcher(HttpServer &server);
void Start();
void Stop();
private:
void Watch();
unique_ptr<std::thread> thread;
std::mutex mutex;
std::condition_variable cv;
std::atomic<bool> should_run;
HttpServer &server;
};
} // namespace ui
} // namespace duckdb