feat: add SSE connection state and reconnection logic

This commit is contained in:
2026-06-11 12:43:12 -04:00
parent 869f4cb67b
commit b090819483
2 changed files with 50 additions and 0 deletions
+24
View File
@@ -11,6 +11,30 @@
(require 'json) (require 'json)
(require 'cl-lib) (require 'cl-lib)
(defvar noise-sse--process nil
"Active SSE network process, or nil.")
(defvar noise-sse--reconnect-timer nil
"Timer for reconnection attempts, or nil.")
(defvar noise-sse--reconnect-delay 1
"Current delay in seconds before next reconnection attempt.")
(defcustom noise-sse-max-reconnect-delay 60
"Maximum delay in seconds between reconnection attempts."
:type 'integer
:group 'noise)
(defun noise-sse--increase-reconnect-delay ()
"Increase reconnect delay with exponential backoff, capped at max."
(setq noise-sse--reconnect-delay
(min (* noise-sse--reconnect-delay 2)
noise-sse-max-reconnect-delay)))
(defun noise-sse--reset-reconnect-delay ()
"Reset reconnect delay to initial value after successful connection."
(setq noise-sse--reconnect-delay 1))
(defun noise-sse--parse-event (event-string) (defun noise-sse--parse-event (event-string)
"Parse an SSE EVENT-STRING into a hash table. "Parse an SSE EVENT-STRING into a hash table.
Returns nil for empty or invalid events." Returns nil for empty or invalid events."
+26
View File
@@ -29,5 +29,31 @@
(let ((event "\n\n")) (let ((event "\n\n"))
(should (null (noise-sse--parse-event event))))) (should (null (noise-sse--parse-event event)))))
(ert-deftest noise-sse--initial-state ()
"SSE client starts in disconnected state."
(let ((noise-sse--process nil)
(noise-sse--reconnect-timer nil)
(noise-sse--reconnect-delay 1))
(should (null noise-sse--process))
(should (null noise-sse--reconnect-timer))
(should (= 1 noise-sse--reconnect-delay))))
(ert-deftest noise-sse--reconnect-delay-backoff ()
"Reconnect delay doubles on each attempt."
(let ((noise-sse--reconnect-delay 1))
(noise-sse--increase-reconnect-delay)
(should (= 2 noise-sse--reconnect-delay))
(noise-sse--increase-reconnect-delay)
(should (= 4 noise-sse--reconnect-delay))
(noise-sse--increase-reconnect-delay)
(should (= 8 noise-sse--reconnect-delay))))
(ert-deftest noise-sse--reconnect-delay-max ()
"Reconnect delay caps at max value."
(let ((noise-sse--reconnect-delay 32)
(noise-sse-max-reconnect-delay 60))
(noise-sse--increase-reconnect-delay)
(should (= 60 noise-sse--reconnect-delay))))
(provide 'noise-sse-test) (provide 'noise-sse-test)
;;; noise-sse-test.el ends here ;;; noise-sse-test.el ends here