241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
-
+
+
+
+
+
+
-
-
+
+
-
-
+
+
-
-
+
-
-
-
+
+
|
(if (not *server-signature*)
(set! *server-signature* (tt:mk-signature *toppath*)))
(lambda ()
(let* ((indat (deserialize))
(newcount (+ *api-process-request-count* 1))
(delay-wait (if (> newcount 10)
(- newcount 10)
0)))
0))
(normal-proc (lambda (cmd run-id params)
(case cmd
((ping) *server-signature*)
(else
(api:dispatch-request dbstruct cmd run-id params))))))
(set! *api-process-request-count* newcount)
(set! *db-last-access* (current-seconds))
(match indat
((cmd run-id params meta)
(let* ((ttdat *server-info*)
(server-state (tt-state ttdat))
(status (cond
((> newcount 30) 'busy)
((> newcount 15) 'loaded)
;; ((> newcount 600) 'busy)
;; ((> newcount 300) 'loaded)
(else 'ok)))
(errmsg (case status
((busy) (conc "Server overloaded, "newcount" threads in flight"))
((loaded) (conc "Server loaded, "newcount" threads in flight"))
(else #f)))
(result (case status
((busy) (- newcount 29))
((loaded) #f)
((busy) (- newcount 29)) ;; call back in as many seconds
((loaded)
(else
(case cmd
(normal-proc cmd run-id params))
((ping) *server-signature*)
(else
(api:dispatch-request dbstruct cmd run-id params))))))
(else
(normal-proc cmd run-id params))))
(meta (case cmd
((ping) `((sstate . ,server-state)))
(else `((wait . ,delay-wait)))))
(payload (list status errmsg result meta)))
(set! *api-process-request-count* (- *api-process-request-count* 1))
(serialize payload)))
(else
|