mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Server sent events (#1920)
## Issue Addressed Resolves #1434 (this is the last major feature in the standard spec. There are only a couple of places we may be off-spec due to recent spec changes or ongoing discussion) Partly addresses #1669 ## Proposed Changes - remove the websocket server - remove the `TeeEventHandler` and `NullEventHandler` - add server sent events according to the eth2 API spec ## Additional Info This is according to the currently unmerged PR here: https://github.com/ethereum/eth2.0-APIs/pull/117 Co-authored-by: realbigsean <seananderson33@gmail.com>
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
[package]
|
||||
name = "websocket_server"
|
||||
version = "0.2.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3.7"
|
||||
serde = "1.0.116"
|
||||
serde_derive = "1.0.116"
|
||||
slog = "2.5.2"
|
||||
tokio = { version = "0.3.2", features = ["full"] }
|
||||
types = { path = "../../consensus/types" }
|
||||
ws = "0.9.1"
|
||||
task_executor = { path = "../../common/task_executor" }
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
/// The core configuration of a Lighthouse beacon node.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub enabled: bool,
|
||||
/// The IPv4 address the REST API HTTP server will listen on.
|
||||
pub listen_address: Ipv4Addr,
|
||||
/// The port the REST API HTTP server will listen on.
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
enabled: false,
|
||||
listen_address: Ipv4Addr::new(127, 0, 0, 1),
|
||||
port: 5053,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
use slog::{debug, error, info, warn};
|
||||
use std::marker::PhantomData;
|
||||
use std::net::SocketAddr;
|
||||
use types::EthSpec;
|
||||
use ws::{Sender, WebSocket};
|
||||
|
||||
mod config;
|
||||
|
||||
pub use config::Config;
|
||||
|
||||
pub struct WebSocketSender<T: EthSpec> {
|
||||
sender: Option<Sender>,
|
||||
_phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: EthSpec> WebSocketSender<T> {
|
||||
/// Creates a dummy websocket server that never starts and where all future calls are no-ops.
|
||||
pub fn dummy() -> Self {
|
||||
Self {
|
||||
sender: None,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_string(&self, string: String) -> Result<(), String> {
|
||||
if let Some(sender) = &self.sender {
|
||||
sender
|
||||
.send(string)
|
||||
.map_err(|e| format!("Unable to broadcast to websocket clients: {:?}", e))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_server<T: EthSpec>(
|
||||
executor: task_executor::TaskExecutor,
|
||||
config: &Config,
|
||||
) -> Result<(WebSocketSender<T>, SocketAddr), String> {
|
||||
let log = executor.log();
|
||||
let server_string = format!("{}:{}", config.listen_address, config.port);
|
||||
|
||||
// Create a server that simply ignores any incoming messages.
|
||||
let server = WebSocket::new(|_| |_| Ok(()))
|
||||
.map_err(|e| format!("Failed to initialize websocket server: {:?}", e))?
|
||||
.bind(server_string.clone())
|
||||
.map_err(|e| {
|
||||
format!(
|
||||
"Failed to bind websocket server to {}: {:?}",
|
||||
server_string, e
|
||||
)
|
||||
})?;
|
||||
|
||||
let actual_listen_addr = server.local_addr().map_err(|e| {
|
||||
format!(
|
||||
"Failed to read listening addr from websocket server: {:?}",
|
||||
e
|
||||
)
|
||||
})?;
|
||||
|
||||
let broadcaster = server.broadcaster();
|
||||
|
||||
// Produce a signal/channel that can gracefully shutdown the websocket server.
|
||||
let exit = executor.exit();
|
||||
let log_inner = log.clone();
|
||||
let broadcaster_inner = server.broadcaster();
|
||||
let exit_future = async move {
|
||||
let _ = exit.await;
|
||||
if let Err(e) = broadcaster_inner.shutdown() {
|
||||
warn!(
|
||||
log_inner,
|
||||
"Websocket server errored on shutdown";
|
||||
"error" => format!("{:?}", e)
|
||||
);
|
||||
} else {
|
||||
info!(log_inner, "Websocket server shutdown");
|
||||
}
|
||||
};
|
||||
|
||||
// Place a future on the handle that will shutdown the websocket server when the
|
||||
// application exits.
|
||||
|
||||
executor.spawn(exit_future, "Websocket exit");
|
||||
|
||||
let log_inner = log.clone();
|
||||
let server_future = move || match server.run() {
|
||||
Ok(_) => {
|
||||
debug!(
|
||||
log_inner,
|
||||
"Websocket server thread stopped";
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
error!(
|
||||
log_inner,
|
||||
"Websocket server failed to start";
|
||||
"error" => format!("{:?}", e)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
executor.spawn_blocking(server_future, "Websocket server");
|
||||
|
||||
info!(
|
||||
log,
|
||||
"WebSocket server started";
|
||||
"address" => format!("{}", actual_listen_addr.ip()),
|
||||
"port" => actual_listen_addr.port(),
|
||||
);
|
||||
|
||||
Ok((
|
||||
WebSocketSender {
|
||||
sender: Some(broadcaster),
|
||||
_phantom: PhantomData,
|
||||
},
|
||||
actual_listen_addr,
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user