[racket] Parameters and servlets

From: antoine (antoine.brand at sfr.fr)
Date: Sat Jan 18 13:11:28 EST 2014

Bonjour,

I want use a parameter to store data with a request lifetime.

I first made a parameter
(define request-data (make-parameter #f))

That i fill in the main entry point for a servlet
(define (start req)
  (fill-request-data! req) 
  (treat req))

;; how i run the servlet for information
(serve
  #:port 8080
  #:dispatch (dispatch/servlet start))

(define (fill-request-data! req)
  (when (something-present-in req)
    (request-data some-data)))

It seems to work well until i noticed that request-data isn't reset to #f for
each request. I thought that each new thread (Is there a new thread created for
each request?) will set request-data to its default.

So i decided to rewrite start as
(define (start req)
  (parameterize ([request-data #f])
    (fill-request-data! req)
    (treat req)))

But that don't works because i used responce/output and the proc arguments is out
parameterize scope, i don't when the response struct call its output fields.

So i have three solution:

rewrite
(define (start req)
  (request-data #f)
  (fill-request-data! req)
  (treat req))

Is there any defect in it? especialy regards to when the output fields is called?

rewrite my responce/output like

(let ([request-data-closurable (request-data)])
  (responce/output (lambda (out) .... i can capture the value of request-data-closurable)))

or rewrite responce/output
(let ([data (do-the-rendering-here-and-now)])
  (responce/output (lambda (out) data)))

After writing this mail i remember threads inherits parameters but i am not sure
why i get a value different from #f.
Why not thread-cell for solution 1?

thank you.

Posted on the users mailing list.