provider: condvar-based timeout (needs dex:post fix)
Some checks failed
Deploy (Gitea) / deploy (push) Failing after 2s

This commit is contained in:
2026-05-04 20:12:12 -04:00
parent 78705f55ec
commit d8929aeb24
2 changed files with 38 additions and 40 deletions

View File

@@ -54,29 +54,28 @@
(handler-case (handler-case
(let* ((result nil) (let* ((result nil)
(lock (bt:make-lock "llm-lock")) (lock (bt:make-lock "llm-lock"))
(done nil) (cvar (bt:make-condition-variable))
(thread (bt:make-thread (thread (bt:make-thread
(lambda () (lambda ()
(handler-case (unwind-protect
(let* ((response (dex:post url :headers headers :content body (handler-case
:connect-timeout (min 10 timeout) (let* ((response (dex:post url :headers headers :content body
:read-timeout (max 10 (- timeout 5)))) :connect-timeout (min 10 timeout)
(json (cl-json:decode-json-from-string response)) :read-timeout (max 10 (- timeout 5))))
(choices (cdr (assoc :choices json))) (json (cl-json:decode-json-from-string response))
(first-choice (car choices)) (choices (cdr (assoc :choices json)))
(message (cdr (assoc :message first-choice))) (first-choice (car choices))
(content (cdr (assoc :content message)))) (message (cdr (assoc :message first-choice)))
(setf result (if content (content (cdr (assoc :content message))))
(list :status :success :content content) (setf result (list :status :success :content content)))
(list :status :error :message (format nil "~a: No content" provider))))) (error (c)
(error (c) (setf result (list :status :error :message (format nil "~a Error: ~a" provider c)))))
(setf result (list :status :error :message (format nil "~a Error: ~a" provider c))))) (bt:with-lock-held (lock)
(bt:with-lock-held (lock) (setf done t))) (bt:condition-notify cvar))))
:name (format nil "llm-~a" provider)))) :name (format nil "llm-~a" provider))))
(loop for waited from 0 below timeout (bt:with-lock-held (lock)
when (bt:with-lock-held (lock) done) (unless result
do (return result) (bt:condition-wait cvar lock :timeout timeout)))
do (sleep 0.5))
(or result (or result
(list :status :error :message (format nil "~a: Request timed out after ~d seconds" provider timeout)))) (list :status :error :message (format nil "~a: Request timed out after ~d seconds" provider timeout))))
(error (c) (error (c)

View File

@@ -82,29 +82,28 @@ Providers register themselves at boot. No API key? That provider doesn't registe
(handler-case (handler-case
(let* ((result nil) (let* ((result nil)
(lock (bt:make-lock "llm-lock")) (lock (bt:make-lock "llm-lock"))
(done nil) (cvar (bt:make-condition-variable))
(thread (bt:make-thread (thread (bt:make-thread
(lambda () (lambda ()
(handler-case (unwind-protect
(let* ((response (dex:post url :headers headers :content body (handler-case
:connect-timeout (min 10 timeout) (let* ((response (dex:post url :headers headers :content body
:read-timeout (max 10 (- timeout 5)))) :connect-timeout (min 10 timeout)
(json (cl-json:decode-json-from-string response)) :read-timeout (max 10 (- timeout 5))))
(choices (cdr (assoc :choices json))) (json (cl-json:decode-json-from-string response))
(first-choice (car choices)) (choices (cdr (assoc :choices json)))
(message (cdr (assoc :message first-choice))) (first-choice (car choices))
(content (cdr (assoc :content message)))) (message (cdr (assoc :message first-choice)))
(setf result (if content (content (cdr (assoc :content message))))
(list :status :success :content content) (setf result (list :status :success :content content)))
(list :status :error :message (format nil "~a: No content" provider))))) (error (c)
(error (c) (setf result (list :status :error :message (format nil "~a Error: ~a" provider c)))))
(setf result (list :status :error :message (format nil "~a Error: ~a" provider c))))) (bt:with-lock-held (lock)
(bt:with-lock-held (lock) (setf done t))) (bt:condition-notify cvar))))
:name (format nil "llm-~a" provider)))) :name (format nil "llm-~a" provider))))
(loop for waited from 0 below timeout (bt:with-lock-held (lock)
when (bt:with-lock-held (lock) done) (unless result
do (return result) (bt:condition-wait cvar lock :timeout timeout)))
do (sleep 0.5))
(or result (or result
(list :status :error :message (format nil "~a: Request timed out after ~d seconds" provider timeout)))) (list :status :error :message (format nil "~a: Request timed out after ~d seconds" provider timeout))))
(error (c) (error (c)