Skip to content

Commit 295d424

Browse files
committed
Instead of pinging the API, actually make a short request and use it
GET /rate_limit gives us information on how many requests we have left. If we're getting low, warn the user. If there's nothing left (or the command times out), ask to go offline.
1 parent 246746d commit 295d424

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

magithub-core.el

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,30 @@ CAR is a time value; CDR is the cached value.")
105105
"Non-nil if the API is available.
106106
107107
Pings the API a maximum of once every ten seconds."
108+
(magithub-debug-message "checking if the API is available")
108109
(unless (and (not ignore-offline-mode) (magithub-offline-p))
109110
(if (and (consp magithub--api-available-p)
110111
(< (time-to-seconds (time-subtract (current-time) (car magithub--api-available-p))) 10))
111112
(prog1 (cdr magithub--api-available-p)
112-
(when magithub-debug-mode
113-
(message "used cached value for api-available-p")))
114-
(cdr
115-
(setq magithub--api-available-p
116-
(cons (current-time)
117-
(with-timeout (1 (ignore (when (y-or-n-p "API is not responding quickly; go offline? ")
118-
(magithub-go-offline))))
119-
(when magithub-debug-mode
120-
(message "pinging GitHub for api-available-p"))
121-
(let ((magit-git-executable "ping")
122-
(magit-pre-call-git-hook nil)
123-
(magit-git-global-arguments nil))
124-
(= 0 (magit-git-exit-code "-c 1" "-n" "api.github.com"))))))))))
113+
(magithub-debug-message "used cached value for api-available-p"))
114+
(magithub-debug-message "retrieving new value for api-available-p")
115+
(let* ((response (with-timeout (magithub-api-timeout :timeout) (ghub-get "/rate_limit")))
116+
(remaining (let-alist response .rate.remaining))
117+
status go-offline-message)
118+
(magithub-debug-message "new value retrieved for api-available-p: %S" response)
119+
(cond
120+
((and (numberp remaining) (< 250 remaining)) (setq status t))
121+
((= 0 remaining) (setq go-offline-message "You're bring rate-limited (no more requests left)"))
122+
((numberp remaining) (setq go-offline-message (format "Only %d requests left" remaining)
123+
status t))
124+
((eq response :timeout) (setq go-offline-message "API is not responding quickly"))
125+
(t (setq go-offline-message "Unknown issue with API access")))
126+
127+
(setq magithub--api-available-p (cons (current-time) status))
128+
(when (and go-offline-message
129+
(y-or-n-p (format "%s; go offline? " go-offline-message)))
130+
(magithub-go-offline))
131+
status))))
125132

126133
(defun magithub--completing-read (prompt collection &optional format-function predicate require-match default)
127134
"Using PROMPT, get a list of elements in COLLECTION.

0 commit comments

Comments
 (0)