utils.lisp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. (in-package #:chatikbot)
  2. (defvar *backoff-start* 1 "Initial back-off")
  3. (defvar *backoff-max* 64 "Maximum back-off delay")
  4. (defun loop-with-error-backoff (func)
  5. (let ((backoff *backoff-start*))
  6. (loop
  7. do
  8. (handler-case
  9. (progn
  10. (funcall func)
  11. (setf backoff *backoff-start*))
  12. (condition (e)
  13. (log:error e)
  14. (log:info "Backing off for" backoff)
  15. (sleep backoff)
  16. (setf backoff (min *backoff-max*
  17. (* 2 backoff))))))))
  18. (defun replace-all (string part replacement &key (test #'char=))
  19. "Returns a new string in which all the occurences of the part
  20. is replaced with replacement."
  21. (with-output-to-string (out)
  22. (loop with part-length = (length part)
  23. for old-pos = 0 then (+ pos part-length)
  24. for pos = (search part string
  25. :start2 old-pos
  26. :test test)
  27. do (write-string string out
  28. :start old-pos
  29. :end (or pos (length string)))
  30. when pos do (write-string replacement out)
  31. while pos)))
  32. (defmacro aget (key alist)
  33. `(cdr (assoc ,key ,alist :test #'equal)))