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