Skip to content

Commit

Permalink
Merge pull request #8 from lmangani/thread-options
Browse files Browse the repository at this point in the history
Thread Options
  • Loading branch information
lmangani authored Oct 19, 2024
2 parents 99b74d1 + ecf9362 commit 9f075f6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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

0 comments on commit 9f075f6

Please sign in to comment.