Megatest

Diff
Login

Differences From Artifact [13a08c65d1]:

To Artifact [800ec32af5]:


236
237
238
239
240
241
242
243












244

245
246













247
248



























249
250
251
252
253
254
255
  (set! *api-threads* (filter (lambda (thdat)
				(not (member (thread-state (car thdat)) '(terminated dead))))
			      *api-threads*)))

(define (api:get-count-threads-alive)
  (length *api-threads*))
  













;; indat is (cmd run-id params meta)

;;
;; 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 (indat)
    (api:register-thread (current-thread))
    (let* (;; (indat      (deserialize))







|
>
>
>
>
>
>
>
>
>
>
>
>
|
>

|
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







236
237
238
239
240
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  (set! *api-threads* (filter (lambda (thdat)
				(not (member (thread-state (car thdat)) '(terminated dead))))
			      *api-threads*)))

(define (api:get-count-threads-alive)
  (length *api-threads*))
  
;; ==========================================================================================================================================
;; api:tcp-dispatch-request-make-handler is a complex TCP request handler that manages server load, dispatches requests, and ensures that the 
;; server's state is consistent with the incoming requests. It is designed to be used in a concurrent environment where multiple requests are 
;; being handled simultaneously.
;;
;; It processes incoming requests and dispatches them accordingly. 
;; The function takes a dbstruct argument, which is a structure representing the database.
;;
;; 1. The function asserts that global variable `*toppath*` is set. 
;;
;; 2. It checks if `*server-signature*` is not set, and if so, it sets it using the `tt:mk-signature` function with `*toppath*` as an argument. 
;;    The `*server-signature*` is used to identify the server instance.
;;
;; 3. The function returns a lambda function that takes `indat` as an argument. indat is (cmd run-id params meta) This lambda is the actual 
;;    request handler that will be called with the incoming data.
;;
;; 4. Inside the lambda, the current thread is registered with `api:register-thread`.
;;
;; 5. Several local variables are initialized:
;;    - `newcount`: A counter for the number of requests being processed.
;;    - `numthreads`: The number of alive threads handling requests.
;;    - `delay-wait`: A calculated delay based on the number of requests.
;;
;; 6. A `normal-proc` lambda is defined to handle the incoming command (`cmd`), `run-id`, and `params`. It uses a `case` statement to handle 
;;    different commands. If the command is "ping", it returns the server signature. Otherwise, it dispatches the request using 
;;    `api:dispatch-request`.
;;
;; 7. The function updates the `*api-process-request-count*` and `*db-last-access*` global variables.
;;
;; 8. It checks if the number of requests (`newcount`) does not match the number of threads (`numthreads`) and performs cleanup and debugging 
;;    if necessary.
;;
;; 9. The `match` expression is used to destructure `indat` into its components (`cmd`, `run-id`, `params`, `meta`).
;;
;; 10. Several local variables are set based on the destructured data and the current server state:
;;     - `db-ok`: Checks if the database file name matches the expected one for the given `run-id`.
;;     - `ttdat`: Retrieves server information.
;;     - `server-state`: Gets the current state of the server.
;;     - `status`: Determines the server's load status based on `newcount`.
;;     - `errmsg`: Generates an error message based on the server's status.
;;     - `result`: Processes the command based on the server's status.
;;
;; 11. The `meta` variable is updated with additional information based on the command.
;;
;; 12. The `payload` is constructed, which includes the status, error message, result, and meta information.
;;
;; 13. The `*api-process-request-count*` is decremented, as the request has been processed.
;;
;; 14. The current thread is unregistered with `api:unregister-thread`.
;;
;; 15. Finally, the `payload` is returned, which would be the response to the incoming request.
;;
;; Nothing should be printed within the lambda because it interacts with the current input/output ports, which could interfere with the 
;; request/response flow.
;;
;; The `else` clause at the end of the `match` expression asserts a fatal error if `indat` cannot be deserialized, indicating that the incoming 
;; data is not in the expected format.
;;

(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 (indat)
    (api:register-thread (current-thread))
    (let* (;; (indat      (deserialize))