forked from per-gron/sack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sack-app.scm
253 lines (198 loc) · 5.94 KB
/
sack-app.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
(import server
show-exceptions
pool-session-store
cookie
(std misc/exception))
(sack-start!
(lambda (env)
(let ((ret #t))
(values 200
'(("Content-Type" . "text/plain"))
(lambda ()
(and
ret
(begin
(set! ret #f)
(with-output-to-u8vector
(list char-encoding: 'UTF-8)
(lambda ()
(display "Hello, world!")))))))))
port-number: 3333)
(define (sack-app env)
(error "hej"))
(define app
(let ((pool (make-session-pool)))
(lambda (env)
((show-exceptions
(cookies->
(pool-session-store
sack-app
pool: pool)))
env))))
(define quit
(sack-start! (lambda (env) (app env))
port-number: 3333))
(load "../../github-modules/build")
(import server
show-exceptions
pool-session-store
cookie
(std misc/exception))
(import "http://github.com/pereckerdal/sack/raw/master/src/server.scm"
"http://github.com/pereckerdal/sack/raw/master/src/show-exceptions.scm"
"http://github.com/pereckerdal/sack/raw/master/src/pool-session-store.scm"
"http://github.com/pereckerdal/sack/raw/master/src/cookie.scm"
(std misc/exception))
(define (sack-app env)
(pp (list sess: ((env 'sack:session:get-all))))
((env 'sack:session:set!) "hej" "du")
(let ((ret #t))
(values 200
'(("Content-Type" . "text/plain; charset=UTF-8"))
(lambda ()
(and
ret
(begin
(set! ret #f)
(with-output-to-u8vector
(list char-encoding: 'UTF-8)
(lambda ()
(display "hej")))))))))
(define app
(let ((pool (make-session-pool)))
(lambda (env)
((show-exceptions
(cookies->
(pool-session-store
sack-app
pool: pool)))
env))))
(thread-start!
(make-thread
(lambda ()
(sack-start!
(lambda (env)
(app env))
port-number: 3333))))
(lambda (env)
(values 200
'(("Content-Type" . "text/plain; charset=UTF-8"))
(lambda ()
(display "hej")
#f)))
the anatomy of a spork request:
* the spork sack app gets called.
* we find out where to enter (a previous continuation id or a fresh spork), and that frame (the previous continuation's frame or empty in the case of a fresh spork)
* the continuation is captured. (this continuation will not be serialized)
* a new thread is created, with the continuation, the sack environment and the current frame as thread data. This frame starts executing our entrance point. The thread is created to make sure that, regardless of the sack stack/dynamic environment, it's possible to serialize continuations from this thread. It will also reduce unnecessary stuff from the continuation data.
* the thread of the sack request terminates itself, making sure not to return.
* the newly created thread will sooner or later decide to show a response and will then invoke the continuation that was previously captured.
saved in thread-specific storage:
* sack continuation.
* sack environment.
* current frame. ;; to implement bt vars
render-widget-cont ;; to implement fork
run-before-show-redirect ;; to implement ajax-fork
order of implementation:
bt vars
fork
ajax-fork
(begin
(define (wr obj)
(call-with-output-string
'()
(lambda (p)
(output-port-readtable-set!
p
(readtable-sharing-allowed?-set
(output-port-readtable p)
'serialize))
(write obj p))))
(define (rd str)
(call-with-input-string
str
(lambda (p)
(input-port-readtable-set!
p
(readtable-sharing-allowed?-set
(input-port-readtable p)
'serialize))
(read p))))
(define fifo (open-vector))
(define (suspend-and-die!)
(call/cc
(lambda (k)
(write (wr k) fifo)
(newline fifo)
(force-output fifo)
(thread-terminate! (current-thread)))))
(let ((dummy-port (open-string)))
(parameterize ((current-input-port dummy-port)
(current-output-port dummy-port))
(thread-start!
(make-thread
(lambda ()
(* 100
(suspend-and-die!)))))))
(define s (read fifo))
(thread-join!
(thread-start!
(make-thread
(lambda ()
((rd s) 111)))))
(thread-join!
(thread-start!
(make-thread
(lambda ()
((rd s) 222)))))
(string-length s))
(u8vector-length
(object->u8vector (open-string)
(lambda (x)
(cond
((mutex? x)
(write x) #f)
((condition-variable? x)
(write x) #f)
(else
x)))))
(##parameterize
((current-output-port #f))
(print "hej\n"))
(define fifo (open-vector))
(thread-start!
(##make-root-thread
(lambda ()
(let ((blah (string-append
"hej"
"san"
(number->string
(time->seconds (current-time)))))
(blah2 (string-append
"hej"
"san"
(number->string
(time->seconds (current-time)))))
(blah3 (string-append
"hej"
"san"
(number->string
(time->seconds (current-time))))))
(call/cc
(lambda (k)
(write (object->u8vector k) fifo)))
(display blah)
(display blah2)
(display blah3)))
"hej"
(thread-thread-group (current-thread))
#f
#f))
(u8vector-length (read fifo))
((u8vector->object (read fifo)))
(lambda (env)
(values 200
'(("Content-Type" . "text/plain; charset=UTF-8"))
(lambda ()
(display "Hello, world!")
#f)))