Skip to content

Thread Options #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ LOAD httpserver;

### 🔌 Usage
Start the HTTP server providing the `host`, `port` and `auth` parameters.<br>
> If you want no authhentication, just pass an empty string.
> * If you want no authentication, just pass an empty string as parameter.<br>
> * If you want the API run in foreground set `DUCKDB_HTTPSERVER_FOREGROUND=1`

#### Basic Auth
```sql
Expand Down
33 changes: 31 additions & 2 deletions src/httpserver_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,41 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
});

string host_str = host.GetString();
global_state.server_thread = make_uniq<std::thread>([host_str, port]() {

const char* run_in_same_thread_env = std::getenv("DUCKDB_HTTPSERVER_FOREGROUND");
bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string(run_in_same_thread_env) == "1");

if (run_in_same_thread) {
#ifdef _WIN32
throw IOException("Foreground mode not yet supported on WIN32 platforms.");
#else
// POSIX signal handler for SIGINT (Linux/macOS)
signal(SIGINT, [](int) {
if (global_state.server) {
global_state.server->stop();
}
global_state.is_running = false; // Update the running state
});

// Run the server in the same thread
if (!global_state.server->listen(host_str.c_str(), port)) {
global_state.is_running = false;
throw IOException("Failed to start HTTP server on " + host_str + ":" + std::to_string(port));
}
});
#endif

// The server has stopped (due to CTRL-C or other reasons)
global_state.is_running = false;
} else {
// Run the server in a dedicated thread (default)
global_state.server_thread = make_uniq<std::thread>([host_str, port]() {
if (!global_state.server->listen(host_str.c_str(), port)) {
global_state.is_running = false;
throw IOException("Failed to start HTTP server on " + host_str + ":" + std::to_string(port));
}
});
}

}

void HttpServerStop() {
Expand Down
Loading