From e02bed9a5030947e6016a20d822368f59e6d35c8 Mon Sep 17 00:00:00 2001 From: Nathaniel Nicandro Date: Tue, 16 Oct 2018 16:45:56 -0500 Subject: [PATCH] Add `jupyter-hb-consider-dead-periods` --- jupyter-channels.el | 23 +++++++++++++++++++---- jupyter-tests.el | 16 +++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/jupyter-channels.el b/jupyter-channels.el index 4bd7d33..ff7c297 100644 --- a/jupyter-channels.el +++ b/jupyter-channels.el @@ -32,6 +32,19 @@ "Jupyter channels" :group 'jupyter) +(defvar jupyter-hb-consider-dead-periods 5 + "Number of `time-to-dead' periods until the `kernel-died-cb' is called. +A `jupyter-hb-channel' sends a ping to the kernel on the +heartbeat channel and waits until `time-to-dead' seconds to see +if the kernel sent a ping back. If the kernel does not send a +ping back for + + (* `time-to-dead'`jupyter-hb-consider-dead-periods') + +seconds, consider the kernel dead and call the callback in the +`kernel-died-cb' slot of a `jupyter-hb-channel'. See +`jupyter-hb-on-kernel-dead'.") + ;;; Basic channel types (defclass jupyter-channel () @@ -214,7 +227,7 @@ call `jupyter-get-message'.") :initform :hb :documentation "The type of this channel is `:hb'.") (time-to-dead - :type integer + :type number :initform 1 :documentation "The time in seconds to wait for a response from the kernel until the connection is assumed to be dead. Note @@ -271,8 +284,9 @@ Stop the timer of the heartbeat channel." (cl-defmethod jupyter-hb-on-kernel-dead ((channel jupyter-hb-channel) fun) "When the kernel connected to CHANNEL dies call FUN. -A kernel is considered dead when the HB-CHANNEL does not receive -a response after 5 `time-to-dead' periods." +A kernel is considered dead when CHANNEL does not receive a +response after `jupyter-hb-consider-dead-periods' of +`time-to-dead' seconds." (declare (indent 1)) (oset channel kernel-died-cb fun)) @@ -293,7 +307,8 @@ a response after 5 `time-to-dead' periods." (oset channel socket (jupyter-connect-channel :hb (oref channel endpoint) identity))) - (when (and (integerp counter) (>= counter 5)) + (when (and (integerp counter) + (>= counter jupyter-hb-consider-dead-periods)) (oset channel paused t) (when (functionp (oref channel kernel-died-cb)) (funcall (oref channel kernel-died-cb))))) diff --git a/jupyter-tests.el b/jupyter-tests.el index 9bd478f..12651cf 100644 --- a/jupyter-tests.el +++ b/jupyter-tests.el @@ -360,23 +360,21 @@ running BODY." (kill-process ioloop))))) (ert-info ("Heartbeat channel") (let ((channel (jupyter-hb-channel :endpoint "tcp://127.0.0.1:5556")) - (died-cb-called nil)) + (died-cb-called nil) + (jupyter-hb-consider-dead-periods 1)) (oset channel time-to-dead 0.1) (should-not (jupyter-channel-alive-p channel)) (should-not (jupyter-hb-beating-p channel)) (should (oref channel paused)) (oset channel beating t) (jupyter-start-channel channel) - (jupyter-hb-on-kernel-dead channel - (lambda () (setq died-cb-called t))) + (jupyter-hb-on-kernel-dead channel (lambda () (setq died-cb-called t))) (should (jupyter-channel-alive-p channel)) (should-not (oref channel paused)) - ;; For some reason just calling `sleep-for' - ;; directly causes the timers to run after the - ;; `sleep-for' returns? - (with-timeout (1 (should (oref channel paused))) - (while (not (oref channel paused)) - (sleep-for 0.1))) + (sleep-for 0.2) + ;; It seems the timers are run after returning from the first `sleep-for' + ;; call. + (sleep-for 0.1) (should (oref channel paused)) (should-not (oref channel beating)) (should died-cb-called)