Skip to content
Open
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
6 changes: 3 additions & 3 deletions lib/src/server/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{path::PathBuf, sync::Arc, thread};

use actix_files as fs;
use actix_web::{web, App, HttpResponse, HttpServer, Responder, Result};
use tokio::runtime::Runtime;
use tokio::runtime::Handle;

use crate::sync::*;

Expand Down Expand Up @@ -73,7 +73,7 @@ async fn metrics(data: web::Data<AppState>) -> impl Responder {

/// Runs an http server on the specified binding address, serving out the supplied server metrics
pub fn run_http_server(
runtime: &Runtime,
handle: &Handle,
address: &str,
content_path: &str,
server_state: Arc<RwLock<ServerState>>,
Expand All @@ -87,7 +87,7 @@ pub fn run_http_server(
// Getting this working was very painful since Actix HttpServer does not implement Send trait, so the
// code has to run on a single thread, but also async and through Tokio.

let runtime_handle = runtime.handle().clone();
let runtime_handle = handle.clone();
thread::spawn(move || {
info!(
"HTTP server is running on http://{}/ to provide OPC UA server metrics",
Expand Down
8 changes: 4 additions & 4 deletions lib/src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ impl Server {
tokio::runtime::Builder::new_current_thread()
};
let runtime = builder.enable_all().build().unwrap();
Self::run_server_on_runtime(runtime, server_task, true);
Self::run_server_on_runtime(runtime.handle(), server_task, true);
}

/// Allow the server to be run on a caller supplied runtime. If block is set, the task
/// runs to completion (abort or by error), otherwise, the task is spawned and a join handle is
/// returned by the function. Spawning might be suitable if the runtime is being used for other
/// async tasks.
pub fn run_server_on_runtime<F>(
runtime: tokio::runtime::Runtime,
handle: &tokio::runtime::Handle,
server_task: F,
block: bool,
) -> Option<tokio::task::JoinHandle<<F as futures::Future>::Output>>
Expand All @@ -257,11 +257,11 @@ impl Server {
F::Output: Send + 'static,
{
if block {
runtime.block_on(server_task);
handle.block_on(server_task);
info!("Server has finished");
None
} else {
Some(runtime.spawn(server_task))
Some(handle.spawn(server_task))
}
}

Expand Down
10 changes: 6 additions & 4 deletions samples/demo-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,27 @@ fn main() {
.build()
.unwrap();

let handle = runtime.handle();

// Start the http server, used for metrics
start_http_server(&runtime, &server, args.content_path.to_str().unwrap());
start_http_server(&handle, &server, args.content_path.to_str().unwrap());

// Run the server. This does not ordinarily exit so you must Ctrl+C to terminate
Server::run_server_on_runtime(
runtime,
handle,
Server::new_server_task(Arc::new(RwLock::new(server))),
true,
);
}
}

fn start_http_server(runtime: &tokio::runtime::Runtime, server: &Server, content_path: &str) {
fn start_http_server(handle: &tokio::runtime::Handle, server: &Server, content_path: &str) {
let server_state = server.server_state();
let connections = server.connections();
let metrics = server.server_metrics();
// The index.html is in a path relative to the working dir.
let _ = http::run_http_server(
runtime,
handle,
"127.0.0.1:8585",
content_path,
server_state,
Expand Down