nx_mesh_probe.nx source
↩ module page · 52 lines · 2535 B
1// nx_mesh_probe.nx -- SHARED worker-mesh health probe (single source; consolidates the copy that lived in
2// nx_mesh_autoscale + nx_mesh_lb + nx_mesh_hostagent). A BOUNDED, non-blocking TCP probe: a cold/firewalled worker
3// is detected in ~1s instead of hanging on a blocking connect (fatal for an autoscaler that must SPAWN promptly).
4// mp_probe(a,b,c,d,port) -> 1 if the worker answers HTTP 2xx on /v1/models, else 0. NO main (library). license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host
7import "nx_http_client.nx" // nx_http_client_sockaddr_ipv4 + nx_http_client_build_request
8const K_MAGIC_2048: i64 = 2048
9const K_MAGIC_2000: i64 = 2000
10const K_MAGIC_16384: i64 = 16384
11const K_MAGIC_16383: i64 = 16383
12
13func mp_pollfd(pfd: *u8, fd: i64, events: i64) -> i64 {
14 pfd[0]=(fd&0xff) as u8; pfd[1]=((fd>>8)&0xff) as u8; pfd[2]=((fd>>16)&0xff) as u8; pfd[3]=((fd>>24)&0xff) as u8
15 pfd[4]=(events&0xff) as u8; pfd[5]=((events>>8)&0xff) as u8; pfd[6]=0 as u8; pfd[7]=0 as u8
16 return 0
17}
18func mp_resp_2xx(buf: *u8, n: i64) -> i64 {
19 var i: i64 = 0
20 while i + 3 < n {
21 if buf[i]==(50 as u8) { if buf[i+1]==(48 as u8) { if buf[i+2]==(48 as u8) { return 1 } } }
22 if buf[i]==(10 as u8) { i = n }
23 i = i + 1
24 }
25 return 0
26}
27// bounded warmth probe: non-blocking connect (SOCK_NONBLOCK=0x800) + poll(POLLOUT,1s); then GET + poll(POLLIN,2s) + read.
28func mp_probe(a: i64, b: i64, c: i64, d: i64, port: i64) -> i64 {
29 let sa: *u8 = sys_mmap(16)
30 nx_http_client_sockaddr_ipv4(sa, a, b, c, d, port)
31 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM | K_MAGIC_2048, 0)
32 if fd < 0 { return 0 }
33 nx_connect_bounded(fd, sa, 16, NX_CONN_DEFAULT_MS)
34 let pfd: *u8 = sys_mmap(8)
35 mp_pollfd(pfd, fd, 4)
36 let pn: i64 = sys_poll(pfd, 1, 1000)
37 if pn <= 0 { sys_close(fd); return 0 }
38 let rev: i64 = (pfd[6] as i64) | ((pfd[7] as i64) << 8)
39 if (rev & 8) != 0 { sys_close(fd); return 0 }
40 if (rev & 16) != 0 { sys_close(fd); return 0 }
41 let req: *u8 = sys_mmap(256)
42 let rl: i64 = nx_http_client_build_request("/v1/models" as *u8, 10, "worker" as *u8, 6, req)
43 sys_write(fd, req, rl)
44 mp_pollfd(pfd, fd, 1)
45 let pn2: i64 = sys_poll(pfd, 1, K_MAGIC_2000)
46 if pn2 <= 0 { sys_close(fd); return 0 }
47 let out: *u8 = sys_mmap(K_MAGIC_16384)
48 let got: i64 = sys_read(fd, out, K_MAGIC_16383)
49 sys_close(fd)
50 if got <= 0 { return 0 }
51 return mp_resp_2xx(out, got)
52}