interplane.Get Omega
Read in your language

cortex-rs

Pure compiled native Rust memory engine with vector search and SQLite WAL

Brought by drakestapleton

branch main Branches Tags History

cortex-rs / om / srv.om

560 lines · 11816 bytes Raw Blame History
1note srv.om: concurrent connections. Three kinds of process:
2note state process (the first one) owns the log and the in-memory store and answers forwarded
3note requests one at a time on a private local socket, so writes stay serialized
4note and crash-safe exactly as with one connection;
5note acceptor forked before the store is loaded (small, cheap to fork); owns the TCP
6note listener (non-blocking) and keeps a pool of handlers: at least SV_IDLE idle,
7note at most S_MAXH alive, an extra one leaving after 10 s without a connection;
8note handler a long-lived process that accepts a connection itself, reads one whole
9note request under the header, request and idle deadlines (hto), forwards the raw
10note bytes to the state process, reads the whole answer and writes it to the client
11note under its own deadline, then takes the next connection. It tells the acceptor
12note on a status pipe when it takes and finishes a connection. (Until 2026-11-11 the
13note acceptor forked one handler per connection: the fork cost about 0.25 ms of
14note each request, evidence/brain/bench.)
15note A slow or stalled client only holds its own handler. The state process never reads from or
16note writes to a client socket.
17global upath uchar 108
18global upl ulong 1
19global uad uchar 112
20global sfd uchar 8
21global sfp ulong 4
22global sst uchar 4
23global sone uchar 1
24global sstat uchar 128
25global pxn ulong 2
26global snh ulong 1
27const S_MAXH ulong 256
28const S_SOCKMX ulong 100
29const SV_IDLE ulong 2
30note handler pool: status pipe fds (sbp), status messages read (sbb, two bytes each: 98 or 100 and
31note the handler's slot), busy handlers (snb), each slot's pid (shp, 0 free) and busy flag (shb);
32note in a handler: swk 0 the status pipe's write end, swk 1 1 for an extra handler, swk 2 its slot
33global sbp uchar 8
34global sbb uchar 256
35global snb ulong 1
36global swk ulong 3
37global shp ulong S_MAXH
38global shb uchar S_MAXH
39global sms uchar 2
40text a_sock "--sock"
41text sockext ".sock"
42text m_sockp "cortex: private socket path too long (at most 100 bytes; use --sock PATH)\n"
43text m_sockx "cortex: --sock path exists and is not a socket\n"
44text m_sock "cortex: cannot create the private socket\n"
45text m_acc "cortex: acceptor process ended; exiting\n"
46
47note deadlines for client connections (handler) and for forwarded requests (state process)
48fn sv_tmo_client to void
49put hto 0 10000000000
50put hto 1 30000000000
51put hto 2 10000000000
52end
53fn sv_tmo_local to void
54put hto 0 2000000000
55put hto 1 2000000000
56put hto 2 2000000000
57end
58
59note --sock PATH (from jrs, n bytes)
60fn sv_setpath n ulong to bool
61if or eq n 0 gt n S_SOCKMX
62ret false
63end
64for k ulong n
65put upath k get jrs k
66end
67put upl 0 n
68ret true
69end
70note default private socket path: the log path plus ".sock"
71fn sv_defpath to bool
72if gt get upl 0 0
73ret true
74end
75n = get lpl 0
76if gt add n len sockext S_SOCKMX
77ret false
78end
79for k ulong n
80put upath k get lpath k
81end
82for k ulong len sockext
83put upath add n k get sockext k
84end
85put upl 0 add n len sockext
86ret true
87end
88note sockaddr_un for upath in uad; returns its length
89fn sv_addr to ulong
90put uad 0 1
91put uad 1 0
92n = get upl 0
93for k ulong n
94put uad add 2 k get upath k
95end
96put uad add 2 n 0
97ret add n 3
98end
99note 0 when upath is absent or a socket (removed), 1 when something else is there, 2 on error
100fn sv_clear to ulong
101r = lstat upath 0 get upl 0 sstat 0 len sstat
102if eq r neg long 2
103ret 0
104end
105if lt r 0
106ret 2
107end
108m = or ulong get sstat 16 shl ulong get sstat 17 8
109if ne and m 61440 49152
110ret 1
111end
112x = unlink upath 0 get upl 0
113if ne x 0
114ret 2
115end
116ret 0
117end
118note private listening socket, mode 0600 before listen; descriptor or -1 (message printed)
119fn sv_ulisten to int
120cl = sv_clear
121if eq cl 1
122say m_sockx
123ret -1
124end
125if eq cl 2
126say m_sock
127ret -1
128end
129fd = socknew 1 524289 0
130if lt fd 0
131say m_sock
132ret -1
133end
134um = umask 63
135r = bind int fd uad 0 sv_addr
136x = umask int um
137if ne r 0
138say m_sock
139ret -1
140end
141r = chmod upath 0 get upl 0 384
142if ne r 0
143say m_sock
144ret -1
145end
146r = listen int fd 1024
147if ne r 0
148say m_sock
149ret -1
150end
151ret int fd
152end
153note handler side: connect to the state process
154fn sv_connect to int
155fd = socknew 1 524289 0
156if lt fd 0
157ret -1
158end
159r = connect int fd uad 0 sv_addr
160if ne r 0
161x = fclose int fd
162ret -1
163end
164ret int fd
165end
166note little-endian descriptor word at sfd[off..off+4)
167fn sv_fdword off ulong to int
168v = ulong 0
169for i ulong 4
170b = ulong get sfd add off i
171v = or v shl b mul i 8
172end
173ret int v
174end
175
176note int at byte off of the status pipe pair sbp
177fn sv_sbword off ulong to int
178v = ulong 0
179for i ulong 4
180b = ulong get sbp add off i
181v = or v shl b mul i 8
182end
183ret int v
184end
185note send b[0..n) of rqraw to u within dl; true when all of it went
186fn sv_sendraw u int n ulong dl long to bool
187o = ulong 0
188while lt o n bound 999999
189now = monons
190if ge now dl
191ret false
192end
193w = waitio u 4 sub dl now
194if le w 0
195ret false
196end
197s = send u rqraw o sub n o 0
198if le s 0
199ret false
200end
201o = add o ulong s
202end
203ret true
204end
205note read the answer of the state process (it closes after it) into job; an answer larger than
206note job is passed on to the client in job-sized pieces. pxn 1 counts bytes received in total
207fn sv_recvall u int c int dl long to bool
208put pxn 0 0
209put pxn 1 0
210while true bound 999999
211n = get pxn 0
212if ge n len job
213if not sv_sendcli c add monons 30000000000
214ret false
215end
216put pxn 0 0
217n = ulong 0
218end
219now = monons
220if ge now dl
221ret false
222end
223w = waitio u 1 sub dl now
224if le w 0
225ret false
226end
227r = recv u job n sub len job n 0
228if eq r 0
229ret gt get pxn 1 0
230end
231if lt r 0
232ret false
233end
234put pxn 0 add n ulong r
235put pxn 1 add get pxn 1 ulong r
236end
237ret false
238end
239note write job[0..pxn 0) to the client within dl; true when all of it went
240fn sv_sendcli c int dl long to bool
241o = ulong 0
242n = get pxn 0
243while lt o n bound 999999
244now = monons
245if ge now dl
246ret false
247end
248w = waitio c 4 sub dl now
249if le w 0
250ret false
251end
252s = send c job o sub n o 0
253if le s 0
254ret false
255end
256o = add o ulong s
257end
258ret true
259end
260note one client connection, in a handler process
261fn sv_handle c int to void
262sv_tmo_client
263put hmir 0 1
264put hrawn 0 0
265st = http_read c
266if eq st 1
267ret
268end
269if ne st 0
270api_rderr c st
271ret
272end
273u = sv_connect
274if lt u 0
275http_send c 503 0 0 0
276ret
277end
278dl = add monons 120000000000
279ok = sv_sendraw u get hrawn 0 dl
280if ok
281ok = sv_recvall u c dl
282end
283x = fclose u
284if ok
285ok = sv_sendcli c add monons 30000000000
286end
287x2 = shutdown c 1
288end
289
290note reap finished handlers without blocking; a handler that ends while busy (a trap inside
291note sv_handle) is no longer counted as busy
292fn sv_reap to void
293for k ulong 1024
294r = waitpid neg int 1 sst 0 len sst 1
295if le r 0
296ret
297end
298h = get snh 0
299if gt h 0
300put snh 0 sub h 1
301end
302for s ulong S_MAXH
303if eq get shp s ulong r
304if and eq get shb s 1 gt get snb 0 0
305put snb 0 sub get snb 0 1
306end
307put shp s 0
308put shb s 0
309end
310end
311end
312end
313note acceptor loop; returns in the acceptor when the state process is gone (its end of the go
314note pipe pr closed, so pr turns readable), and in each handler when it leaves sv_work (both then
315note exit). The status messages are read before the reaping, so a dead handler's last message is
316note counted before its slot is freed, and once more after it (before a slot is reused), so a message
317note a handler wrote just before it died is dropped instead of being given to the slot's next
318note handler. When the state process is gone the listener is shut down,
319note so a new server can bind the port while a busy handler still finishes its client. getppid is
320note not used: omc 189ada82 emits the x86-64 number (110) for it on arm64, fixed upstream after
321note this pin.
322fn sv_accept lfd int pr int to void
323put snh 0 0
324put snb 0 0
325r = pipefds sbp 0 len sbp 524288
326if ne r 0
327ret
328end
329sr = sv_sbword 0
330sw = sv_sbword 4
331while true bound 999999
332for k ulong 999999
333if gt waitio pr 1 0 0
334x = shutdown lfd 0
335ret
336end
337sv_drain sr
338sv_reap
339sv_drain sr
340if sv_spawn lfd pr sr sw
341ret
342end
343w = waitio sr 1 200000000
344if gt w 0
345sv_drain sr
346end
347end
348end
349end
350note fork handlers until SV_IDLE are idle or S_MAXH are alive; true in a handler that has left
351note sv_work
352fn sv_spawn lfd int pr int sr int sw int to bool
353for k ulong 256
354h = get snh 0
355if ge h S_MAXH
356ret false
357end
358b = get snb 0
359if and gt h b ge sub h b SV_IDLE
360ret false
361end
362s = sv_slot
363if eq s S_MAXH
364ret false
365end
366pid = fork
367if lt pid 0
368ret false
369end
370if eq pid 0
371x = fclose sr
372put swk 0 ulong sw
373put swk 1 0
374if ge h SV_IDLE
375put swk 1 1
376end
377put swk 2 s
378sv_work lfd pr
379ret true
380end
381put shp s ulong pid
382put shb s 0
383put snh 0 add h 1
384end
385ret false
386end
387note a free handler slot (S_MAXH when none)
388fn sv_slot to ulong
389for s ulong S_MAXH
390if eq get shp s 0
391ret s
392end
393end
394ret S_MAXH
395end
396note read every status message waiting on the pipe: 98 a handler took a connection, 100 finished it
397note (two-byte writes are atomic on a pipe, and sbb holds a whole number of messages)
398fn sv_drain sr int to void
399for k ulong 4096
400if le waitio sr 1 0 0
401ret
402end
403n = read sr sbb 0 len sbb
404if le n 0
405ret
406end
407for i ulong div ulong n 2
408j = mul i 2
409c = ulong get sbb j
410sv_msg c ulong get sbb add j 1
411end
412end
413end
414fn sv_msg c ulong s ulong to void
415if ge s S_MAXH
416ret
417end
418if eq get shp s 0
419ret
420end
421if and eq c 98 eq get shb s 0
422put shb s 1
423put snb 0 add get snb 0 1
424end
425if and eq c 100 eq get shb s 1
426put shb s 0
427if gt get snb 0 0
428put snb 0 sub get snb 0 1
429end
430end
431end
432note a handler: accept and serve connections until the state process is gone, or (an extra
433note handler) until 10 s pass without a connection. The listener is non-blocking: when another
434note handler took the connection first, accept fails and the handler waits again.
435fn sv_work lfd int pr int to void
436t0 = monons
437while true bound 999999
438for k ulong 999999
439if gt waitio pr 1 0 0
440ret
441end
442w = waitio lfd 1 200000000
443if gt w 0
444c = accept lfd 524288
445if ge c 0
446sv_note 98
447sv_handle int c
448x = fclose int c
449sv_note 100
450t0 = monons
451end
452else
453if eq get swk 1 1
454if gt sub monons t0 10000000000
455ret
456end
457end
458end
459end
460end
461end
462fn sv_note b ulong to void
463put sone 0 uchar b
464put sms 0 uchar b
465put sms 1 uchar get swk 2
466x = write int get swk 0 sms 0 2
467end
468note acceptor process: wait for the state process's go byte, bind TCP, report, serve
469fn sv_accmain pr int qw int to int
470n = read pr sone 0 1
471if ne n 1
472ret 0
473end
474fd = main_listen
475if lt fd 0
476put sone 0 110
477x = write qw sone 0 1
478ret 4
479end
480put sone 0 111
481x = write qw sone 0 1
482x = fclose qw
483sv_accept fd pr
484ret 0
485end
486note start the acceptor; 0 on success with the pipe ends in sfp (0 go-write, 1 ack-read, 2 pid),
487note 1 when this process is the acceptor or a handler and has finished (exit status in sfp 3)
488fn sv_start u int to ulong
489r = pipefds sfd 0 len sfd 524288
490if ne r 0
491put sfp 3 4
492ret 1
493end
494gr = sv_fdword 0
495gw = sv_fdword 4
496r = pipefds sfd 0 len sfd 524288
497if ne r 0
498put sfp 3 4
499ret 1
500end
501ar = sv_fdword 0
502aw = sv_fdword 4
503pid = fork
504if lt pid 0
505put sfp 3 4
506ret 1
507end
508if eq pid 0
509x = fclose u
510x = fclose gw
511x = fclose ar
512put sfp 3 ulong sv_accmain gr aw
513ret 1
514end
515x = fclose gr
516x = fclose aw
517put sfp 0 ulong gw
518put sfp 1 ulong ar
519put sfp 2 ulong pid
520ret 0
521end
522note state process: tell the acceptor to bind; true when it reports success
523fn sv_go to bool
524put sone 0 103
525w = write int get sfp 0 sone 0 1
526if ne w 1
527ret false
528end
529put sone 0 0
530n = read int get sfp 1 sone 0 1
531if or ne n 1 ne get sone 0 111
532x = waitpid int get sfp 2 sst 0 len sst 0
533ret false
534end
535ret true
536end
537note state process loop: one forwarded request at a time; returns when the acceptor has ended
538fn sv_serve u int to void
539sv_tmo_local
540put hprox 0 1
541put hmir 0 0
542apid = int get sfp 2
543while true bound 999999
544for k ulong 999999
545w = waitio u 1 1000000000
546if gt w 0
547c = accept u 524288
548if ge c 0
549api_conn int c
550x = fclose int c
551end
552end
553r = waitpid apid sst 0 len sst 1
554if ne r 0
555say m_acc
556ret
557end
558end
559end
560end