Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
socket/communicate: introduce a DEFAULT_READ_TIMEOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
Profpatsch committed Jun 7, 2019
1 parent b3efb28 commit 69c2e5f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
6 changes: 2 additions & 4 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::build_loop::BuildLoop;
use crate::project::Project;
use crate::roots::Roots;
use crate::socket::communicate::{NoMessage, Ping};
use crate::socket::communicate::{NoMessage, Ping, DEFAULT_READ_TIMEOUT};
use crate::socket::{ReadError, ReadWriter, Timeout};
use crate::NixFile;
use std::collections::HashMap;
Expand Down Expand Up @@ -42,8 +42,7 @@ impl<'a> Daemon<'a> {
build_events_tx: tx,
paths,
handler_fns: HandlerFns {
// We just declare 1s as timeout time, which should be more than enough
read_timeout: Timeout::from_millis(1000),
read_timeout: DEFAULT_READ_TIMEOUT,
},
},
rx,
Expand Down Expand Up @@ -93,7 +92,6 @@ impl HandlerFns {
// TODO: make private again
// the ReadWriter here has to be the inverse of the `Client.ping()`, which is `ReadWriter<!, Ping>`
pub fn ping(&self, rw: ReadWriter<Ping, NoMessage>, build_chan: mpsc::Sender<StartBuild>) {
// TODO: read timeout
let ping: Result<Ping, ReadError> = rw.read(&self.read_timeout);
match ping {
Err(ReadError::Timeout) => debug!(
Expand Down
10 changes: 4 additions & 6 deletions src/ops/direnv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use self::version::{DirenvVersion, MIN_DIRENV_VERSION};
use crate::ops::{ok, ok_msg, ExitError, OpResult};
use crate::project::Project;
use crate::socket::communicate::client;
use crate::socket::communicate::Ping;
use crate::socket::Timeout;
use crate::socket::communicate::{Ping, DEFAULT_READ_TIMEOUT};
use std::process::Command;

/// See the documentation for lorri::cli::Command::Direnv for more
Expand All @@ -18,11 +17,10 @@ pub fn main(project: &Project) -> OpResult {
let mut shell_root = project.gc_root_path().unwrap();
shell_root.push("build-0"); // !!!

// TODO: timeout
// TODO: don’t start build/evaluation automatically, let the user decide
if let Ok(client) = client::ping(Timeout::Infinite).connect(&::socket::path::SocketPath::from(
::ops::get_paths()?.daemon_socket_file(),
)) {
if let Ok(client) = client::ping(DEFAULT_READ_TIMEOUT).connect(
&::socket::path::SocketPath::from(::ops::get_paths()?.daemon_socket_file()),
) {
client
.write(&Ping {
nix_file: project.expression().clone(),
Expand Down
6 changes: 2 additions & 4 deletions src/ops/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ use crate::ops::{ok, OpResult};
use crate::NixFile;

use crate::socket::communicate::client;
use crate::socket::communicate::Ping;
use crate::socket::Timeout;
use crate::socket::communicate::{Ping, DEFAULT_READ_TIMEOUT};

/// See the documentation for lorri::cli::Command::Shell for more
/// details.
pub fn main(nix_file: NixFile) -> OpResult {
// TODO: set up socket path, make it settable by the user
// TODO timeout
client::ping(Timeout::Infinite)
client::ping(DEFAULT_READ_TIMEOUT)
// TODO
.connect(&::socket::path::SocketPath::from(
::ops::get_paths()?.daemon_socket_file(),
Expand Down
10 changes: 6 additions & 4 deletions src/socket/communicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use crate::socket::path::{BindError, BindLock, SocketPath};
use crate::socket::{ReadWriteError, ReadWriter, Timeout};
use crate::NixFile;

/// We declare 1s as the time readers should wait
/// for the other side to send something.
pub const DEFAULT_READ_TIMEOUT: Timeout = Timeout::from_millis(1000);

/// Enum of all communication modes the lorri daemon supports.
#[derive(Serialize, Deserialize)]
pub enum CommunicationType {
Expand Down Expand Up @@ -82,9 +86,7 @@ pub mod listener {
Ok(Listener {
listener: l,
bind_lock: lock,
// same timeout as read_timeout in daemon handlers
// TODO: unify when moving Listener into daemon
accept_timeout: Timeout::from_millis(1000),
accept_timeout: DEFAULT_READ_TIMEOUT,
})
}

Expand Down Expand Up @@ -158,7 +160,7 @@ pub mod client {
ServerHandshake(ReadWriteError),
}

// TODO: builder pattern for timeouts?
// builder pattern for timeouts?

impl<R, W> Client<R, W> {
/// “Bake” a Client, aka set its communication type (and message type arguments).
Expand Down
2 changes: 1 addition & 1 deletion src/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub enum Timeout {

impl Timeout {
/// Construct from a millisecond u16.
pub fn from_millis(m: u16) -> Timeout {
pub const fn from_millis(m: u16) -> Timeout {
Timeout::D(Millis(m))
}
}
Expand Down

0 comments on commit 69c2e5f

Please sign in to comment.