Skip to content

Commit

Permalink
use runtime handle instead of runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Burger committed Nov 29, 2024
1 parent fcc89d8 commit 1dc3cc7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
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,

Check warning on line 76 in lib/src/server/http/mod.rs

View check run for this annotation

Codecov / codecov/patch

lib/src/server/http/mod.rs#L76

Added line #L76 was not covered by tests
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();

Check warning on line 90 in lib/src/server/http/mod.rs

View check run for this annotation

Codecov / codecov/patch

lib/src/server/http/mod.rs#L90

Added line #L90 was not covered by tests
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))

Check warning on line 264 in lib/src/server/server.rs

View check run for this annotation

Codecov / codecov/patch

lib/src/server/server.rs#L264

Added line #L264 was not covered by tests
}
}

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();

Check warning on line 150 in samples/demo-server/src/main.rs

View check run for this annotation

Codecov / codecov/patch

samples/demo-server/src/main.rs#L149-L150

Added lines #L149 - L150 were not covered by tests
// 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());

Check warning on line 152 in samples/demo-server/src/main.rs

View check run for this annotation

Codecov / codecov/patch

samples/demo-server/src/main.rs#L152

Added line #L152 was not covered by tests

// Run the server. This does not ordinarily exit so you must Ctrl+C to terminate
Server::run_server_on_runtime(
runtime,
handle,

Check warning on line 156 in samples/demo-server/src/main.rs

View check run for this annotation

Codecov / codecov/patch

samples/demo-server/src/main.rs#L156

Added line #L156 was not covered by tests
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) {

Check warning on line 163 in samples/demo-server/src/main.rs

View check run for this annotation

Codecov / codecov/patch

samples/demo-server/src/main.rs#L163

Added line #L163 was not covered by tests
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,

Check warning on line 169 in samples/demo-server/src/main.rs

View check run for this annotation

Codecov / codecov/patch

samples/demo-server/src/main.rs#L169

Added line #L169 was not covered by tests
"127.0.0.1:8585",
content_path,
server_state,
Expand Down

0 comments on commit 1dc3cc7

Please sign in to comment.