A stateless servlet should provide the following exports:
value
interface-version :(one-of/c 'stateless)
value
If it is not provided, it defaults to default-stuffer .
If it is not provided, it defaults to (create-none-manager #f).
`(html(body(h2"Look ma, no state!")))))
The web-server/base language exports all of the functions and syntax from racket/base and nothing else.
The web-server language exports all of the functions and syntax from the following libraries: racket, net/url, web-server/http, web-server/http/bindings, web-server/lang/abort-resume, web-server/lang/web, web-server/lang/native, web-server/lang/web-param, web-server/lang/web-cells, web-server/lang/file-box, web-server/lang/soft, web-server/dispatch, and web-server/stuffers. Some of these are documented in the subsections that follow.
All uses of letrec are removed and replaced with equivalent uses of let and imperative features.
The program is converted into ANF (A-Normal Form), making all continuations explicit.
All continuations and continuations marks are recorded in the continuation marks of the expression they are the continuation of.
All calls to external modules are identified and marked.
All uses of call/cc are removed and replaced with equivalent gathering of the continuations through the continuation marks installed earlier.
The program is defunctionalized with a serializable data-structure for each lambda .
This process allows the continuations captured by your servlet to be serialized. This means they may be stored on the client’s browser or the server’s disk.
This means your servlet has no cost to the server other than execution. This is very attractive if you’ve used Racket servlets and had memory problems.
This means your server can restart in the middle of a long running Web interaction without the URLs that have been shared with the client expiring. This is very attractive if you’ve used Racket servlets and had session timeout problems.
This process is defined on all of Racket and occurs after macro-expansion, so you are free to use all interesting features of Racket. However, there are some considerations you must make.
First, this process drastically changes the structure of your program. It will create an immense number of lambdas and structures your program did not normally contain. The performance implication of this has not been studied with Racket.
Second, the defunctionalization process is sensitive to the syntactic structure of your program. Therefore, if you change your program in a trivial way, for example, changing a constant, then all serialized continuations will be obsolete and will error when deserialization is attempted. This is a feature, not an error! It is a small price to pay for protection from the sorts of errors that would occur if your program were changed in a meaningful way. If you use the default-stuffer or web-server/stuffers/hash, then whenever you change your servlet’s code, you can safely delete all saved continuations, because they won’t be used any longer.
Third, the values in the lexical scope of your continuations must be serializable for the continuations itself to be serializable. This means that you must use define-serializable-struct rather than define-struct , and take care to use modules that do the same. Similarly, you may not use parameterize , because parameterizations are not serializable.
Fourth, and related, this process only runs on your code, not on the code you require . Thus, your continuations—to be serializable—must not be in the context of another module. For example, the following will fail with an "unsafe context" exception:
response-generators))
response-generators)))
Fifth, the store is not serialized. If you rely on the store you will be taking huge risks. You will be assuming that the serialized continuation is invoked on the same server before the server is restarted or the memory is garbage collected.
This process is derived from the papers Continuations from Generalized Stack Inspection by Pettyjohn et al. in 2005, Automatically RESTful Web Applications, Or Marking Modular Serializable Continuations by Jay McCarthy in 2009, and The Two-State Solution: Native and Serializable Continuations Accord by Jay McCarthy in 2010, We thank Greg Pettyjohn for his initial implementation of this algorithm.
procedure
( call-with-serializable-current-continuation response-generator)
→any
syntax
( serial->native expr)
syntax
( native->serial expr)
3
3
syntax
( define-native (nativearg-spec...)original)
arg-spec : hoarg-spec : _
(define-native (build-list/native_ ho)build-list )
fst
procedure
( send/suspend/url response-generator)→request?
procedure
( send/suspend response-generator)→request?
procedure
( send/suspend/hidden response-generator)→request?
procedure
( send/suspend/url/dispatch make-response)→any
procedure
( send/suspend/dispatch make-response)→any
procedure
( redirect/get [#:headershs])→request?
Changed in version 1.3 of package web-server-lib: Added hs argument and changed to use see-other instead of temporarily .
syntax
( make-web-cell default-expr)
procedure
( web-cell-ref wc)→any/c
wc:web-cell?
procedure
( file-unbox fb)→serializable?
fb:file-box?
procedure
( file-box-set? fb)→boolean?
fb:file-box?
procedure
( file-box-set! fbv)→void
fb:file-box?
syntax
( make-web-parameter default)
procedure
( web-parameter? v)→boolean?
v:any/c
syntax
( web-parameterize ([web-parameter-exprvalue-expr]...)expr...)
procedure
( soft-state? v)→boolean?
v:any/c
procedure
( make-soft-state thnk)→soft-state?
procedure
( soft-state-ref ss)→any/c
ss:soft-state?
syntax
( soft-state expr...)
(start`(html(body(a([href,k-url])"Done"))))))))
$ plt-web-server -p 8080
Doing a long computation...
Done
Done
Done
Done
^Cuser break
$ plt-web-server -p 8080
Doing a long computation...
Done
The web-server language provides serializable continuations. The serialization functionality is abstracted into stuffers that control how it operates. You can supply your own (built with these functions) when you write a stateless servlet.
value
procedure
( stuffer-compose gf)→(stuffer/c any/c any/c )
procedure
( stuffer-sequence fg)→(stuffer/c any/c any/c )
procedure
( stuffer-if cf)→(stuffer/c bytes? bytes? )
value
value
value
The web-server/stuffers/hash stuffers rely on a key/value store.
value
procedure
( hash-stuffer Hstore)→(stuffer/c bytes? bytes? )
store:store?
procedure
( md5-stuffer root)→(stuffer/c bytes? bytes? )
root:path-string?
procedure
( HMAC-SHA1-stuffer kb)→(stuffer/c bytes? bytes? )
kb:bytes?
procedure
( is-url-too-big? v)→boolean?
v:bytes?
procedure
( make-default-stuffer root)→(stuffer/c serializable? bytes? )
root:path-string?
value
".urls"))