Skip to content

Commit

Permalink
Timestamp fixes (#3823)
Browse files Browse the repository at this point in the history
* Prefer float-time to doing it by hand

Prefer (float-time) to (time-to-seconds (current-time)),
as this uses less time and memory.

* New function lsp--ms-since

This refactors, preparing for a bug fix.

* Fix bug in time delay calculation

Return number of millseconds, not number of milliseconds modulo 1000.
  • Loading branch information
eggert authored Nov 24, 2022
1 parent 25be0a5 commit e5a6274
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lsp-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -3206,7 +3206,7 @@ workspace->result.
If NO-WAIT is non-nil send the request as notification."
(if no-wait
(lsp-notify method params)
(let* ((send-time (time-to-seconds (current-time)))
(let* ((send-time (float-time))
;; max time by which we must get a response
(expected-time
(and
Expand All @@ -3228,7 +3228,7 @@ If NO-WAIT is non-nil send the request as notification."
(accept-process-output
nil
(if expected-time (- expected-time send-time) 1))))
(setq send-time (time-to-seconds (current-time)))
(setq send-time (float-time))
(when (and expected-time (< expected-time send-time))
(error "Timeout while waiting for response. Method: %s" method)))
(setq done? t)
Expand All @@ -3245,7 +3245,7 @@ If NO-WAIT is non-nil send the request as notification."
"Send request METHOD with PARAMS and waits until there is no input.
Return same value as `lsp--while-no-input' and respecting `non-essential'."
(if non-essential
(let* ((send-time (time-to-seconds (current-time)))
(let* ((send-time (float-time))
;; max time by which we must get a response
(expected-time
(and
Expand All @@ -3263,7 +3263,7 @@ Return same value as `lsp--while-no-input' and respecting `non-essential'."
(catch 'lsp-done
(sit-for
(if expected-time (- expected-time send-time) 1)))
(setq send-time (time-to-seconds (current-time)))
(setq send-time (float-time))
(when (and expected-time (< expected-time send-time))
(error "Timeout while waiting for response. Method: %s" method)))
(setq done? (or resp-error resp-result))
Expand Down Expand Up @@ -6469,6 +6469,10 @@ PARAMS are the `workspace/configuration' request params"
section))))))
(apply #'vector)))

(defun lsp--ms-since (timestamp)
"Integer number of milliseconds since TIMESTAMP. Fractions discarded."
(floor (* 1000 (float-time (time-since timestamp)))))

(defun lsp--send-request-response (workspace recv-time request response)
"Send the RESPONSE for REQUEST in WORKSPACE and log if needed."
(-let* (((&JSONResponse :params :method :id) request)
Expand All @@ -6478,7 +6482,7 @@ PARAMS are the `workspace/configuration' request params"
(lsp--make-log-entry method id params 'incoming-req)))
(resp-entry (and lsp-log-io
(lsp--make-log-entry method id response 'outgoing-resp
(/ (nth 2 (time-since recv-time)) 1000)))))
(lsp--ms-since recv-time)))))
;; Send response to the server.
(when (lsp--log-io-p method)
(lsp--log-entry-new req-entry workspace)
Expand Down Expand Up @@ -6657,7 +6661,7 @@ server. WORKSPACE is the active workspace."
(when (lsp--log-io-p method)
(lsp--log-entry-new
(lsp--make-log-entry method id data 'incoming-resp
(/ (nth 2 (time-since before-send)) 1000))
(lsp--ms-since before-send))
workspace))
(when callback
(remhash id (lsp--client-response-handlers client))
Expand All @@ -6668,7 +6672,7 @@ server. WORKSPACE is the active workspace."
(when (lsp--log-io-p method)
(lsp--log-entry-new
(lsp--make-log-entry method id (lsp:json-response-error-error json-data)
'incoming-resp (/ (nth 2 (time-since before-send)) 1000))
'incoming-resp (lsp--ms-since before-send))
workspace))
(when callback
(remhash id (lsp--client-response-handlers client))
Expand Down

0 comments on commit e5a6274

Please sign in to comment.