@@ -147,11 +147,11 @@ tasks-add tasks-set-state-given-param-key )) (define *db-write-mutexes* (make-hash-table)) - +(define *server-signature* #f) ;; These are called by the server on recipt of /api calls ;; - keep it simple, only return the actual result of the call, i.e. no meta info here ;; ;; - returns #( flag result ) ;; @@ -231,35 +231,45 @@ (ok-res . #f))) (vector #t res)))))))) ;; indat is (cmd run-id params meta) ;; -;; WARNING: Do not print anything in this function as it reads/writes to current in/out port +;; WARNING: Do not print anything in the lambda of this function as it +;; reads/writes to current in/out port ;; (define (api:tcp-dispatch-request-make-handler dbstruct) ;; cmd run-id params) + (assert *toppath* "FATAL: api:tcp-dispatch-request-make-handler called but *toppath* not set.") + (if (not *server-signature*) + (set! *server-signature* (tt:mk-signature *toppath*))) (lambda () - (let* ((indat (deserialize))) - (set! *api-process-request-count* (+ *api-process-request-count* 1)) + (let* ((indat (deserialize)) + (newcount (+ *api-process-request-count* 1)) + (delay-wait (if (> newcount 10) + (- newcount 10) + 0))) + (set! *api-process-request-count* newcount) + (set! *db-last-access* (current-seconds)) (match indat ((cmd run-id params meta) (let* ((status (cond - ;; turn off busy throttling while trying to get things stable - ;; ((> *api-process-request-count* 50) 'busy) - ;; ((> *api-process-request-count* 25) 'loaded) + ;; ((> newcount 30) 'busy) + ;; ((> newcount 15) 'loaded) (else 'ok))) (errmsg (case status - ((busy) (conc "Server overloaded, "*api-process-request-count*" threads in flight")) - ((loaded) (conc "Server loaded, "*api-process-request-count*" threads in flight")) + ((busy) (conc "Server overloaded, "newcount" threads in flight")) + ((loaded) (conc "Server loaded, "newcount" threads in flight")) (else #f))) (result (case status - ((busy loaded) #f) + ((busy) (- newcount 29)) + ((loaded) #f) (else (case cmd - ((ping) (tt:mk-signature *toppath*)) + ((ping) *server-signature*) (else (api:dispatch-request dbstruct cmd run-id params)))))) - (payload (list status errmsg result '()))) + (meta `((wait . ,delay-wait))) + (payload (list status errmsg result meta))) (set! *api-process-request-count* (- *api-process-request-count* 1)) (serialize payload))) (else (assert #f "FATAL: failed to deserialize indat "indat))))))