Skip to content

Commit 37613ab

Browse files
committed
Cache notebook server response in one instance
When accessing directories using TRAMP the function calls end up hitting `jupyter-api-server-accessible-p` indirectly from the calls `jupyter-tramp-with-api-connection` -> `jupyter-tramp-server-from-file-name`. Since `jupyter-tramp-with-api-connection` is used throughout all of the TRAMP file operations it can be called a lot which means a server request just to check if the server is accessible before making the request for the TRAMP operation since `jupyter-api-server-accessible-p` did not cache its result. This change, caches the result so that TRAMP can perform all of its file operations while only having to do an initial check of the server availability. * jupyter-rest-api (jupyter-api--response-cache) (jupyter-api--response-cahce-expiry): New variables. (jupyter-api-server-accessible-p): Cache response value.
1 parent 18f4cde commit 37613ab

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

jupyter-rest-api.el

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,30 @@ error with the data being the error received by `url-retrieve'."
505505

506506
;;;; Authenticators
507507

508+
;; TODO Other instances where caching results of requests could be
509+
;; beneficial? Currently it is only used here.
510+
(defvar jupyter-api--response-cache (make-hash-table))
511+
512+
(defvar jupyter-api--response-cache-expiry 1
513+
"Number of seconds of Emacs idle time before cache invalidation.")
514+
508515
(cl-defmethod jupyter-api-server-accessible-p ((client jupyter-rest-client))
509516
"Return non-nil if CLIENT can access the Jupyter notebook server."
510-
(ignore-errors
511-
(prog1 t
512-
(let ((jupyter-api-authentication-in-progress-p t)
513-
jupyter-api-request-data
514-
jupyter-api-request-headers)
515-
(jupyter-api-get-kernelspec client)))))
517+
(pcase (gethash client jupyter-api--response-cache 'check)
518+
('check
519+
(puthash client
520+
(ignore-errors
521+
(prog1 t
522+
(let ((jupyter-api-authentication-in-progress-p t)
523+
jupyter-api-request-data
524+
jupyter-api-request-headers)
525+
;; Hit an endpoint that requires authentication.
526+
(jupyter-api-get-kernelspec client))))
527+
jupyter-api--response-cache)
528+
(run-with-idle-timer
529+
jupyter-api--response-cache-expiry
530+
nil (lambda () (remhash client jupyter-api--response-cache))))
531+
(cached cached)))
516532

517533
(cl-defgeneric jupyter-api-authenticate (client &rest args)
518534
(declare (indent 1)))

0 commit comments

Comments
 (0)